douhui5529 2009-08-08 01:41 采纳率: 100%
浏览 59
已采纳

Zend_Search_Lucene帮助

EDIT:

I have managed to solve the problem by using:

+"lorem ipsum" +type:photo
+"lorem ipsum" +type:video

Another problem though is that the index is returning correct results but with wrong id (id is a primary key). More specifically, id fields returned are 1 less than real ids (id - 1) in the database which I use to build the index.

That's very strange.


What's wrong with these search queries:

"lorem ipsum" AND +type:photo
"lorem ipsum" AND +type:video

First query is supposed to find only results with type = photo, second one searches only videos. But they are both returning both photos and videos.

Here is how I build the index:

    // create media index
    $index = Zend_Search_Lucene::create('/data/media_index');
    // get all media
    $media = $this->_getTable('Media')->get();
    // iterate through media and build index
    foreach ($media as $m) {

        $doc = new Zend_Search_Lucene_Document();

        $doc->addField(Zend_Search_Lucene_Field::UnIndexed('id',
                                                           $m->id));
        $doc->addField(Zend_Search_Lucene_Field::UnIndexed('thumb_path',
                                                           $m->thumb_path));
        $doc->addField(Zend_Search_Lucene_Field::Keyword('title',
                                                         $m->title));
        $doc->addField(Zend_Search_Lucene_Field::UnStored('description',
                                                          $m->description));
        $doc->addField(Zend_Search_Lucene_Field::Keyword('type',
                                                         $m->type));

        $index->addDocument($doc);

    }
    // commit the index
    $index->commit();

And here is how I search it:

    $index = Zend_Search_Lucene::open('/data/media_index');
    $this->view->photos = $index->find('"lorem ipsum" AND +type:photo');
    $this->view->videos = $index->find('"lorem ipsum" AND +type:video');

Any ideas?

  • 写回答

3条回答 默认 最新

  • douhui7136 2009-08-08 03:43
    关注

    I just ran some tests on my own search index, and the problem seems to be in the query itself and not the code. The "AND" in the query is an operator, and so is the "+". The query parser seems to be confused by the double operator logic with no term between. This was a block quote I found in their docs:

    If the AND/OR/NOT style is used, then an AND or OR operator must be present between all query terms. Each term may also be preceded by NOT operator. The AND operator has higher precedence than the OR operator. This differs from Java Lucene behavior.

    Now, running your query through the parser, this was the Search_Query object:

    string '"lorem ipsum" AND +type:photo' (length=29)
    
    object(Zend_Search_Lucene_Search_Query_MultiTerm)[230]
      private '_terms' => 
        array
          0 => 
            object(Zend_Search_Lucene_Index_Term)[236]
              public 'field' => null
              public 'text' => string 'lorem' (length=5)
          1 => 
            object(Zend_Search_Lucene_Index_Term)[237]
              public 'field' => null
              public 'text' => string 'ipsum' (length=5)
          2 => 
            object(Zend_Search_Lucene_Index_Term)[238]
              public 'field' => null
              public 'text' => string 'and' (length=3)
          3 => 
            object(Zend_Search_Lucene_Index_Term)[239]
              public 'field' => null
              public 'text' => string 'type' (length=4)
          4 => 
            object(Zend_Search_Lucene_Index_Term)[240]
              public 'field' => null
              public 'text' => string 'photo' (length=5)
    

    Changing the query up a bit, removing the AND or removing the +, and only using 1.

    string '"lorem ipsum" +type:photo' (length=25)
    string '"lorem ipsum" AND type:photo' (length=28)
    
    object(Zend_Search_Lucene_Search_Query_Boolean)[227]
      private '_subqueries' => 
        array
          0 => 
            object(Zend_Search_Lucene_Search_Query_Phrase)[230]
              private '_terms' => 
                array
                  0 => 
                    object(Zend_Search_Lucene_Index_Term)[233]
                      public 'field' => null
                      public 'text' => string 'lorem' (length=5)
                  1 => 
                    object(Zend_Search_Lucene_Index_Term)[234]
                      public 'field' => null
                      public 'text' => string 'ipsum' (length=5)
          1 => 
            object(Zend_Search_Lucene_Search_Query_Term)[235]
              private '_term' => 
                object(Zend_Search_Lucene_Index_Term)[232]
                  public 'field' => string 'type' (length=4)
                  public 'text' => string 'photo' (length=5)
    

    The only difference: AND:

      private '_signs' => 
        array
          0 => boolean true
          1 => boolean true
    

    +:

      private '_signs' => 
        array
          0 => null
          1 => boolean true
    

    The AND operator requires that both of the search queries are required in the result, where as the + only requires the value on the right be required.

    So just change up the query to

    "lorem ipsum" AND type:photo

    And you should get the results you are looking for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题