Elasticsearch Java API - query building

I looked through the documents for the search API , but I find them not descriptive enough (although they are very well written). I am trying to create a query, but I don’t understand much about all available options and cannot find information on this issue when creating a query, and I can’t translate queries that I can run into Sense for queries that I can run using the Java API .

In Sense, I have the following:

GET index/_search { "query": { "match" : { "name" : "some string" } } } 

And in my Java code, I:

 node = nodeBuilder().client(true).clusterName(CLUSTER_NAME).node(); client = node.client(); QueryBuilder qb = QueryBuilders.termQuery("name", "some string"); SearchResponse response = client.prepareSearch("index") // .setQuery(qb) // Query .execute().actionGet(); 

But they create different search results. What is the difference since I do not see this? Also there is a good source of information that may be useful?

+7
java elasticsearch
source share
1 answer

If you want two queries to return the same results, you need to use the same type of query. In a Sense request, you execute a compliance request:

 "query": { "match" : { "name" : "some string" } } 

but in your java code you execute termQuery:

 QueryBuilder qb = QueryBuilders.termQuery("name", "some string"); 

So, to answer your question, use the matching code in your Java code instead:

 QueryBuilder qb = QueryBuilders.matchQuery("name", "some string"); 

As for your second question, it is a bit broad. I would of course try to go through the documentation and search here in StackOverflow. Regarding the Java API, I would look here for a review and here for information on the dsl thru Java query.

I think a good general understanding of how Elasticsearch works and some comfort with a request mechanism through the REST API will be very useful for understanding the Java API. Good places to start:

http://joelabrahamsson.com/elasticsearch-101/

http://exploringelasticsearch.com/

http://java.dzone.com/articles/elasticsearch-getting-started

+13
source share

All Articles