2

我正在测试 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();

如果我注释掉过滤器,我会得到预期的结果。我错过了什么?它与分析器或映射有关吗?

4

1 回答 1

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);

我认为您的位置在空格和逗号上被标记,所以它正在扼杀搜索。应该为你解决。

于 2013-05-10T11:31:23.173 回答