从 Solr 检索对象 [英] Retrieve Object from Solr

查看:27
本文介绍了从 Solr 检索对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个 基于 Java 的 Web 项目,它显示从托管在不同服务器上的 3 个独立服务检索的信息,我使用 Apache Http Client 通过 REST 检索信息JSON 中的 API,使用 Gson 库.我将 Json 转换为用于显示信息的 POJO.

So, I have a java based web project that displays information retrieved from 3 separate services, hosted on different servers, I use Apache Http Client to retrieve information via REST API in JSON, using Gson library. I convert the Json into POJO's that I use to display information.

现在我想在我的项目中实现搜索功能,所以我在单独的服务器上安装了 Solr,我想要的是:

Now I want to implement search feature in my project, so I installed Solr on a separate server, what I want is:

  1. 为所有 3 个服务在 solr 服务器中索引 JSON.

  1. Index the JSON in solr server for all 3 services.

以我的项目中描述的 POJO 的形式从 Solr 获取搜索结果

fetch search result from Solr in form of POJO's described in my project

我知道第 (1) 点可以通过 jsonRequestHandler 完成,但我不想编写单独的逻辑来索引,我在我的项目中使用 Solrj 来提取信息.

I know that point (1) can be done by jsonRequestHandler, but I don't want to write separate logic to index, I am using Solrj in my project to extract information.

所以我想知道

  • solrj 可以使用我的 POJO 定义来解析搜索结果吗?
  • 还有适用于上述工作场景的任何可能的工作流程和工具需要(我是 solrj 的新手)?

推荐答案

为 Solr 映射 POJO

为此,您需要使用 org.apache.solr.client.solrj.beans.Field-Annotation 注释 POJO 的字段/访问方法.

Mapping a POJO for Solr

To do so you need to annotate the fields/access-methods of your POJO with the org.apache.solr.client.solrj.beans.Field-Annotation.

当然,这些字段需要通过名称直接匹配您的 schema.xml 中的字段,或者通过在 Field 注释中给出名称而通过您指向 Solr 的名称来匹配.

Of course those fields need to match the fields of your schema.xml either by their name directly or by the name you point Solr to by giving the name in the Field annotation.

例如,您在 schema.xml

As example you have the following definition of fields in your schema.xml

<fields>
  <field name="id"    type="int"    indexed="true" stored="true" multiValued="false" />
  <field name="title" type="string" indexed="true" stored="true" multiValued="false" />
</fields>

然后你就会有一个这样的 POJO

Then you would have a POJO like this

import org.apache.solr.client.solrj.beans.Field;

public class SampleDocument {

    public SampleDocument() {
        // required for solrj to make an instance
    }

    public SampleDocument(int id, String title) {
        this.id = id;
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    @Field("title")
    public void setTitle(String title) {
        this.title = title;
    }

}

使用 POJO 更新 Solr 的索引

索引这些 POJO 的代码相当简单.为此,您可以使用 solrj 的 SolrServer.

Using a POJO to update Solr's Index

The code to index those POJOs is rather straight forward. You can use solrj's SolrServer for that purpose.

// connect to your solr server
SolrServer server = new HttpSolrServer("http://HOST:8983/solr/");

// adding a single document
SampleDocument document = new SampleDocument(1, "title 1");
server.addBean(document);

// adding multiple documents
List<SampleDocument> documents = Arrays.asList(
        new SampleDocument(2, "title 2"), 
        new SampleDocument(3, "title 3"));
server.addBeans(documents);

// commit changes
server.commit();

// query solr for something
QueryResponse response = server.query(new SolrQuery("*:*"));
// get the response as List of POJO type
List<SampleDocument> foundDocuments = response.getBeans(SampleDocument.class);

进一步阅读

结果是我们编写的代码和以下参考

Further reading

The results are a write up of our code and the following references

这篇关于从 Solr 检索对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆