I am testing out Elastica and Elastic Search. I am trying to add a filter to my query that only returns results by city location. It is returning empty. I've tried filter by username, and so on and it always returns empty, so it would seem my understanding isn't quite correct. Here's my code to analyse, map, then search with a filter
$elasticaIndex = $elasticaClient->getIndex('users');
// Create the index new
$elasticaIndex->create(
array(
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('lowercase', 'lb_ngram')
),
'searchAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('standard', 'lowercase', 'lb_ngram')
)
),
'filter' => array(
'lb_ngram' => array(
"max_gram" => 10,
"min_gram" => 1,
"type" => "nGram"
)
)
)
), true
);
//Create a type
$elasticaType = $elasticaIndex->getType('profile');
// Set mapping
$mapping->setProperties(array(
//'id' => array('type' => 'integer', 'include_in_all' => FALSE),
'firstName' => array('type' => 'string', 'include_in_all' => TRUE),
'lastName' => array('type' => 'string', 'include_in_all' => TRUE),
'username' => array('type' => 'string', 'include_in_all' => TRUE),
'bio' => array('type' => 'string', 'include_in_all' => TRUE),
'thumbnail' => array('type' => 'string', 'include_in_all' => FALSE),
'location' => array('type' => 'string', 'include_in_all' => TRUE),
));
..... Then to search, I do the following
$elasticaQueryString = new Elastica\Query\QueryString();
//'And' or 'Or' default : 'Or'
$elasticaQueryString->setDefaultOperator('AND');
$elasticaQueryString->setQuery($term);
// Create the actual search object with some data.
$elasticaQuery = new Elastica\Query();
$elasticaQuery->setQuery($elasticaQueryString);
To add a filter
$elasticaFilterLocation = new \Elastica\Filter\Term();
//search 'location' = $region;
$elasticaFilterLocation->setTerm('location', $region);
$elasticaQuery->setFilter($elasticaFilterLocation);
$elasticaResultSet = $elasticaIndex->search($elasticaQuery);
$elasticaResults = $elasticaResultSet->getResults();
If I comment out the filter, I do get the expected results. What am I missing? Does it have to do with the analyzer or mapping?