gpt4 book ai didi

elasticsearch - 根据嵌套数组中的字段对结果进行排名

转载 作者:行者123 更新时间:2023-12-02 23:42:17 24 4
gpt4 key购买 nike

假设我有一个名为“neighborhood”的elasticsearch索引,并且在其中存储具有以下设置和映射的文档(下面的示例)。
模型中有很多嵌套字段,因此我在其中许多字段上使用了copy_to来简化搜索-例如我只是在_nayberz字段上进行匹配查询。
搜索效果很好,但是我想对ratedTags [n] .rating(可以为1-5)较高的比赛进行排名
对应:

{
"properties": {
"_nayberz": {
"type": "text",
"analyzer": "autocomplete",
"store" : true
},
"describedTagCombos": {
"type": "nested",
"properties": {
"tags": {
"type": "nested",
"properties": {
"keywords": {
"type": "text",
"analyzer": "autocomplete",
"boost": 5,
"copy_to": "_nayberz"
},
"name": {
"type": "text",
"analyzer": "autocomplete",
"boost": 5,
"copy_to": "_nayberz"
},
"synonym": {
"type": "text",
"analyzer": "autocomplete",
"boost": 5,
"copy_to": "_nayberz"
}
}
}
}
},
"name": {
"type": "text",
"fielddata" : true,
"copy_to": "_nayberz",
"boost": 1
},
"ratedTags": {
"type": "nested",
"properties": {
"tags": {
"type": "nested",
"properties": {
"keywords": {
"type": "text",
"boost": 5,
"copy_to": "_nayberz"
},
"name": {
"type": "text",
"boost": 5,
"copy_to": "_nayberz"
},
"synonym": {
"type": "text",
"boost": 5,
"copy_to": "_nayberz"
}
}
}
}
}
}
}



{
"id": 6475,
"neighborhoodId": 2495,
"name": "Some neighborhood name",
"xMin": -87.7351229157221,
"xMax": -87.687849915678,
"xAvg": -87.7114864157001,
"yMin": 41.9316410223988,
"yMax": 41.9466920224128,
"yAvg": 41.9391665224058,
"city": {
"id": 539,
"name": "SomeCity",
"county": "SomeCounty",
"state": {
"id": 174,
"name": "Illinois",
"abbreviation": "IL",
"country": "USA"
}
},
"ratedTags": [
{
"id": 11572,
"rating": 2,
"tags": [
{
"id": 2323,
"tagId": 36,
"name": "shopping options",
"synonym": "",
"keywords": "shopping options, shopping, shop, shopper, shoppers, shops"
}
]
},
{
"id": 11418,
"rating": 3,
"tags": [
{
"id": 2292,
"tagId": 5,
"name": "public transportation options",
"synonym": "",
"keywords": "public transport, public transportation"
}
]
},
{
"id": 11434,
"rating": 4,
"tags": [
{
"id": 2295,
"tagId": 8,
"name": "quiet",
"synonym": "",
"keywords": "quiet, chill, peaceful, not noisy, not loud, not too noisy, not too loud, relaxed"
}
]
},
{
"id": 11458,
"rating": 3,
"tags": [
{
"id": 2300,
"tagId": 13,
"name": "expensive relative to other neighborhoods",
"synonym": "costly",
"keywords": "upscale, chic"
}
]
},
{
"id": 11469,
"rating": 4,
"tags": [
{
"id": 2302,
"tagId": 15,
"name": "restaurants",
"synonym": "",
"keywords": "restaurants, restaurant, eatery, eateries"
}
]
},
{
"id": 11477,
"rating": 2,
"tags": [
{
"id": 2304,
"tagId": 17,
"name": "clean",
"synonym": "",
"keywords": "clean, cleanest, not dirty"
}
]
},
{
"id": 11603,
"rating": 3,
"tags": [
{
"id": 2329,
"tagId": 42,
"name": "safe compared to other neighborhoods",
"synonym": "",
"keywords": "safe, safety, safest, not dangerous"
}
]
},
{
"id": 11557,
"rating": 2,
"tags": [
{
"id": 2320,
"tagId": 33,
"name": "green space and parks",
"synonym": "",
"keywords": "green space, green, parks, open space, nature"
}
]
},
{
"id": 11577,
"rating": 2,
"tags": [
{
"id": 2324,
"tagId": 37,
"name": "tourist attractions",
"synonym": "sightseeing options",
"keywords": "tourist attractions, sightseeing, attractions, sightsee, tourist"
}
]
},
{
"id": 11582,
"rating": 2,
"tags": [
{
"id": 2325,
"tagId": 38,
"name": "entertainment options",
"synonym": "fun options",
"keywords": "fun, entertainment, good time, enjoyment, enjoy, pleasure"
}
]
},
{
"id": 11588,
"rating": 3,
"tags": [
{
"id": 2326,
"tagId": 39,
"name": "cafes",
"synonym": "coffee shops",
"keywords": "cafes, coffee shops, cafe, coffee shop, coffee houses, coffee house, café, cafés"
}
]
},
{
"id": 11594,
"rating": 4,
"tags": [
{
"id": 2327,
"tagId": 40,
"name": "dining options",
"synonym": "",
"keywords": "dining places, dining spots, dining options, restaurants, food options, food places, places to eat, eateries, dining, fast food, chain restaurant, chain restaurants, eatery"
}
]
}
],
"describedTagCombos": [
{
"id": 26842,
"descriptor": "Foodies: must try a polish sausage in this neighborhood",
"tags": [
{
"id": 2302,
"tagId": 15,
"name": "restaurants",
"synonym": "",
"keywords": "restaurants, restaurant, eatery, eateries"
},
{
"id": 2327,
"tagId": 40,
"name": "dining options",
"synonym": "",
"keywords": "dining places, dining spots, dining options, restaurants, food options, food places, places to eat, eateries, dining, fast food, chain restaurant, chain restaurants, eatery"
}
]
},
{
"id": 26843,
"descriptor": "Addison Mall in area with a Target, Starbucks, etc.",
"tags": [
{
"id": 2323,
"tagId": 36,
"name": "shopping options",
"synonym": "",
"keywords": "shopping options, shopping, shop, shopper, shoppers, shops"
}
]
}
]
}

设定:

{
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 3,
"max_gram": 20
},
"english_stop": {
"type": "stop",
"stopwords": [
"a",
"an",
"and",
"are",
"as",
"at",
"be",
"but",
"by",
"for",
"if",
"in",
"into",
"is",
"it",
"no",
"not",
"of",
"on",
"or",
"such",
"that",
"the",
"their",
"then",
"there",
"these",
"they",
"this",
"to",
"was",
"will",
"with",
"have"
]
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter",
"english_stop"
]
}
}
}
}

最佳答案

我认为最好的候选人是 field_value_factor function:

GET neighborhoods/_search
{
"query": {
"bool": {
"should": [
{
"query_string": {
"default_field": "_nayberz",
"query": "enjoy"
}
},
{
"nested": {
"path": "ratedTags",
"query": {
"function_score": {
"functions": [
{
"field_value_factor": { <--
"field": "ratedTags.rating",
"factor": 1
}
}
],
"boost_mode": "sum"
}
}
}
}
]
}
}
}

关于elasticsearch - 根据嵌套数组中的字段对结果进行排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64376434/

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