Updated on 2024-11-29 GMT+08:00

Query Based on Filter Conditions

The query operation on Elasticsearch includes query or filter. In query mode, the score of each returned document is calculated and the scores are sorted by default. In filter mode, only the documents that meet the requirements are filtered and the score is not calculated. In addition, the documents can be cached.

For non-full-text search scenarios, if you do not care about the correlation between query results and query conditions but want to search for target data, you can use the filter mode to improve query efficiency.

The following is an example of query in security mode:

curl -XGET --tlsv1.2 --negotiate -k -u : "https://ip:httpport/myindex-001/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "age": "56"
    }
  }
}'

The following is an example of filter in security mode:

curl -XGET --tlsv1.2 --negotiate -k -u : "https://ip:httpport/myindex-001/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "filter": {
         "match": {
          "age": "56"
        }
      }
    }
  }
}'