首页 > 解决方案 > 根据嵌套数组中的字段对结果进行排名

问题描述

假设我有一个名为“neighborhood”的弹性搜索索引,并且我在其中存储具有以下设置和映射的文档(下面的示例)。

模型中有许多嵌套字段,因此我在其中许多字段上使用了 copy_to 以使搜索更容易 - 例如,我只是对 _nayberz 字段进行匹配查询。

搜索效果很好,但是我希望匹配的 ratingTags[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"
        ]
      }
    }
  }
}

标签: elasticsearch

解决方案


我认为这里最好的候选人是field_value_factor函数

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"
              }
            }
          }
        }
      ]
    }
  }
}

推荐阅读