弹性搜索自动完成与辅助搜索顺序
我只是通过弹性搜索的方式工作,需要一些帮助与自动完成功能。弹性搜索自动完成与辅助搜索顺序
基本上,我有自动完成功能工作,但我想添加使用我的人口领域降序次要排序顺序。但是,当我插入一个排序参数给我的查询时,它给了我一个400错误信息。你能看看我的映射和查询,看看它们是否设置正确,并建议我需要做些什么来完成这项工作?
很多谢谢。
映射:
PUT /ff_search_locations2?pretty
{
"mappings": {
"1": {
"_all": {
"enabled": false
},
"properties": {
"city": {
"type": "text"
},
"county": {
"type": "text"
},
"region": {
"type": "text"
},
"country": {
"type": "text"
},
"url": {
"type": "text"
},
"combined": {
"type": "completion"
},
"city_lc": {
"type": "text"
},
"population": {
"type": "text"
},
"location": {
"type": "geo_point"
}
}
}
}
}
我的工作查询:
POST /ff_search_locations2/_suggest?pretty&pretty
{
"1": {
"prefix": "london",
"completion": {
"field": "combined"
}
}
}
我已经试过:
POST /ff_search_locations2/_suggest?pretty&pretty
{
"1": {
"prefix": "london",
"completion": {
"field": "combined"
}
},
"sort": { "population": { "order": "desc" }}
}
和返回的错误:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "suggester with name [population] not supported"
}
],
"type": "illegal_argument_exception",
"reason": "suggester with name [population] not supported"
},
"status": 400
}
我使用生成索引的PHP代码:
error_reporting(-1);
ini_set('display_errors', 'On');
$db = "my_db";
$link = mysqli_connect("localhost", "user", "pass", "$db") or die(mysql_error());
$result = mysqli_query($link, "SELECT * FROM search_locations6 ORDER BY Population DESC") or die(mysql_error());
while($row = mysqli_fetch_array($result))
{
$ii++;
$i++;
$data_array = array("city" => $row['city'],"county" => $row['county'],"region" => $row['region'],"country" => $row['country'],"url" => $row['url'],"combined" => $row['combined'],"city_lc" => $row['city_lc'],"population" => $row['Population'],"location" => array("lat" => $row['lat'],"lon" => $row['long']));
//doing it in chunks, don't feel like changing heap sizes... etc.
if($i < 10000)
{
$json_data = '{"index":{"_id":"'.$ii.'"}}';
$json_data .= "\n";
$json_data .= json_encode($data_array);
$json_data .= "\n";
file_put_contents('bulk.php', $json_data, FILE_APPEND);
}
else
{
exec('curl --fail --silent --show-error -XPOST \'localhost:9200/ff_search_locations2/1/_bulk?pretty&refresh\' --data-binary "@bulk.php"');
unlink('bulk.php');
unset($i);
}
}
排序顺序不Suggesters查询支持。建议者的重点在于速度,对混音的加入会减慢速度。
您可以将weight
添加到您的查询以提高排名,但不能添加辅助排序索引。如果你正在寻找一个辅助排序,我会建议你可以使用search
编辑:纵观索引代码,你可以为你combined
字段添加weight
为领域。
`$data_array = array("city" => $row['city'],"county" => $row['county'],"region" => $row['region'],"country" => $row['country'],"url" => $row['url'],"combined" => array("input" => $row['combined'], "weight" => $row['Population']),"city_lc" => $row['city_lc'],"population" => $row['Population'],"location" => array("lat" => $row['lat'],"lon" => $row['long']));`
它的工作!如果你可以更新你的答案来说一些像......在你的完成类型中添加你的人口数值,如下所示:'code' $ data_array = array(“city”=> $ row ['city'],“county “=> $ row ['country'],”url“=> $ row ['url'],”=“$ row ['county'],”region“=> $ row ['region'],'country'=> “combined”=> array(“input”=> $ row ['combined'],“weight”=> $ row ['Population']),“city_lc”=> $ row ['city_lc'],“population” => $ row ['Population'],“location”=> array(“lat”=> $ row ['lat'],“lon”=> $ row ['long']));'code' then I将它标记为正确的答案。 –
感谢您提供索引代码。更新了我的答案。 – NutcaseDeveloper
参考我应该早发现[链接](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html) –