【问题标题】:Elasticsearch phrase/ multi-worlds searchElasticsearch 短语/多世界搜索
【发布时间】:2021-08-19 09:26:19
【问题描述】:

我正在尝试实现一个用例,其中用户可以对“product_name”字段进行多词/短语搜索。

假设有人在搜索“最富有的人”,应该会出现以下结果:

  • 巴比伦首富
  • 首富的故事
  • 世界首富
  • 如何赢得首富

搜索结果不得包含以下文档-

  • 最富有的女人和男人
  • 当一个穷人成为最富有的人

这是我写的分析器:

'settings' => [
    'analysis' => [
        'filter' => [
            'autocomplete_filter' => ['type' => 'ngram', 'min_gram' => 1, 'max_gram' => 10]
        ],
        'analyzer' => [
            'autocomplete' => ['type' => 'custom', 'tokenizer' => 'standard', 'filter' => ['lowercase', 'autocomplete_filter']]
        ]
    ],
    'index.max_ngram_diff' => 10
],
'mappings' => ['properties' => [
    'product_name' => ['type' => 'text', 'analyzer' => 'autocomplete', 'search_analyzer', 'standard'],                  
]],

我正在使用以下用 PHP 编写的代码来发出搜索请求:

$params = [
    'index' => ProductData::ELASTIC_INDEX,
    'type' => ProductData::ELASTIC_TYPE,
    'body' => [
        'query' => ['match' => ['product_name' => ['query' => $requestVars['product_name']]]
    ],
    ]
];

$result =  $this->client->search($params);

但是,我得到的结果是出乎意料的,而不是我上面列出的结果。

【问题讨论】:

    标签: php elasticsearch search


    【解决方案1】:

    由于您定义了 autocomplete 分析器,其中包含一个 n-gram 标记器,因此为 "When a poor man becomes the richest" 生成的标记将包括 "when""a""poor""man"、@ 987654328@, "the", "richest".

    现在,当您搜索 Richest Man 时,这将返回其文档中包含 richestman 的所有匹配文档

    您无需定义任何单独的分析器,只需使用match_phrase query

    添加一个工作示例

    索引映射:

    {
      "mappings": {
        "properties": {
          "name": {
            "type": "text"
          }
        }
      }
    }
    

    搜索查询:

    {
      "query": {
        "match_phrase": {
          "name": "Richest Man"
        }
      }
    }
    

    搜索结果将是

    "hits": [
          {
            "_index": "67784465",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.15394104,
            "_source": {
              "name": "The Richest Man in Babylon"
            }
          },
          {
            "_index": "67784465",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.15394104,
            "_source": {
              "name": "Story of the Richest man"
            }
          },
          {
            "_index": "67784465",
            "_type": "_doc",
            "_id": "3",
            "_score": 0.14290144,
            "_source": {
              "name": "The richest man in the world"
            }
          },
          {
            "_index": "67784465",
            "_type": "_doc",
            "_id": "4",
            "_score": 0.14290144,
            "_source": {
              "name": "How to win the richest man"
            }
          }
        ]
    

    【讨论】:

    • 感谢@ESCoder。这解决了我发布的原始问题。我应该提供更多细节。如果我搜索“最富有的人”或“最富有的人”,我应该得到这 4 个结果。如果我搜索“richest”、“ches”或“ma”,我应该会看到所有结果,即 6 个文档。
    猜你喜欢
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    相关资源
    最近更新 更多