简体   繁体   English

带有术语过滤器的弹性搜索过滤查询

[英]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.您的role字段可能是经过analyzed字符串。 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)或将您的role字段的映射更改为not_analyzed (您需要重新索引您的数据以使更改生效)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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