0

I am new to Elasticsearch. I have the following query:

{
    "query": {
        "filtered" : {
            "query" : {
                "term" : {
                    "title" : "crime"
                }
            },
            "filter" : {
                "term" : { "year" : 1961 }
            }
        }
    }
}

It runs fine under Windows prompt as follows:

curl -XGET localhost:9200/book/_search?pretty -d @my-query.json

For the same query with Java client, I have the following:

    SearchResponse sr = client.prepareSearch("book")
            .setTypes("fiction")
            .setQuery(query_string)
            .setFrom(page)
            .setSize(10).execute().actionGet(); 

However, I have to the following query string in order to run it without exception:

{

        "filtered" : {
            "query" : {
                "term" : {
                    "title" : "crime"
                }
            },
            "filter" : {
                "term" : { "year" : 1961 }
            }
        }

}

Why is there such a difference? How can I retain the removed "query" property"? Suppose that I have to use query string in my Java client.

Thanks and regards!

1 Answer 1

1

Strictly speaking, the two variants you show are not the same: you don't specify the type, offset, or size parameters in your URI-based query (even though you can do it there too, according to the docs). You can omit these parameters in Java query as well:

SearchResponse sr = client.prepareSearch("book")
            .setQuery(query_string)
            .execute().actionGet(); 

Regarding the argument for setQuery, it can be either the same JSON as you have in your URI variant:

String theQuery = String.join(System.getProperty("line.separator"),
   "{\"filtered\" : {\"query\" : {\"term\" : {\"title\" : \"crime\"}},",
    "\"filter\" : {\"term\" : { \"year\" : 1961 }}}}");

SearchResponse sr = client.prepareSearch("book") 
.setTypes("fiction")
.setFrom(page)
.setQuery(queryString(theQuery)).execute().actionGet(); 

Or you can provide the analog of this query using Java methods:

SearchResponse sr = client.prepareSearch("book") 
.setTypes("fiction")
.setFrom(page)
.setQuery(filteredQuery(QueryBuilders.termQuery("title","crime"),
                       FilterBuilders.termFilter("year","1961")))
.execute().actionGet(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Ashalynd, thanks so much for helping me! Where did you get String.join method? theQuery variable you use contains is NOT the same query string as what I used in Windows prompt. Can I use exactly the same query string as in Windows prompt in Java client? Why do I have to remove "query" as shown (which is also shown in your theQuery)?
the outer "query" is designating a query part, it's not needed because the method is already called setQuery.
Regarding String.join: it's Java8. If you can't use it, you can look at StringUtils from Apache where there is a similar functionality, or guava library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.