ElasticSearch Bool Fitler 似乎不适用于字符串项

ElasticSearch Bool Fitler doesn't seem to work with string terms

我正在尝试使用布尔过滤器执行查询,但当类型为字符串时它似乎不起作用。

示例:

当我将此有效负载发送到我的 _search 端点时:

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        { "term": { "level" : 3 } },
                        { "term": { "id" : 4 } }
                    ]
                }
            }
        }
    }
}

结果是:

{
  "took": 29,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "app",
        "_type": "reviewable",
        "_id": "4",
        "_score": 1,
        "_source": {
          "id": 4,
          "name": "Hololens Bootcamp Experience",
          "providers": [],
          "gender": [],
          "duration": "3",
          "category": "COURSE",
          "themes": [],
          "baseThemes": [],
          "budget": "1000+",
          "language": "EN",
          "level": "3",
          "region": "NL_BR",
          "subscribemethod": "2",
          "kind": "2"
        }
      }
    ]
  }
}

然后当我尝试用这样的语言术语过滤它时:

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        { "term": { "level" : 3 } },
                        { "term": { "id" : 4 } },
                        { "term": { "language" : "EN" } }
                    ]
                }
            }
        }
    }
}

我的结果有 0 次点击:

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

这对我来说很奇怪,因为您可以在我的第一个结果中看到过滤器应该与我的数据相匹配。

我四处搜索,有人 post 提到我可能没有将所有内容都编入索引。但据我所知,我肯定有。有什么想法吗?

您的语言字段很可能是经过分析的字符串,因此 EN 被标记化并索引为 en,因此您的查询应该是这样的:

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        { "term": { "level" : 3 } },
                        { "term": { "id" : 4 } },
                        { "term": { "language" : "en" } }   <--- lowercase here
                    ]
                }
            }
        }
    }
}

另一种方法是使用 match 查询,但不使用包装 filtered 查询,这对您的情况没有用:

{
    "query": {
         "bool": {
             "must": [
                 { "term": { "level" : 3 } },
                 { "term": { "id" : 4 } },
                 { "match": { "language" : "EN" } }
             ]
         }
    }
}