具有辅助搜索顺序的弹性搜索自动完成功能

时间:2017-02-11 22:02:10

标签: elasticsearch autocomplete

我正在通过弹性搜索工作,并需要一些自动完成功能的帮助。

基本上,我有自动完成功能,但我想使用我的填充字段降序添加辅助排序顺序。但是,当我向我的查询插入一个排序参数时,它给我一个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);
    }
}

1 个答案:

答案 0 :(得分:0)

建议者查询不支持排序顺序。建议者的重点是速度,添加排序混合会减慢速度。

您可以在查询中添加weight以提高排名,但无法添加辅助排序索引。如果您正在寻找中学排序,我建议您使用search

编辑:查看索引编码,您可以为weight字段添加combined作为population字段。

`$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']));`