2013-05-08 70 views
2

我正在测试弹性和弹性搜索。我正在尝试为我的查询添加一个过滤器,该过滤器只返回城市位置的结果。它正在返回空。我试着按用户名过滤,等等,它总是返回空的,所以看起来我的理解并不完全正确。这里是我的代码进行分析,地图,然后用过滤弹性过滤器不工作

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

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

回答

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

我觉得你的位置被符号化的空间和逗号因此被杀害搜索。应该为你解决它。

+0

你能提供更多的细节吗?比如你改变了你的分析仪,你改变了什么? – landland 2013-05-11 20:16:48

+0

我用了2自定义分析仪1电子邮件,标记者:uax_url_email,另一个用于词(如你的位置的情况下),分词器:标准,通过试验和错误做了一些东西,因为没有大量的文档资料...如果告诉我你需要帮助 – CodeBird 2013-05-11 22:04:23

+0

是的,请帮忙。我在这里旋转我的轮子无处可去。文档非常稀少。我试图使用tokenizer:标准的位置,并已重建我的索引,但它仍然返回空。 – landland 2013-05-12 16:21:43