Zend_Search_Lucene 在数组中搜索

发布于 2024-08-10 14:31:06 字数 102 浏览 4 评论 0原文

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

我收集了一些带有标签的物品。我希望能够搜索与标签 55 和 67 等匹配的所有项目。

我将如何实现这一目标?

Is there a way to store an array as a document field and then query that array?

I've got a collection of items, which are tagged. I'd like to be able to search all items that match for example tags 55 and 67.

How would I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2024-08-17 14:31:06

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

所以想象你的数组看起来

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

查询 你的索引文件

$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 是如何工作的,这可以是一个例子

First you have to create index file with the data in your array. The documentation covers how to create a new index.

so imagining your array look like

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

it would give something like that to create your index

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

finally to query your index file

$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;
}

Note

I am not really sure why you intend to use Lucene to do such work, if you just want a listing of article matching whatever tag would be more easy to do it with plain SQL queries.

After if want to know how Zend_Search_Lucene works that could be an example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文