1

I have json object i want to search through it looks like this
I currently am able to do a range of price search but i want to add search by city , which is a property of departure object so i decided to do a nested query but i get error

My ES logs shows

e_synonyms_phrase_query":true,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},"path":"departure","ignore_unmapped":false,"score_mode":"avg","boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}}}}] lastShard [true]
org.elasticsearch.transport.RemoteTransportException: [AoBfpnE][127.0.0.1:9300][indices:data/read/search[phase/query]]
Caused by: org.elasticsearch.index.query.QueryShardException: failed to create query: {
  "bool" : {
    "filter" : [
      {
        "range" : {
          "price" : {
            "from" : 1300,
            "to" : 5000,
            "include_lower" : true,
            "include_upper" : true,
            "boost" : 1.0
          }
        }
      },
      {
        "nested" : {
          "query" : {
            "bool" : {
              "must" : [
                {
                  "match" : {
                    "departure.city" : {
                      "query" : "minsk",
                      "operator" : "OR",
                      "prefix_length" : 0,
                      "max_expansions" : 50,
                      "fuzzy_transpositions" : true,
                      "lenient" : false,
                      "zero_terms_query" : "NONE",
                      "auto_generate_synonyms_phrase_query" : true,
                      "boost" : 1.0
                    }
                  }
                }
              ],
              "adjust_pure_negative" : true,
              "boost" : 1.0
            }
          },
          "path" : "departure",
          "ignore_unmapped" : false,
          "score_mode" : "avg",
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
    at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:320) ~[elasticsearch-6.3.2.jar:6.3.2]
    at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:303) ~[elasticsearch-6.3.2.jar:6.3.2]

1 Answer 1

2

The correct query looks like this:

    QueryBuilder range = QueryBuilders.rangeQuery("price")
            .from(minPrice)
            .to(maxPrice)
            .includeLower(true)
            .includeUpper(true);

    QueryBuilder city = QueryBuilders.matchQuery("departure.city", city);

    QueryBuilder query = QueryBuilders.boolQuery()
            .filter(range)
            .filter(city);

    ...

    searchSourceBuilder.query(query);
Sign up to request clarification or add additional context in comments.

13 Comments

thanks actually it works for parameters such as nights, people but not for city , I think because city belongs to an object , so when i run your code it retruns empty but if i change departure.city to nights and give it value it returns data
If departure is not of type nested but object it should work. What's the mapping of the departure field?
so the whole data is mapped to a Tour class while departure is a object that Tour is composed off
what do you get when running GET your_index ?
Caused by: java.lang.IllegalStateException: [nested] nested object under path [departure] is not of nested type
|

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.