Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting Elasticsearch精确结果在第一次搜索时不返回_Sorting_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Resultset_Exact Match - Fatal编程技术网 elasticsearch,resultset,exact-match,Sorting,elasticsearch,Resultset,Exact Match" /> elasticsearch,resultset,exact-match,Sorting,elasticsearch,Resultset,Exact Match" />

Sorting Elasticsearch精确结果在第一次搜索时不返回

Sorting Elasticsearch精确结果在第一次搜索时不返回,sorting,elasticsearch,resultset,exact-match,Sorting,elasticsearch,Resultset,Exact Match,我添加了一些产品,如“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\u idx1和类型product 当我搜索像苹果i

我添加了一些产品,如“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\u idx1
和类型
product

当我搜索像苹果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);
您可以使用with分析文本并从分析的文本中创建短语查询

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

搜索查询:

"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"
                    }
                }
            ]
        }
    }
}
搜索结果:

"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"
                    }
                }
            ]
        }
    }
}
您还可以使用多重匹配使用搜索查询

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

虽然bhavya的答案是可行的,但它更复杂,因为它使用了
match_短语
查询,根据您可能拥有的数据集,该查询更复杂,成本也可能更高,我已经创建了一个更简单的查询版本,它可以工作

索引样本文档

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "title" : "apple iphone xr"
                    }
                },
                {
                    "match": {    --> simple additional match clause solves issue.
                        "title": "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"
                }
            }
        ]
搜索结果显示顶部的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"
                }
            }
        ]