【问题标题】:Zend_Search_Lucene search in arrayZend_Search_Lucene 在数组中搜索
【发布时间】:2009-11-10 09:34:03
【问题描述】:

有没有办法将数组存储为文档字段,然后查询该数组?

我有一组已标记的项目。我希望能够搜索所有匹配的项目,例如标签 55 和 67。

我将如何实现这一目标?

【问题讨论】:

    标签: zend-framework zend-search-lucene


    【解决方案1】:

    首先,您必须使用数组中的数据创建索引文件。该文档介绍了如何创建new index

    所以想象你的数组看起来像

    $data = array(
      array(
         'tag' => '55 67',
         'content' => 'Lorem Ipsu 1',
         'url' => 'http://foobar.net/content.php?id=1'
      ),
      array(
         'tag' => '32 67'
         'content' => 'Lorem Ipsu 2',
         'url' => 'http://foobar.net/content.php?id=2'
      )
    );
    

    它会提供类似的东西来创建你的索引

     // Create index
    $index = Zend_Search_Lucene::create('/data/my-index');
    
    
    foreach($data as $row){
      $doc = new Zend_Search_Lucene_Document();
      // Store document URL to identify it in the search results
      $doc->addField(Zend_Search_Lucene_Field::Text('url', $row['url']));
    
      // Store document TAGS 
      $doc->addField(Zend_Search_Lucene_Field::Text('tag', $row['tag']));
    
      // Index document contents
      $doc->addField(Zend_Search_Lucene_Field::UnStored('contents', $row['content']));
    
      // Add document to the index
      $index->addDocument($doc);
     }
    

    终于query你的索引文件

    $index = Zend_Search_Lucene::open('/data/my_index');
    
    $hits = $index->find('tag:55 AND tag:67');
    
    foreach ($hits as $hit) {
        echo $hit->score;
        echo $hit->url;
        echo $hit->tag;
    }
    

    注意

    我不确定你为什么打算使用 Lucene 来做这样的工作,如果你只是想要一个匹配任何标签的文章列表,用普通的SQL 查询更容易做到这一点。

    如果想知道Zend_Search_Lucene 是如何工作的,这可能是一个例子

    【讨论】:

    • 完全不相关。我在问是否可以将实际数组存储为文档字段,然后搜索该字段。但无论如何,感谢您的长回答。
    • @Apikot:哈哈,完全可以,据我所知,您不能在索引中存储数组。
    • 上面的解决方案对我也不起作用。我没有得到任何结果,Zend 和 Luke 都没有。
    猜你喜欢
    • 2011-02-06
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2013-04-15
    • 2020-02-01
    相关资源
    最近更新 更多