我正在测试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();
如果我注释掉过滤器,我会得到预期的结果。我错过了什么?它与分析仪或映射有关吗?
答案 0 :(得分:2)
我有同样的问题,我通过更改映射中这些字段的分析器来修复它。现在我必须重建我的索引等...不要试图找到改变映射或分析器的解决方案而不重建你的索引,你不能。
lc_analyzer' => array(
'type' => 'custom',
'tokenizer' => 'keyword',
'filter' => array('lowercase')
),
用于制图:
'location' => array('type' => 'string', 'analyzer' => 'lc_analyzer'),
然后,查询如
$elasticaFilterBool = new \Elastica\Filter\Bool();
$filter1 = new \Elastica\Filter\Term();
$filter1->setTerm('location', array(strtolower($region)));
$elasticaFilterBool->addMust($filter1);
$elasticaQuery->setFilter($elasticaFilterBool);
我认为您的位置在空格和逗号上被标记化,因此它会杀死搜索。应该为你解决。