我想过滤掉所有在数组字段中包含特定值的文档。即该值是该数组字段的元素。
具体来说-我想选择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
更新
根据您的映射,分析了names
字段,您需要改为使用names.keyword
字段,它将起作用,如下所示:
curl -XPOST localhost:9200/test-index/_search -d '{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"term": {
"names.keyword": "test-name"
}
}
}
}
}'
关于arrays - Elasticsearch-数组过滤器中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43683526/