使用Solrj可以将文档从QueryResponse读取为(带注释的)bean:
List<Item> items = queryResponse.getBeans(Item.class) 其中项是映射到Solr文档的带注释的类。
现在,我查询单个文档并请求10个MoreLikeThis文档:
?q=id:AZ133007&mlt=true&mlt.fl=technique,subject&mlt.mindf=1&mlt.mintf=1&mlt.count=10这与id AZ133007和10个'MoreLikeThis‘文档一起返回文档(例如,与字段’MoreLikeThis‘和’subject‘有关的AZ133007更类似)。见下文(简化)答复:
<response>
<lst name="responseHeader">
...
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="id">AZ133007</str>
<str name="title">Still I</str>
<str name="artist">A.R. Tist</str>
<str name="technique">Watercolor</str>
<str name="subject">Still life</str>
</doc>
</result>
<lst name="moreLikeThis">
<result name="AZ133007" numFound="84" start="0">
<doc>
<str name="id">AZ002001</str>
<str name="title">Cubes</str>
<str name="artist">John Doe</str>
<str name="technique">Watercolor</str>
<str name="subject">Landscape</str>
</doc>
<doc>
<str name="id">AZ002002</str>
<str name="title">Cats and Dogs</str>
<str name="artist">A. Nothername</str>
<str name="technique">Watercolor</str>
<str name="subject">Cityscape</str>
</doc>
...
</result>
</lst>
</response>可以将response部分中请求的文档response作为Item bean返回,如下所示:
Item item = queryResponse.getBeans(Item.class).get(0);但是如何将“moreLikeThis”中列出的文档作为bean来获取呢?
发布于 2015-09-23 12:21:02
经过大量的实验,浏览web并深入了解Solrj,我提供了以下解决方案。也许有更好的方法去做,我真的很想知道。
首先从响应中提取moreLikeThis部分并将其转换为NamedList
NamedList mlt = (NamedList) queryResponse.getResponse().get("moreLikeThis");在调试时检查NamedList mlt,它将显示AZ133007条目和“更相似”的SolrDocuments。
{
AZ133007={
numFound=295,
start=0,
docs=[
SolrDocument{
id=AZ002001,
title=Cubes,
artist=JohnDoe,
technique=Watercolor,
subject=Landscape
},
SolrDocument{
id=AZ002002,
title=CatsAndDogs,
artist=A.Nothername,
technique=Watercolor,
subject=Cityscape
},
...
]
}
}现在获得一个提供id的SolrDocumentList:
SolrDocumentList mltDocList = (SolrDocumentList) mlt.get("AZ133007");并将此SolrDocumentList绑定到bean:
DocumentObjectBinder b = new DocumentObjectBinder();
List<Item> similarItems = b.getBeans(Item.class, mltDocList);当然,我并没有对id进行硬编码,我还构建了一些空检查,但是您知道了。
https://stackoverflow.com/questions/32739459
复制相似问题