我正在测试 Elastica 和 Elastic Search。我正在尝试向我的查询添加一个过滤器,该过滤器仅按城市位置返回结果。它返回空。我试过按用户名过滤,等等,它总是返回空,所以我的理解似乎不太正确。这是我的代码,用于分析、映射,然后使用过滤器进行搜索
$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),
));
.....然后进行搜索,我执行以下操作
$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);
添加过滤器
$elasticaFilterLocation = new \Elastica\Filter\Term();
//search 'location' = $region;
$elasticaFilterLocation->setTerm('location', $region);
$elasticaQuery->setFilter($elasticaFilterLocation);
$elasticaResultSet = $elasticaIndex->search($elasticaQuery);
$elasticaResults = $elasticaResultSet->getResults();
如果我注释掉过滤器,我会得到预期的结果。我错过了什么?它与分析器或映射有关吗?