[英]Elasticsearch - value in array filter
我想过滤掉所有在数组字段中包含特定值的文档。 即,值是该数组字段的元素。
具体来说-我想选择names
包含test-name
所有文档,请参见下面的示例。
所以当我用
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
}
}
},
...
这是因为对names
字段进行了分析,并且test name
被索引为两个标记test
和name
。
因此,搜索test name
术语将不会产生任何结果。 如果改用match
,则将获得文档。
如果要检查确切值的test name
(即两个标记一个接一个),则需要将names
字段更改为keyword
类型而不是text
UPDATE
根据您的映射,对names
字段进行了分析,您需要改为使用names.keyword
字段,它将起作用,如下所示:
curl -XPOST localhost:9200/test-index/_search -d '{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"term": {
"names.keyword": "test-name"
}
}
}
}
}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.