Guide To Solr in Java With Apache SolrJ – 在Java中使用Apache SolrJ的Solr指南

最后修改: 2017年 3月 1日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Apache Solr is an open-source search platform built on top of Lucene. Apache SolrJ is a Java-based client for Solr that provides interfaces for the main features of search like indexing, querying, and deleting documents.

Apache Solr是建立在Lucene之上的一个开源搜索平台。Apache SolrJ是一个基于Java的Solr客户端,它为搜索的主要功能提供了接口,如索引、查询和删除文档。

In this article, we’re going to explore how to interact with an Apache Solr server using SolrJ.

在这篇文章中,我们将探讨如何使用SolrJ与Apache Solr服务器互动

2. Setup

2.设置

In order to install a Solr server on your machine, please refer to the Solr QuickStart Guide.

为了在您的机器上安装Solr服务器,请参考Solr快速入门指南

The installation process is simple — just download the zip/tar package, extract the contents, and start the server from the command line. For this article, we’ll create a Solr server with a core called ‘bigboxstore’:

安装过程很简单–只要下载zip/tar包,解压内容,然后从命令行启动服务器。在这篇文章中,我们将创建一个Solr服务器,其核心名为 “bigboxstore”。

bin/solr start
bin/solr create -c 'bigboxstore'

By default, Solr listens to port 8983 for incoming HTTP queries. You can verify that it is successfully launched by opening the http://localhost:8983/solr/#/bigboxstore URL in a browser and observing the Solr Dashboard.

默认情况下,Solr对进入的HTTP查询进行监听,端口为8983。你可以通过在浏览器中打开http://localhost:8983/solr/#/bigboxstore URL并观察Solr仪表盘来验证它是否被成功启动。

3. Maven Configuration

3.Maven配置

Now that we have our Solr server up and running, let’s jump straight to the SolrJ Java client. To use SolrJ in your project, you will need to have the following Maven dependency declared in your pom.xml file:

现在我们的Solr服务器已经启动并运行,让我们直接跳到SolrJ的Java客户端。要在你的项目中使用SolrJ,你需要在pom.xml文件中声明以下Maven依赖。

<dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-solrj</artifactId>
    <version>6.4.0</version>
</dependency>

You can always find the latest version hosted by Maven Central.

您可以随时找到由Maven Central托管的最新版本。

4. Apache SolrJ Java API

4.Apache SolrJ Java API

Let’s initiate the SolrJ client by connecting to our Solr server:

让我们通过连接到Solr服务器来启动SolrJ客户端。

String urlString = "http://localhost:8983/solr/bigboxstore";
HttpSolrClient solr = new HttpSolrClient.Builder(urlString).build();
solr.setParser(new XMLResponseParser());

Note: SolrJ uses a binary format, rather than XML, as its default response format. For compatibility with Solr, it’s required to explicitly invoke setParser() to XML as shown above. More details on this can be found here.

注意:SolrJ使用二进制格式,而不是XML,作为其默认响应格式。为了与Solr兼容,需要明确地调用setParser() 到XML,如上所示。关于这一点的更多细节可以在这里找到。

4.1. Indexing Documents

4.1.为文件编制索引

Let’s define the data to be indexed using a SolrInputDocument and add it to our index using the add() method:

让我们使用SolrInputDocument定义要被索引的数据,并使用add()方法将其添加到我们的索引。

SolrInputDocument document = new SolrInputDocument();
document.addField("id", "123456");
document.addField("name", "Kenmore Dishwasher");
document.addField("price", "599.99");
solr.add(document);
solr.commit();

Note: Any action that modifies the Solr database requires the action to be followed by commit().

注意:任何修改Solr数据库的操作都需要在该操作后面加上commit()

4.2. Indexing With Beans

4.2.用Bean做索引

You can also index Solr documents using beans. Let’s define a ProductBean whose properties are annotated with @Field:

你也可以使用Bean来索引Solr文档。让我们定义一个ProductBean,其属性用@Field来注释。

public class ProductBean {

    String id;
    String name;
    String price;

    @Field("id")
    protected void setId(String id) {
        this.id = id;
    }

    @Field("name")
    protected void setName(String name) {
        this.name = name;
    }

    @Field("price")
    protected void setPrice(String price) {
        this.price = price;
    }

    // getters and constructor omitted for space
}

Then, let’s add the bean to our index:

然后,让我们把这个Bean添加到我们的索引中。

solrClient.addBean( new ProductBean("888", "Apple iPhone 6s", "299.99") );
solrClient.commit();

4.3. Querying Indexed Documents by Field and Id

4.3.按字段和标识查询被索引的文件

Let’s verify our document is added by using SolrQuery to query our Solr server.

让我们通过使用SolrQuery来查询我们的Solr服务器来验证我们的文档是否被添加。

The QueryResponse from the server will contain a list of SolrDocument objects matching any query with the format field:value. In this example, we query by price:

来自服务器的QueryResponse将包含一个符合任何查询格式field:valueSolrDocumentobjects列表。在这个例子中,我们通过价格查询。

SolrQuery query = new SolrQuery();
query.set("q", "price:599.99");
QueryResponse response = solr.query(query);

SolrDocumentList docList = response.getResults();
assertEquals(docList.getNumFound(), 1);

for (SolrDocument doc : docList) {
     assertEquals((String) doc.getFieldValue("id"), "123456");
     assertEquals((Double) doc.getFieldValue("price"), (Double) 599.99);
}

A simpler option is to query by Id using getById(). which will return only one document if a match is found:

一个更简单的选择是用Id查询,使用getById()。如果找到匹配的文档,将只返回一个文档。

SolrDocument doc = solr.getById("123456");
assertEquals((String) doc.getFieldValue("name"), "Kenmore Dishwasher");
assertEquals((Double) doc.getFieldValue("price"), (Double) 599.99);

4.4. Deleting Documents

4.4.删除文件

When we want to remove a document from the index, we can use deleteById() and verify it has been removed:

当我们想从索引中删除一个文档时,我们可以使用deleteById(),并验证它是否已经被删除。

solr.deleteById("123456");
solr.commit();
SolrQuery query = new SolrQuery();
query.set("q", "id:123456");
QueryResponse response = solr.query(query);
SolrDocumentList docList = response.getResults();
assertEquals(docList.getNumFound(), 0);

We also have the option to deleteByQuery(), so let’s try deleting any document with a specific name:

我们也可以选择deleteByQuery(),所以让我们试着删除任何有特定名字的文档。

solr.deleteByQuery("name:Kenmore Dishwasher");
solr.commit();
SolrQuery query = new SolrQuery();
query.set("q", "id:123456");
QueryResponse response = solr.query(query);
SolrDocumentList docList = response.getResults();
assertEquals(docList.getNumFound(), 0);

5. Conclusion

5.结论

In this quick article, we’ve seen how to use the SolrJ Java API to perform some of the common interactions with the Apache Solr full-text search engine.

在这篇快速文章中,我们已经看到了如何使用SolrJ Java API来执行与Apache Solr全文搜索引擎的一些常见交互。

You can check out the examples provided in this article over on GitHub.

你可以查看本文在GitHub上提供的例子