Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query with multiple values on a property with one value in Elasticsearch

I am trying to build on this query a little bit. The index I am searching also has a field "entity" with an id. So a few records will have "entity" : 16, "entity" 156 etc, depending on the id of the entity. I need to expand this query in such a way that I can pass an array or some list of values in, such as {:term => {:entity => [1, 16, 100]}} and get back records that have one of these integers as their entity value. I haven't had any luck so far, can someone help me?

{ 
  "query" : {

    "bool" : {
      "must" : [
        {
          "term" : {"user_type" : "alpha"}
        }, 
        { 
          "term" :{"area" : "16"}
        }
      ], 
      "must_not" : [], 
      "should" :   []
    }
  }, 
  "filter": {
    "or" : [{
       "and" : [
          { "term" : { "area" : "16" } },
          { "term" : { "date" : "05072013" } }
       ]
    }, {
       "and" : [
          { "term" : { "area" : "16" } },
          { "term" : { "date" : "blank" } }
       ]
    }


    ]
  },
"from" : 0,
"size" : 100 
}
like image 812
Aristata Avatar asked Apr 27 '13 18:04

Aristata


People also ask

How do I query multiple indices in Elasticsearch?

To search multiple data streams and indices, add them as comma-separated values in the search API's request path. The following request searches the my-index-000001 and my-index-000002 indices. You can also search multiple data streams and indices using an index pattern.

How do I search multiple fields in Elasticsearch?

One of the most common queries in elasticsearch is the match query, which works on a single field. And there's another query with the very same options that works also on multiple fields, called multi_match. These queries support text analysis and work really well.

How do I merge two queries in Elasticsearch?

You need to use the bool query to combine different queries together. You can then choose whether each single query must match, should match (optional), or must not match.


1 Answers

Use "terms" instead of "term".

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

{ "terms" : { "entity" : [ 123, 1234, ... ] }}

like image 163
mbj Avatar answered Oct 31 '22 16:10

mbj