简体   繁体   中英

How do I query for elasticsearch documents with exact field1 match and non-empty field2 match

I have documents in elasticsearch with field1 mapped to an integer value and field2 mapped to an array. I would like to create a query to find all documents with field1 == 100 and field2 is not empty.

How do I write such a query?

Use term query to match field1 to value 100 and use exists query to check if field2 exists (not empty). Both the above conditions are AND so wrap them in must clause of bool query .

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "field1": 100
          }
        },
        {
          "exists": {
            "field": "field2"
          }
        }
      ]
    }
  }
}

Update based on the comment by @Val:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "field1": 100
          }
        },
        {
          "exists": {
            "field": "field2"
          }
        }
      ]
    }
  }
}

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