[英]Elasticsearch - value in array filter
I want to filter out all documents which contain a specific value in an array field. 我想过滤掉所有在数组字段中包含特定值的文档。 Ie the value is an element of that array field.
即,值是该数组字段的元素。
To be specific - I want to select all documents which names
contains test-name
, see the example below. 具体来说-我想选择
names
包含test-name
所有文档,请参见下面的示例。
So when I do an empty search with 所以当我用
curl -XGET localhost:9200/test-index/_search
the result is 结果是
{
"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,
...
}
},
...
}
}
But in case of a more specific query 但是在更具体的查询的情况下
curl -XPOST localhost:9200/test-index/_search -d '{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"term": {
"names": "test-name"
}
}
}
}
}'
I don't get any results 我没有任何结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
There are some questions similar to this one. 有一些与此类似的问题。 Although, I cannot get any of the answers to work for me.
虽然,我无法获得为我工作的任何答案。
System specs: Elasticsearch 5.1.1
, Ubuntu 16.04
系统规格:
Elasticsearch 5.1.1
, Ubuntu 16.04
EDIT 编辑
curl -XGET localhost:9200/test-index
...
"names": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
...
That's because the names
field is analyzed and test name
gets indexed as two tokens test
and name
. 这是因为对
names
字段进行了分析,并且test name
被索引为两个标记test
和name
。
Searching for the test name
term will hence not yield anything. 因此,搜索
test name
术语将不会产生任何结果。 If you use match
instead, you'll get the document. 如果改用
match
,则将获得文档。
If you want to check for the exact value test name
(ie the two tokens one after another), then you need to change your names
field to a keyword
type instead of text
如果要检查确切值的
test name
(即两个标记一个接一个),则需要将names
字段更改为keyword
类型而不是text
UPDATE UPDATE
According to your mapping, the names
field is analyzed, you need to use the names.keyword
field instead and it will work, like this: 根据您的映射,对
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.