Reputation: 9842
I am using php's client library for elasticsearch. I'd like to create an index that indexes a person's id
and his name
, and allows the user to search for names in a very flexible way (case insensitive, search for partial names, etc.
Here is a code snippet of what I have so far, annotated with comments for convenience
<?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);
I have had this search feature working at times, but I've been fussing with the script to get the case insensitive feature working as expected, and in the process, the script now fails to find any person with a J
or j
used as the query value to match.
Any ideas what might be going on here?
Upvotes: 1
Views: 1809
Reputation: 9842
To fix the case insensitive bit, I added
'filter' => 'lowercase',
to my ngram analyzer.
Also, the reason why it was failing to begin with is that, while using php's client library, you can't create the index then search it in the same script. My guess is something async is going on here. So create the index in one script and search it in another script, it should work.
Upvotes: 1