elasticsearch,filter,Arrays,elasticsearch,Filter" /> elasticsearch,filter,Arrays,elasticsearch,Filter" />

Arrays Elasticsearch-数组筛选器中的值

Arrays Elasticsearch-数组筛选器中的值,arrays,elasticsearch,filter,Arrays,elasticsearch,Filter,我想过滤掉数组字段中包含特定值的所有文档。即,该值是该数组字段的一个元素 具体来说,我想选择名称包含测试名称的所有文档,请参见下面的示例 所以当我用 curl -XGET localhost:9200/test-index/_search 结果是 { "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": {

我想过滤掉数组字段中包含特定值的所有文档。即,该值是该数组字段的一个元素

具体来说,我想选择
名称
包含
测试名称
的所有文档,请参见下面的示例


所以当我用

curl -XGET localhost:9200/test-index/_search
结果是

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 50,
    "max_score": 1,
    "hits": [
      {
        "_index": "test-index",
        "_type": "test",
        "_id": "34873ae4-f394-42ec-b2fc-41736e053c69",
        "_score": 1,
        "_source": {
          "names": [
            "test-name"
          ],
          "age": 100,
          ...
        }
      },
      ...
   }
}

但如果有更具体的查询

curl -XPOST localhost:9200/test-index/_search -d '{
  "query": {
    "bool": {
      "must": {
        "match_all": {}

      },
      "filter": {
        "term": {
           "names": "test-name"
        }
      }
    }
  }
}'
我没有得到任何结果

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

有一些问题与此类似。尽管如此,我还是无法找到任何适合我的答案

系统规格:
Elasticsearch 5.1.1
Ubuntu 16.04


编辑

curl -XGET localhost:9200/test-index
          ...
          "names": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          ...

这是因为分析了
名称
字段,并将
测试名称
索引为两个标记
测试
名称

因此,搜索
测试名称
项不会产生任何结果。如果改用
match
,您将获得文档

如果要检查确切的值
测试名称
(即两个令牌一个接一个),则需要将
名称
字段更改为
关键字
类型,而不是
文本

更新

根据您的映射,将分析
names
字段,您需要使用
names.keyword
字段,它将工作,如下所示:

curl -XPOST localhost:9200/test-index/_search -d '{
  "query": {
    "bool": {
      "must": {
        "match_all": {}

      },
      "filter": {
        "term": {
           "names.keyword": "test-name"
        }
      }
    }
  }
}'

名称
是类型
关键字
。将查询更改为
{“query”:{“match”:{“names”:“test name”}}
会返回多个结果。能否使用运行
curl-XGET localhost:9200/test index
得到的结果更新您的问题?您现在已将问题从
test name
更改为
test name
。。。HMMMYES,之前没有注意到,抱歉。是的,因为没有分析
names.keyword
字段。