soulmerge
soulmerge

Reputation: 75704

Match whole field in Lucene

I'm currently indexing a database with lucene. I was thinking of storing the table id in the index, but I can't find a way to retrieve the documents by that field. I guess some pseudo-code will further clarify the question:

document.add("_id", 7, Field.Index.UN_TOKENIZED, Field.Store.YES);
// How can I query the document with _id=7
// without getting the document with _id=17 or _id=71?

Upvotes: 1

Views: 393

Answers (2)

Richard Fila
Richard Fila

Reputation: 31

Just to say I've just implemented this successfully on my Zend Lucene search engine. However, after some time troubleshooting I discovered that the field name and field value are the opposite way around to the way shown. To correct the example:

// Fine - no change here
$doc->addField(Zend_Search_Lucene_Field::Keyword('_id', '7'));

// Reversed order of parameters
$idTerm  = new Zend_Search_Lucene_Index_Term('7', '_id',);
$idQuery = new Zend_Search_Lucene_Search_Query_Term($idTerm);

I hope that helps someone!

Upvotes: 1

Yuval F
Yuval F

Reputation: 20621

EDIT for Zend Lucene: You will need a Keyword type field in order for it to be searched. For indexing, use something like:

$doc->addField(Zend_Search_Lucene_Field::Keyword('_id', '7'));

For search, use:

$idTerm  = new Zend_Search_Lucene_Index_Term('_id', '7');
$idQuery = new Zend_Search_Lucene_Search_Query_Term($idTerm);

Upvotes: 1

Related Questions