【问题标题】:Solrj: getting MoreLikeThis as beansSolrj:将 MoreLikeThis 作为 bean
【发布时间】:2015-09-23 12:21:02
【问题描述】:

使用 Solrj 可以从 QueryResponse 读取文档作为(带注释的)bean:

List<Item> items = queryResponse.getBeans(Item.class)  

Item 是映射到 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”文档(即,在“技术”和“主题”字段方面更像 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 部分中请求的文档AZ133007 可以作为Item bean 返回,如下所示:

Item item = queryResponse.getBeans(Item.class).get(0);

但是如何将“moreLikeThis”下列出的文档作为 bean 获取?

【问题讨论】:

    标签: java solr solrj


    【解决方案1】:

    经过大量试验、浏览网页并深入研究 Solrj API,我得到了以下解决方案。可能有更好的方法来做到这一点,我真的很想知道。

    首先从响应中提取moreLikeThis 部分并将其转换为NamedList

    NamedList mlt = (NamedList) queryResponse.getResponse().get("moreLikeThis");
    

    在调试时检查NamedListmlt,它会显示AZ133007 条目以及“moreLikeThis”的 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
                },
    
                ...
    
            ]
         }
      }
    

    现在得到一个 SolrDocumentList 提供 id:

    SolrDocumentList mltDocList = (SolrDocumentList) mlt.get("AZ133007");
    

    并将这个 SolrDocumentList 绑定到 beans:

    DocumentObjectBinder b = new DocumentObjectBinder();
    List<Item> similarItems = b.getBeans(Item.class, mltDocList);
    

    当然,我没有对 id 进行硬编码,而且我内置了一些 null 检查,但你明白了。

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      相关资源
      最近更新 更多