Hello. Please tell me how to create a query to search for the desired item in several fields. For example:

{ "_index": "myindex", "_type": "users", "_id": "1", "_source": { "name": "Ivan", "age": "20", } } { "_index": "myindex", "_type": "users", "_id": "2", "_source": { "name": "Ivan", "age": "25", } } 

I need to create a query of the type: SELECT FROM users WHERE name = "Ivan" AND age = "25"

    2 answers 2

    do not refuse to look into the documentation until you understand how the tool works and where to look for answers, and you will not start using it

     { "query": { "bool": { "must": [ { "term": { "name": "Ivan" } }, { "term": { "age": "25" } } ] } } } 

    I also recommend reading the section on analyzers and terms before starting to search by lines, if the request consists of several words (eg "John Doe"), you will be surprised by the result.

    • I know that. I need to make the same request using Java API - PREDATORik
    • one
      @PREDATORik is absolutely identical to dsl there. QueryBuilders.boolQuery() , QueryBuilders.termQuery() . if I have time, I will write a complete example - etki
    • I will be very grateful - PREDATORik

    Solved, thanks to Etki

      QueryBuilder qb = boolQuery().must(termQuery("name", "Ivan")).must(termQuery("age", "25")); SearchRequestBuilder requestBuilder = client.getClient().prepareSearch(client.getIndex()).setTypes("users").setQuery(qb).setSize(1); SearchResponse response = requestBuilder.execute().actionGet();