1

我有一个带有以下数据的对象

String key  
String name  
List<String> address

我将如何为上述对象创建一个 lucene 文档我必须搜索名称和地址,我已经创建了名称索引,但我也想
为 ex 创建地址索引

key:1 name:sam address:lane no 1 behind la gardens  
key:1 name:sam address:near abc cross main road

我应该如何创建和存储索引我必须存储两个具有通用名称和密钥的文档

4

2 回答 2

0

If I understand correctly, you want an arbitrary number of related addresses, retrievable separately, but searchable simply as:

address:"my address"

I would recommend storing addresses named whatever you like (I will use 'address1' and 'address2' here), and accumulating their text into one big old 'address' field. Something like:

doc.add(new Field("key", "1", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("name", "sam", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("address", "lane no 1 behind la gardens near abc cross main road", Field.Store.NO, Field.Index.ANALYZED));
//Note, that you can add multiple fields with the same name, and it will effectively be merged together, as:
doc.add(new Field("address", "more address information for searching", Field.Store.NO, Field.Index.ANALYZED));
doc.add(new Field("address1", "lane no 1 behind la gardens", Field.Store.YES, Field.Index.NO));
doc.add(new Field("address2", "near abc cross main road", Field.Store.YES, Field.Index.NO));

Note that address is passed Field.Store.NO and Field.Index.ANALYZED, while address1 and address2 are passed Field.Store.YES and Field.Index.NO. So you search on 'address', but never on 'address1' or 'address2', and you retrieve 'address1' and 'address2' from found documents, but never 'address'

于 2012-12-10T21:34:15.017 回答
0
Document temp = new Document();
temp.add(new Field("name", "sam", Field.Store.YES, Field.Index.ANALYZED));
temp.add(new Field("address", "lane no 1 behind la gardens", Field.Store.YES,
                   Field.Index.ANALYZED));
indexWriter.addDocument(temp);

搜索喜欢

name:sam address:gardens

将导致匹配上述文档

于 2012-12-10T18:39:33.117 回答