【问题标题】:Elasticsearch exact result not return on a firstElasticsearch 的确切结果不会首先返回
【发布时间】:2020-09-30 02:02:00
【问题描述】:

我添加了一些产品,例如“lcd apple iphone 11”“lcd apple iphone x”“lcd apple iphone xs”“lcd apple iphone xr”“lcd samsung s8”“lcd samsung s8+”“lcd apple iphone xs max” 'lcd apple iphone xr 电池' 我们上次添加了 iphone xr 产品

我已创建弹性搜索索引products_idx1 并输入product

当我搜索 apple iphone xr 之类的产品时,它会返回 iphone xr 但不会排在首位。

我想要什么 确切的结果应该在前,部分结果应该在确切的结果之后。我想根据准确度结果对结果进行排序。

这是我在 php elasticsearch 中的代码

<?php

    use Elasticsearch\ClientBuilder;

    require 'vendor/autoload.php';

   $client = ClientBuilder::create()->build();
 $values =['name','name.prefix','name.suffix','sku'];
$params =
[
'client'=>['verify'=>1,'connect_timeout'=>5],
'from'=> 0,
'size'=>25,
 'body'  =>[
'query' => [
 'bool'=>
            [
            'should'=> [[
                'multi_match'=> ['query'=>'apple iphone xr','type'=>'cross_fields','fields'=>$values,'operator'=>'AND']
                ],
                ['match'=>['all'=>['query'=>'apple iphone xr','operator'=>'AND','fuzziness'=>'AUTO'] ]]
                ]
            ]

],
'sort'=>['_score'=>['order'=>'desc']],
],

'index'=>'products_idx1'
];

 $response = $client->search($params);
echo "<pre>";print_r($response);

【问题讨论】:

    标签: sorting elasticsearch resultset exact-match


    【解决方案1】:

    虽然 bhavya 的答案有效,但它使用 match_phrase 查询更复杂,根据您可能拥有的数据集,该查询更复杂且可能成本更高,但我创建了此查询的更简单版本,该查询有效。

    索引示例文档

    {
        "title" : "lcd apple iphone xr"
    }
    {
        "title" : "lcd apple iphone 11"
    }
    {
        "title" : "lcd apple iphone x"
    }
    {
        "title" : "lcd apple iphone xs"
    }
    {
        "title" : "iphone xr"
    }
    

    使用更简单的匹配子句的搜索查询

    {
        "query": {
            "bool": {
                "should": [
                    {
                        "match": {
                            "title" : "apple iphone xr"
                        }
                    },
                    {
                        "match": {    --> simple additional match clause solves issue.
                            "title": "iphone xr"
                        }
                    }
                ]
            }
        }
    }
    

    搜索结果在顶部显示iphone xr,分数更高

     "hits": [
                {
                    "_index": "64129903",
                    "_type": "_doc",
                    "_id": "6",
                    "_score": 1.8347404, // note score is higher than other results.
                    "_source": {
                        "title": "iphone xr"
                    }
                },
                {
                    "_index": "64129903",
                    "_type": "_doc",
                    "_id": "2",
                    "_score": 1.8268716,
                    "_source": {
                        "title": "lcd apple iphone xr"
                    }
                },
                {
                    "_index": "64129903",
                    "_type": "_doc",
                    "_id": "3",
                    "_score": 0.54542315,
                    "_source": {
                        "title": "lcd apple iphone 11"
                    }
                },
                {
                    "_index": "64129903",
                    "_type": "_doc",
                    "_id": "4",
                    "_score": 0.54542315,
                    "_source": {
                        "title": "lcd apple iphone x"
                    }
                },
                {
                    "_index": "64129903",
                    "_type": "_doc",
                    "_id": "5",
                    "_score": 0.54542315,
                    "_source": {
                        "title": "lcd apple iphone xs"
                    }
                }
            ]
    

    【讨论】:

      【解决方案2】:

      您可以将bool querymatch phrase query 结合使用,以分析文本并根据分析的文本创建短语查询。

      添加带有搜索查询和搜索结果的工作示例

      搜索查询:

      {
        "query": {
          "bool": {
            "should": [
              {
                "match": {
                  "title": "apple iphone xr"
                }
              },
              {
                "match_phrase":{
                  "title":"iphone xr"
                }
              }
            ]
          }
        }
      }
      

      搜索结果:

      "hits": [
              {
                  "_index": "test",
                  "_type": "_doc",
                  "_id": "5",
                  "_score": 1.8850331,
                  "_source": {
                      "title": "iphone xr"
                  }
              },
              {
                  "_index": "test",
                  "_type": "_doc",
                  "_id": "4",
                  "_score": 1.7120029,
                  "_source": {
                      "title": "lcd apple iphone xr"
                  }
              },
              {
                  "_index": "test",
                  "_type": "_doc",
                  "_id": "1",
                  "_score": 0.30396554,
                  "_source": {
                      "title": "lcd apple iphone 11"
                  }
              },
              {
                  "_index": "test",
                  "_type": "_doc",
                  "_id": "2",
                  "_score": 0.30396554,
                  "_source": {
                      "title": "lcd apple iphone x"
                  }
              },
              {
                  "_index": "test",
                  "_type": "_doc",
                  "_id": "3",
                  "_score": 0.30396554,
                  "_source": {
                      "title": "lcd apple iphone xs"
                  }
              }
          ]
      

      您还可以使用多重匹配来使用搜索查询

      {
          "query": {
              "bool": {
                  "should": [
                      {
                          "multi_match": {
                              "query": "apple iphone xr",
                              "fields": [
                                  "title"
                              ]
                          }
                      },
                      {
                          "match_phrase": {
                              "title": "iphone xr"
                          }
                      }
                  ]
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-28
        • 1970-01-01
        • 2014-09-13
        • 2015-02-15
        • 2013-06-29
        • 2023-04-09
        • 2017-05-08
        • 1970-01-01
        相关资源
        最近更新 更多