有没有办法将数组存储为文档字段然后查询该数组?
我有一组物品,这些物品都被标记了。我希望能够搜索所有匹配的项目,例如标签 55 和 67。
我将如何实现这一目标?
有没有办法将数组存储为文档字段然后查询该数组?
我有一组物品,这些物品都被标记了。我希望能够搜索所有匹配的项目,例如标签 55 和 67。
我将如何实现这一目标?
首先,您必须使用数组中的数据创建索引文件。该文档介绍了如何创建新索引。
所以想象你的数组看起来像
$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
这可能是一个例子