Reputation: 10502
How to index and search multiple value under single field.
e.g. say i have a field processor which might have i3,i5,i7 or i3 or i3,i5 values. now imagine a laptop data as below:
data1:
name= laptop name
price = laptop price
processor=core duo
data2:
name= laptop name
price = laptop price
processor=i3,i5
data3:
name= laptop name
price = laptop price
processor=i3,i5,i7
Now,
if a user want to search only i3 and i5 processor it should show data2 & data3 only.
So my question is how should i index and search the lucene. I am using lucene 4.4.
I checked this but could not understand as no example was there. An example will be good for me.
Upvotes: 4
Views: 8005
Reputation: 33351
Frankly, there isn't really much to it. Using using StandardAnalyzer
and the standard QueryParser
, you would simply add the field to the document, in the form shown, like:
Document document = new Document();
document.add(new TextField("name", "laptop name"));
document.add(new TextField("processor", "i3,i5,i7"));
//Add other fields as needed...
//Assuming you've set up your writing to use StandardAnalyzer...
writer.addDocument(document);
StandardAnalyzer will tokenize on punctuation (and whitespace, etc), indexing the tokens "i3", "i5" and "i7" in the "processor" field, so when using just using the standard QueryParser
(see query parser syntax), the query:
processor:(i3 i5)
Will find any fields with either an "i3" or "i5" in the "processor" field
Upvotes: 7
Reputation: 9406
You can inspire by my source code: http://git.abclinuxu.cz/?p=abclinuxu.git;a=tree;f=src/cz/abclinuxu/utils/search;h=d825ec75da1b19ca0cd6065458fec771de174be9;hb=HEAD
MyDocument is POJO that constructs LuceneDocument. Important information is stored in Field, so it is searchable. My document type is similar to your processor type:
Field field = new Field(TYPE, type, Field.Store.YES, Field.Index.UN_TOKENIZED);
Each processor type shall be stored individually.
Upvotes: 0