【问题标题】:Getting the document with searched term first, before its synonyms [Elastic]在同义词之前首先获取带有搜索词的文档 [Elastic]
【发布时间】:2018-01-04 07:53:54
【问题描述】:

我想我应该用一个例子来解释我的问题:

假设我使用同义词分析器创建了索引,并声明“laptop”、“phone”和“tablet”是可以概括为“mobile”的相似词:

PUT synonym
{
  "settings": {
    "index": {
      "number_of_shards": 3,
      "number_of_replicas": 2,
      "analysis": {
        "analyzer": {
          "synonym": {
            "tokenizer": "whitespace",
            "filter": [
              "synonym"
            ]
          }
        },
        "filter": {
          "synonym": {
            "type": "synonym",
            "synonyms": [
              "phone, tablet, laptop => mobile"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "synonym" : {
            "properties" : {
                "field1" : { 
                  "type" : "text",
                  "analyzer": "synonym",
                  "search_analyzer": "synonym"
                }
            }
        }
  }
}

现在我正在创建一些文档:

PUT synonym/synonym/1
{
    "field1" : "phone"
}
PUT synonym/synonym/2
{
    "field1" : "tablet"
}
PUT synonym/synonym/3
{
    "field1" : "laptop"
}

现在当我匹配laptoptabletphone 的查询时,结果总是:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "synonym",
        "_type": "synonym",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "field1": "tablet"
        }
      },
      {
        "_index": "synonym",
        "_type": "synonym",
        "_id": "1",
        "_score": 0.18232156,
        "_source": {
          "field1": "phone"
        }
      },
      {
        "_index": "synonym",
        "_type": "synonym",
        "_id": "3",
        "_score": 0.18232156,
        "_source": {
          "field1": "laptop"
        }
      }
    ]
  }
}

你可以看到tablet的分数总是更高,即使我搜索laptop

我知道那是因为我将它们声明为相似的词。

但是,我试图弄清楚如何查询,以便带有搜索词的文档可以首先出现在结果列表中的相似词之前。

可以通过boosting来完成,但一定有更简单的方法..

【问题讨论】:

    标签: elasticsearch search elasticsearch-5 analyzer synonym


    【解决方案1】:

    Multi-fields 来救你。 以两种方式索引 field1,一种使用同义词分析器,另一种使用标准分析器。 现在您可以简单地使用 bool-should 查询来为 field1(同义词)和 field1.raw(标准)上的匹配添加分数。 所以,你的映射应该是这样的:

    PUT synonym
    {
      "settings": {
        "index": {
          "number_of_shards": 3,
          "number_of_replicas": 2,
          "analysis": {
            "analyzer": {
              "synonym": {
                "tokenizer": "whitespace",
                "filter": [
                  "synonym"
                ]
              }
            },
            "filter": {
              "synonym": {
                "type": "synonym",
                "synonyms": [
                  "phone, tablet, laptop => mobile"
                ]
              }
            }
          }
        }
      },
      "mappings": {
        "synonym": {
          "properties": {
            "field1": {
              "type": "text",
              "analyzer": "synonym",
              "search_analyzer": "synonym",
              "fields": {
                "raw": {
                  "type": "text",
                  "analyzer": "standard"
                }
              }
            }
          }
        }
      }
    }
    

    您可以使用以下方式查询:

    GET synonyms/_search?search_type=dfs_query_then_fetch
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "field1": "tablet"
              }
            },
            {
              "match": {
                "field1.raw": "tablet"
              }
            }
          ]
        }
      }
    }
    

    注意:我用过search_type=dfs_query_then_fetch。由于您在 3 个分片上进行测试并且文档很少,因此您获得的分数不是应有的分数。这是因为频率是按分片计算的。您可以在测试时使用dfs_query_then_fetch,但不鼓励用于生产。见:https://www.elastic.co/blog/understanding-query-then-fetch-vs-dfs-query-then-fetch

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 2023-04-04
      • 1970-01-01
      • 2012-11-03
      相关资源
      最近更新 更多