我有一个包含以下数据的对象
String key
String name
List<String> address
我如何为上面的对象创建lucene的文档我必须在名称和地址上搜索,我在名称上创建了索引但我也想在地址上创建索引
对于前
key:1 name:sam address:lane no 1 behind la gardens
key:1 name:sam address:near abc cross main road
如果我必须存储两个带有通用名称和密钥
的文档,我将如何创建和存储索引答案 0 :(得分: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
将导致匹配上述文件
答案 1 :(得分:0)
如果我理解正确,您需要任意数量的相关地址,可以单独检索,但可以简单地搜索到:
address:"my address"
我建议存储名为你喜欢的地址(我将在这里使用'address1'和'address2'),并将它们的文本累积到一个很大的旧“地址”字段中。类似的东西:
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));
请注意,地址传递Field.Store.NO
和Field.Index.ANALYZED
,而地址1和地址2传递Field.Store.YES
和Field.Index.NO
。所以你搜索'地址',但从不在'address1'或'address2',你从找到的文件中检索'address1'和'address2',但从不'地址'