忽略 Elasticsearch 上的 ascii 字符

Ignore ascii characters on Elasticsearch

如何忽略 Elasticsearch 上的 ascii 字符? 我读过 http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-asciifolding-tokenfilter.html 但现在真的不知道如何执行它。

我正在使用 PHP 包。

public function createIndex()
{
    $indexParams['index'] = $this->data['index'];

    $mapping = [
        '_source'    => [
            'enabled' => true
        ],
        'properties' => [
            'history.name'  => [
                'type'  => 'string',
                '_boost' => 0.2
            ]
        ]
    ];
    $settings = [
        "analysis" => [
            "analyzer" => [
                "default" => [
                    "tokenizer" => "standard",
                    "filter" => ["standard", "asciifolding"]
                ]
            ]
        ]
    ];
    $indexParams['body']['mappings'][$this->data['type']] = $mapping;
    $indexParams['body']['settings'][$this->data['type']] = $settings;

    $this->es->client->indices()->create($indexParams);
}

但这仍然没有忽略重音字符。

谢谢,

我想知道您的 PHP 脚本是否正确(我不是 PHP 开发人员)。我可能会被写成:

$indexParams['body']['settings'][$this->data['index']] = $settings;

一些更正和建议:

  • 我更喜欢设置显式分析器,而不是更改默认值。未来的惊喜越来越少。所以在你的例子中,我明确地设置了 analyzer: ascii_folding
  • 然后我将分析器名称从 default 更改为 ascii_folding
  • 最后,设置是针对每个索引的,而不是针对每个类型的。 JSON结构是:

    {
      "settings" : {
        "analysis" : {}
      },
      "mappings" : {
        "my_type" : {}
      }
    }
    

编辑:用一段经过测试的有效代码替换了旧示例。硬编码了一些值(索引、类型等),但其他方面相同。它 returns 文档很成功...您的查询肯定有其他问题。

$indexParams['index'] = 'test';
$mapping = [
    '_source'    => [
        'enabled' => true
    ],
    'properties' => [
        'history.name'  => [
            'type'  => 'string',
            '_boost' => 0.2,
            'analyzer' => 'ascii_folding'
        ]
    ]
];
$settings = [
    "analysis" => [
        "analyzer" => [
            "ascii_folding" => [
                "tokenizer" => "standard",
                "filter" => ["standard", "asciifolding"]
            ]
        ]
    ]
];
$indexParams['body']['mappings']['test'] = $mapping;
$indexParams['body']['settings'] = $settings;

// create index and wait for yellow
$client->indices()->create($indexParams);
$client->cluster()->health(['wait_for_status' => 'yellow']);


//Index your document, refresh to make it visible
$params = [
    'index' => 'test',
    'type' => 'test',
    'id' => 1,
    'body' => [
        'history.name' => 'Nicôlàs Wîdàrt'
    ]
];
$client->index($params);
$client->indices()->refresh();

// Now search for it
$params = [
    'index' => 'test',
    'type' => 'test',
    'body' => [
        'query' => [
            'match' => [
                'history.name' => 'Nicolas'
            ]
        ]
    ]
];
$results = $client->search($params);
print_r($results);

返回单个文档作为值:

Array
(
    [took] => 3
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )
    [hits] => Array
        (
            [total] => 1
            [max_score] => 0.19178301
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => test
                            [_type] => test
                            [_id] => 1
                            [_score] => 0.19178301
                            [_source] => Array
                                (
                                    [history.name] => Nicôlàs Wîdàrt
                                )
                        )
                )
        )
)