gpt4 book ai didi

elasticsearch - 嵌套数组上的elasticsearch过滤器

转载 作者:行者123 更新时间:2023-12-03 00:44:56 25 4
gpt4 key购买 nike

lets say records have city field as an array of city names. 
记录前:
  record 1:
{
cities : [
{name: city1},
{name : city2},
{name : city3}
]
}
record 2:
{
cities : [
{name: city2},
{name : city3},
{name : city4}
]
}
record 3:
{
cities : [
{name: city3},
{name : city4},
{name : city5}
]
}
需求:
我的过滤条件是获取与city1或city2或city3匹配的记录,但是由于记录1与所有3匹配,因此它应该排在第一位,而记录2与2匹配,因此它应该排在第二位,而记录3仅与一个匹配,因此应该排在最后。

最佳答案

您不必使用nested数据类型,因为您没有嵌套的属性或复杂的对象,这非常简单且易于实现。
工作实例
索引映射

{
"mappings": {
"properties": {
"cities": {
"type": "text"
}
}
}
}
索引样本文档
{
"cities": [
"tel-aviv", "bangalore", "sf"
]
}

{
"cities": [
"tel-aviv"
]
}

{
"cities": [
"sf"
]
}
搜索查询
{
"query": {
"bool": {
"should": [
{
"match": {
"cities": "tel-aviv"
}
},
{
"match": {
"cities": "bangalore"
}
},
{
"match": {
"cities": "sf"
}
}
]
}
}
}
并且搜索结果具有正确的预期结果和得分
 "hits": [
{
"_index": "cities",
"_type": "_doc",
"_id": "1",
"_score": 1.850198,
"_source": {
"cities": [
"tel-aviv",
"bangalore",
"sf"
]
}
},
{
"_index": "cities",
"_type": "_doc",
"_id": "2",
"_score": 0.9983525,
"_source": {
"cities": [
"tel-aviv"
]
}
},
{
"_index": "cities",
"_type": "_doc",
"_id": "3",
"_score": 0.6133945,
"_source": {
"cities": [
"sf"
]
}
}
]

关于elasticsearch - 嵌套数组上的elasticsearch过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63806067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com