elasticsearch精确结果不返回第一个

ttygqcqt  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(2)|浏览(396)

我添加了一些产品,如“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产品
我已经创建了elasticsearch索引 products_idx1 和类型 product .
当我搜索产品时 apple iphone xr 所以它返回iphonexr,但不在最前面。
我想要的是准确的结果应该是第一,然后部分结果应该是在准确的结果之后。我想要一个排序结果的基础上的准确性的结果。
这是我在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);
h43kikqp

h43kikqp1#

虽然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"
}

使用附加simpler match子句的搜索查询

{
    "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"
                }
            }
        ]
0qx6xfy6

0qx6xfy62#

可以将bool查询与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"
                    }
                }
            ]
        }
    }
}

相关问题