Elasticsearch的确切结果不会在第一个

问题描述

添加了一些产品,例如“ lcd苹果iphone 11”“ lcd苹果iphone x”“ lcd苹果iphone xs”“ lcd苹果iphone xr”“ lcd三星s8”“ lcd三星s8 +”“ lcd苹果iphone xs max” “ lcd苹果iphone xr电池”我们最近才添加了iphone xr产品

我已经创建了Elasticsearch索引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);

解决方法

您可以将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","_id": "4","_score": 1.7120029,"_source": {
                "title": "lcd apple iphone xr"
            }
        },"_id": "1","_score": 0.30396554,"_source": {
                "title": "lcd apple iphone 11"
            }
        },"_id": "2","_source": {
                "title": "lcd apple iphone x"
            }
        },"_id": "3","_source": {
                "title": "lcd apple iphone xs"
            }
        }
    ]

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

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

虽然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","_id": "6","_score": 1.8347404,// note score is higher than other results.
                "_source": {
                    "title": "iphone xr"
                }
            },{
                "_index": "64129903","_score": 1.8268716,"_source": {
                    "title": "lcd apple iphone xr"
                }
            },"_score": 0.54542315,"_source": {
                    "title": "lcd apple iphone 11"
                }
            },"_source": {
                    "title": "lcd apple iphone x"
                }
            },"_source": {
                    "title": "lcd apple iphone xs"
                }
            }
        ]