同义词,在文档中存储权重,用于弹性搜索中的相关性评分

时间:2015-12-09 17:35:55

标签: search elasticsearch synonym

故事:鉴于下面的示例文档并通过扩展它们,是否可以获得以下排名:

  • 搜索"谷物"得到以下排名

    1. 玉米片
    2. Rice Krispies
  • 搜索" Rice"得到以下排名

    1. 巴斯马蒂
    2. Rice Krispies

执行反对搜索的文件:

[{
  name: "Cornflakes"
},
{
  name: "Basmati"
},
{
  name: "Rice Krispies"
}]

当然,其中一些甚至没有保留搜索词,所以一个选项是添加一个带有文本值和权重的同义词数组,这将有助于计算排名:

[{
  name: "Cornflakes",
  synonyms: [
    {t: 'Cereals', weight: 100},
    {t: 'Sugar', weight: 100}]
},
{
  name: "Basmati",
  synonyms: [
    {t: 'Cereals', weight: 1},
    {t: 'Rice', weight: 1000}]
},
{
  name: "Rice Krispies",
  synonyms: [
    {t: 'Cereals', weight: 10},
    {t: 'Rice', weight: 1}]
}]

这是正确的方法吗?

考虑加权同义词的弹性搜索查询是什么?

1 个答案:

答案 0 :(得分:3)

我认为“标签”对于字段来说比“同义词”更合适。 您可以使用nested type来存储代码,并使用function score tags.weight 字段(最佳匹配代码,如果有的话)的值与匹配分数相结合名称字段。

其中一个实现可能如下所示:

"hits": {
      "total": 3,
      "max_score": 100,
      "hits": [
         {
            "_index": "test",
            "_type": "tag_doc",
            "_id": "1",
            "_score": 100,
            "_source": {
               "name": "Cornflakes",
               "tags": [
                  {
                     "t": "Cereals",
                     "weight": 100
                  },
                  {
                     "t": "Sugar",
                     "weight": 100
                  }
               ]
            }
         },
         {
            "_index": "test",
            "_type": "tag_doc",
            "_id": "3",
            "_score": 10,
            "_source": {
               "name": "Rice Krispies",
               "tags": [
                  {
                     "t": "Cereals",
                     "weight": 10
                  },
                  {
                     "t": "Rice",
                     "weight": 1
                  }
               ]
            }
         },
         {
            "_index": "test",
            "_type": "tag_doc",
            "_id": "2",
            "_score": 1,
            "_source": {
               "name": "Basmati",
               "tags": [
                  {
                     "t": "Cereals",
                     "weight": 1
                  },
                  {
                     "t": "Rice",
                     "weight": 1000
                  }
               ]
            }
         }
      ]
   }

结果

java.lang.NullPointerException: Attempt to invoke virtual method (android.view.View$OnTouchListener)' on a null object reference.