简体   繁体   中英

Elastic Search Filtered Query with term filter

I do have a problem with a filtered query. When I run the following query :

{
"query":{
  "filtered":{
     "query":{
        "regexp":{
           "name":"chri.*"
        }
     },
     "filter":{
        "bool":{
           "must":[
                {"term": { "accountNonLocked": "false" }}
            ]
        }
     }
  }
},"fields" : ["name", "id", "accountNonLocked","role"], 
"from" : 0, 
"size" : 10
}

I got to following response

{
"took": 4,
  "timed_out": false,
  "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
  },
  "hits": {
  "total": 2,
  "max_score": 1,
  "hits": [
  {
    "_index": "candidatindex",
    "_type": "job",
    "_id": "54c7867ecfcbbe42dc000478",
    "_score": 1,
    "fields": {
      "name": [
        "Christophe toto"
      ],
      "accountNonLocked": [
        true
      ],
      "role": [
        "DHR"
      ]
    }
  },
  {
    "_index": "candidatindex",
    "_type": "job",
    "_id": "54c7867ecfcbbe42dc000468",
    "_score": 1,
    "fields": {
      "name": [
        "Christophe Toto"
      ],
      "accountNonLocked": [
        true
      ],
      "role": [
        "CANDIDATE"
      ]
    }
  }
  ]}
}

But when I add a filter on the role like this :

{
"query":{
  "filtered":{
     "query":{
        "regexp":{
           "name":"chri.*"
        }
     },
     "filter":{
        "bool":{
           "must":[
                {"term": { "accountNonLocked": "false" }},
                {"term": { "role": "CANDIDATE" }}
            ]
        }
     }
  }
},"fields" : ["name", "id", "accountNonLocked","role"], 
"from" : 0, 
"size" : 10
}

Then I got an empty result :

{
"took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

Does anyone can tell me what I am doing wrong ?

Your role field is probably an analyzed string. So either try with a lowercase value like in

 {"term": { "role": "candidate" }}

or change the mapping of your role field to be not_analyzed (you'll need to reindex your data for the change to take effect)

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