在Elasticsearch中使用ngram过滤器创建mutli字段autosuggest索引

时间:2016-12-02 06:56:09

标签: php elasticsearch autosuggest

我想在Elasticsearch中实现mutliple match autosuggest,我尝试用php客户端创建索引。但我得到了这个错误。

BadRequest400Exception: {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"analyzer on field [name] must be set when search_analyzer is set"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [hotel]: analyzer on field [name] must be set when search_analyzer is set","caused_by":{"type":"mapper_parsing_exception","reason":"analyzer on field [name] must be set when search_analyzer is set"}},"status":400}

    use Elasticsearch\ClientBuilder;
    require '../vendor/autoload.php';
    $client = ClientBuilder::create()->build();

    $params = [
        'index' => 'hotel_index',
        'body' => [
            'settings' => [
                'analysis' => [
                    'filter' => [
                        "ngram_filter" =>  [
                           "type" => "ngram",
                           "min_gram" => 2,
                           "max_gram" => 20
                        ]
                    ],
                    'analyzer' => [
                        "ngram_analyzer" => [
                           "type" => "custom",
                           "tokenizer" => "whitespace",
                           "filter" => [
                              "lowercase",
                              "asciifolding",
                              "ngram_filter"
                           ]
                        ]
                    ]
                ]
            ],
            'mappings' => [
                 "hotel" => [
                    'properties' => [
                        "name" => [
                            "type" => "string",
                            "include_in_all" => true,
                            "index_analyzer" => "ngram_analyzer",
                            "search_analyzer" => "standard"
                        ],
                        "city" => [
                            "type" => "string",
                            "include_in_all" => true,
                        ],
                        "country" => [
                            "type" => "string",
                            "include_in_all" => true,
                        ]
                    ],
                    // "boost" => 3,
                 ]
            ]
        ]
    ];

    $response = $client->indices()->create($params);
    print_r($response);

我的弹性搜索版本

"version" : {
    "number" : "2.4.1",
    "build_snapshot" : false,
    "lucene_version" : "5.5.2"
  },

0 个答案:

没有答案