简体   繁体   中英

Elastic Search: Multiple term search in one query

New to ElasticSearch.

I have documents under an index : myindex in Elastic search with mappings: http://host:port/myindex/_mapping

{
"mappings":{
   "properties": {
        "en_US":  {
             "type": "keyword"
                  }
                 }
          }
}

Let's say my 3 documents look like this:

{
"product": "p1",
"subproduct": "p1.1"
}

{
"product": "p1",
"subproduct": "p1.2"
}

{
"product": "p2",
"subproduct": "p2.1"
}

Now, I am querying using for single subproduct p1.1 with product p1 as below and it's working fine:

POST: http://host:port/myindex/_search

{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "product" : "p1" }
      },
      "filter": {
        "term" : { "subproduct" : "p1.1" }
      }
    }
  }
}

My question is : How I can query for 2 or more subproducts in one _search query, like suproducts p1.1 and p1.2 under product p1 ? Query should return list of all subproduct p1.1 and subproduct p1.2 with p1 product.

Simply change the term -query in your filter-clause to a terms -query and search for multiple terms.

{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "product" : "p1" }
      },
      "filter": {
        "terms" : { "subproduct" : ["p1.1", "p1.2"] }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM