Lucene: how to index and search multiple values ​​in one field

How to index and search for multiple values ​​in one field.

eg. let's say I have a processor field that can have values i3, i5, i7 or i3 or i3, i5 . Now imagine the laptop data as shown 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 the user wants to search only the i3 and i5 processor, he should show only data2 and data3.

So my question is: how should I index and search for lucene. I am using lucene 4.4.

I checked this one , but could not understand, since not a single example was there. For me this will be a good example.

+3
source share
2 answers

Honestly, this is actually not so much. Using StandardAnalyzer and standard QueryParser , you would simply add a field to the document in the form shown, for example:

 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 mark punctuation (and spaces, etc.) by indexing the β€œi3”, β€œi5” and β€œi7” tokens in the β€œprocessor” field, so when used only using the standard QueryParser (see syntax analysis syntax ), the query :

 processor:(i3 i5) 

Find any fields with "i3" or "i5" in the "processor" field

+4
source

You can inspire with your source code: http://git.abclinuxu.cz/?p=abclinuxu.git;a=tree;f=src/cz/abclinuxu/utils/search;h=d825ec75da1b19ca0cd6065458fec771de174be9;hb=HEAD

MyDocument is a POJO that creates LuceneDocument. Important information is stored in the 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 type of processor must be stored separately.

0
source

All Articles