Elasticsearch 准确结果不是 return 第一
Elasticsearch exact result not return on a first
我添加了一些产品,例如 '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 battery' 我们在最后添加了 iphone xr 产品所以
我已经创建了 elasticsearch 索引 products_idx1
并输入 product
.
当我搜索像 apple iphone xr
这样的产品时,它 returns 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 query with match 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"
}
}
]
}
}
}
虽然 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"
}
}
]
我添加了一些产品,例如 '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 battery' 我们在最后添加了 iphone xr 产品所以
我已经创建了 elasticsearch 索引 products_idx1
并输入 product
.
当我搜索像 apple iphone xr
这样的产品时,它 returns 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 query with match 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"
}
}
]
}
}
}
虽然 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"
}
}
]