本文介绍了Lucene:如何在单个字段下索引和搜索多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在单个字段下索引和搜索多个值.

How to index and search multiple value under single field.

例如说我有一个字段处理器,该字段可能具有 i3,i5,i7 i3 i3,i5 值.现在想象一下笔记本电脑的数据,如下所示:

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:

数据1:

 name= laptop name
 price = laptop price
 processor=core duo

数据2:

 name= laptop name
 price = laptop price
 processor=i3,i5

数据3:

 name= laptop name
 price = laptop price
 processor=i3,i5,i7

现在

如果用户只想搜索i3和i5处理器,则应显示data2&仅限data3.

if a user want to search only i3 and i5 processor it should show data2 & data3 only.

所以我的问题是我应该如何索引搜索该lucene.我正在使用Lucene 4.4.

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.

推荐答案

坦白说,它并没有太多内容.使用StandardAnalyzer和标准的QueryParser,您只需将字段添加到文档中即可,格式如下所示:

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将对标点符号(以及空格等)进行标记化,并在处理器"字段中为标记"i3","i5"和"i7"建立索引,因此仅在使用标准QueryParser时使用(请参阅查询解析器语法 ),查询:

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)

将在处理器"字段中找到任何带有"i3"或"i5"的字段

Will find any fields with either an "i3" or "i5" in the "processor" field

这篇关于Lucene:如何在单个字段下索引和搜索多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 19:33