繁体   English   中英

如何搜索不区分大小写的elasticsearch

[英]How to search elasticsearch case insensitive

我正在使用php的客户端库进行Elasticsearch。 我想创建一个索引,以索引一个人的id和他的name ,并允许用户以一种非常灵活的方式搜索名称(不区分大小写,搜索部分名称等)。

这是我到目前为止的代码片段,并带有注释,以方便使用

<?php

require_once(__DIR__ . '/../init.php');

$client = new Elasticsearch\Client();
$params = [
    'index' => 'person',
    'body' => [
        'settings' => [
            // Simple setings for now, single shard
            'number_of_shards' => 1,
            'number_of_replicas' => 0,
            'analysis' => [
                'filter' => [
                    'shingle' => [
                        'type' => 'shingle'
                    ]
                ],
                'analyzer' => [
                    'my_ngram_analyzer' => [
                        'tokenizer' => 'my_ngram_tokenizer',
                    ]
                ],
                // Allow searching for partial names with nGram
                'tokenizer' => [
                    'my_ngram_tokenizer' => [
                        'type' => 'nGram',
                        'min_gram' => 1,
                        'max_gram' => 15,
                        'token_chars' => ['letter', 'digit']
                    ]
                ]
            ]
        ],
        'mappings' => [
            '_default_' => [
                'properties' => [
                    'person_id' => [
                        'type' => 'string',
                        'index' => 'not_analyzed',
                    ],
                    // The name of the person
                    'value' => [
                        'type' => 'string',
                        'analyzer' => 'my_ngram_analyzer',
                        'term_vector' => 'yes',
                        'copy_to' => 'combined'
                    ],
                ]
            ],
        ]
    ]
];

// Create index `person` with ngram indexing
$client->indices()->create($params);

// Index a single person using this indexing scheme
$params = array();
$params['body']  = array('person_id' => '1234', 'value' => 'Johnny Appleseed');
$params['index'] = 'person';
$params['type']  = 'type';
$params['id']    = 'id';
$ret = $client->index($params);

// Get that document (to prove it's in there)
$getParams = array();
$getParams['index'] = 'person';
$getParams['type']  = 'type';
$getParams['id']    = 'id';
$retDoc = $client->get($getParams);
print_r($retDoc); // success


// Search for that document
$searchParams['index'] = 'person';
$searchParams['type']  = 'type';
$searchParams['body']['query']['match']['value'] = 'J';
$queryResponse = $client->search($searchParams);
print_r($queryResponse); // FAILURE

// blow away index so that we can run the script again immediately
$deleteParams = array();
$deleteParams['index'] = 'person';
$retDelete = $client->indices()->delete($deleteParams);

我有时会使用此搜索功能,但是我一直在忙于脚本以使不区分大小写的功能按预期工作,并且在此过程中,脚本现在无法找到任何将Jj用作查询要匹配的值。

任何想法可能在这里发生什么?

为了解决不区分大小写的问题,我添加了

'filter' => 'lowercase',

到我的ngram分析器。

同样,它之所以未能开始是因为,在使用php的客户端库时,您无法创建索引然后在同一脚本中对其进行搜索。 我的猜测是异步发生在这里。 因此,在一个脚本中创建索引,然后在另一个脚本中搜索索引,它应该可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM