2

I am using the following elasticsearch query to fetch the details,

{
    "query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "bool": {
                    "should": [
                    {"match": {
                        "val": "GET"
                    }}]
                }
            }
        }
    }
}

It is working fine and given the result as required.

I want to execute the same query through java and get the same results and tried the following,

 getClient().prepareSearch(esIndex)
                .setQuery(QueryBuilders.queryStringQuery(QUERY)).execute().actionGet();

It is not returning the data and throw some query format wrong exception as well.

Is there any Java API available using which the same query can be executed?

NOTE: There is a possibility available to create the boolquery and aggregation builders in java api and execute the same. I am just curious to find a way to execute this query directly through elasticsearch java api

4
  • Hmm. Don't you think the content of that query that you give to your Java code would matter? Especially given the fact that java tells you that that query is not well formatted? So... Why is that part missing from your question? Commented Mar 26, 2017 at 6:06
  • I have passed the same query which is worked well through the HTTP POST. Commented Mar 26, 2017 at 6:16
  • You are using a query string constant. And you claim what is written in that string. Instead of just showing us that code. Commented Mar 26, 2017 at 7:07
  • Which version of Es you are using ? Commented Mar 26, 2017 at 10:52

3 Answers 3

3

If you really want to use the Query String Query, your query has to follow Query String Syntax:

getClient().prepareSearch(esIndex)
           .setQuery(QueryBuilders.queryStringQuery("val: \"GET\""))
           .execute()
           .actionGet();

As already stated, you should construct your query by using the provided QueryBuilders instead of strings. This will keep your code clean and readable even for complex queries.

getClient().prepareSearch(esIndex)
           .setQuery(QueryBuilders.boolQuery()
                                  .should(QueryBuilders.matchQuery("val", "GET"))
           .execute()
           .actionGet();
Sign up to request clarification or add additional context in comments.

Comments

2
BoolQueryBuilder bool       =   boolQuery();

bool.must(QueryBuilders.matchAllQuery()); 
    bool.filter(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("Val", "GET")));

    AggregationBuilder agg = AggregationBuilders.terms("").field("");

    SearchResponse reponse =    getClient().prepareSearch().setIndices("indexName").setTypes("indexType")
                .setQuery(bool).addAggregation(agg).execute().actionGet();

Comments

1

you should use boolQuery() when you construct your QueryBuilder:

QueryBuilder qb = boolQuery()
            .must(termQuery("content", "test1"))
            .must(termQuery("content", "test4"))
            .mustNot(termQuery("content", "test2"))
            .should(termQuery("content", "test3"))
            .filter(termQuery("content", "test5"));

Official docs: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-compound-queries.html

Comments

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.