2017-02-11 116 views
0

我只是通過彈性搜索的方式工作,需要一些幫助與自動完成功能。彈性搜索自動完成與輔助搜索順序

基本上,我有自動完成功能工作,但我想添加使用我的人口領域降序次要排序順序。但是,當我插入一個排序參數給我的查詢時,它給了我一個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); 
    } 
} 
+0

參考我應該早發現[鏈接](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html) –

回答

0

排序順序不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']));` 
+0

它的工作!如果你可以更新你的答案來說一些像......在你的完成類型中添加你的人口數值,如下所示:'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將它標記爲正確的答案。 –

+0

感謝您提供索引代碼。更新了我的答案。 – NutcaseDeveloper