简体   繁体   English

如何检索Lucene中“击中”的字段

[英]How to retrieve the Field that “hit” in Lucene

Maybe I'm really missing something. 也许我真的错过了什么。

I have indexed a bunch of key/value pairs in Lucene (v4.1 if it matters). 我已经在Lucene中索引了一堆键/值对(v4.1,如果重要的话)。 Say I have key1=value1 and key2=value2, eg as read from a properties file. 假设我有key1 = value1和key2 = value2,例如从属性文件中读取。

They get indexed both as specific fields and into a catchall "ALL" field, eg 它们被索引为特定字段并被编入一个笼统的“ALL”字段,例如

new Field("key1", "value1", aFieldTypeMimickingKeywords);
new Field("key2", "value2", aFieldTypeMimickingKeywords);
new Field("ALL", "key1=value1", aFieldTypeMimickingKeywords);
new Field("ALL", "key2=value2", aFieldTypeMimickingKeywords);
// then get added to the Document of course...

I can then do a wildcard search, using 然后,我可以使用通配符搜索

new WildcardQuery(new Term("ALL", "*alue1"));

and it will find the hit. 它会找到命中。

But, it would be nice to get more info, like "what was complete value (eg "key1=value1") that goes with that hit?". 但是,获得更多信息会更好,例如“什么是完整的价值(例如”key1 = value1“)与该命中一起?”。

The best I can figure out it to get the Document, then get the list of IndexableFields, then loop over all of them and see if the field.stringValue().contains("alue1"). 最好的我可以找到它来获取Document,然后获取IndexableFields的列表,然后循环遍历所有这些并查看field.stringValue()。contains(“alue1”)。 (I can look at the data structures in the debugger and all the info is there) (我可以查看调试器中的数据结构,所有信息都在那里)

This seems completely insane cause isn't that what Lucene just did ? 这似乎完全是疯了导致Lucene刚刚做了什么? Shouldn't the Hit information return some of the Fields? 命中信息不应该返回一些字段吗?

Is Lucene missing what seems like "obvious" functionality? Lucene是否错过了看似“明显”的功能? Google and starting at the APIs hasn't revealed anything straightforward, but I feel like I must be searching on the wrong stuff. 谷歌和从API开始并没有透露任何直截了当的东西,但我觉得我必须在搜索错误的东西。

You might want to try with IndexSearcher.explain() method. 您可能想尝试使用IndexSearcher.explain()方法。 Once you get the ID of the matching document, prepare a query for each field (using the same search keywords) and invoke Explanation.isMatch() for each query: the ones that yield true will give you the matched field. 获得匹配文档的ID后,为每个字段准备一个查询(使用相同的搜索关键字)并为每个查询调用explain.isMatch() :产生true的那些将为您提供匹配的字段。 Example: 例:

for (String field: fields){
    Query query = new WildcardQuery(new Term(field, "*alue1"));
    Explanation ex = searcher.explain(query, docID);
    if (ex.isMatch()){
        //Your query matched field
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM