【发布时间】:2021-06-03 08:14:55
【问题描述】:
假设我创建了一个索引 people,它将获取具有两个属性的条目:name 和 friends
PUT /people
{
"mappings": {
"properties": {
"friends": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
我放了两个条目,每个条目都有两个朋友。
POST /people/_doc
{
"name": "Jack",
"friends": [
"Jill", "John"
]
}
POST /people/_doc
{
"name": "Max",
"friends": [
"John", "John" # Max will have two friends, but both named John
]
}
现在我想搜索有多个朋友的人
GET /people/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"source": "doc['friends.keyword'].length > 1"
}
}
}
]
}
}
}
这只会返回 Jack 并忽略 Max。我假设这是因为我们实际上是在遍历倒排索引,而 John 和 John 只创建了一个标记 - 'john',所以这里标记的长度实际上是 1。
由于我的索引比较小,性能不是关键,所以我想实际遍历源而不是倒排索引
GET /people/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"source": "ctx._source.friends.length > 1"
}
}
}
]
}
}
}
但根据https://github.com/elastic/elasticsearch/issues/20068的说法,只有更新时才支持源,搜索时不支持,所以我不能。
一个明显的解决方案似乎是获取字段的长度并将其存储到索引中。 friends_count: 2 之类的东西,然后根据它进行过滤。但这需要重新索引,而且这似乎是应该以某种明显的方式解决的问题。
非常感谢。
【问题讨论】:
标签: elasticsearch elastic-stack