Solr入门案例,通过java实现查询,添加,删除solr的索引的操作,特别需要注意的一点是,solr的jar版本需要通过服务器版本一致,不然又有错误问题,同时还要注意依赖包的问题。
添加索引
添加索引的字段,必须是在schema.xml 中配置了,不然就报错
/**
* 添加索引到solr服务器
* @throws Exception
* @throws IOException
*/
public static void addIndex() throws Exception, IOException {
// 实例化solr对象
SolrServer solrServer = new HttpSolrServer(SOLR_PATH);
// 创建一个文档
//添加的Field 必须是在schema.xml 中配置了,不然就报错
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "6");
doc.addField("price", "100");
doc.addField("url", "http://test.com");
doc.addField("name", "毛衣6666");
// 创建文档2
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("id", "7");
doc2.addField("price", "100");
doc2.addField("url", "http://test.com");
doc2.addField("name", "毛衣77776");
// 单独添加的方式
// solrServer.add(doc);
// solrServer.add(doc2);
// 集合添加多个的方式
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add(doc2);
docs.add(doc);
solrServer.add(docs);
// 提交
solrServer.commit();
}
查询索引
案例
/**
* 查询solr里面的数据
* @throws Exception
*/
public static void query() throws Exception {
//查询索引
// 实例化solr对象
SolrServer solrServer = new HttpSolrServer(SOLR_PATH);
//添加查询
SolrQuery solrQuery = new SolrQuery();
//查询
solrQuery.set("q", "name:毛衣");
//获取查询结果
QueryResponse response = solrServer.query(solrQuery);
//获取查询到的文档
SolrDocumentList docs = response.getResults();
//查询到的条数
long cnt = docs.getNumFound();
System.out.println("查询到的条数\t"+cnt);
//获取查询结果
for(SolrDocument doc :docs) {
String id = doc.get("id").toString();
String price = doc.get("price").toString();
String url = doc.get("url").toString();
String name = doc.get("name").toString();
System.out.printf("%s:%s:%s:%s\r\n",id,price,url,name);
}
}
运行结果
查询的参数
删除索引
删除的方式有两种,一种是通过id来删除,还有一种是通过查询的方式删除。
/**
* 通过id来删除数据
* @throws Exception
* @throws IOException
*/
public static void deleteById() throws Exception, IOException {
// 实例化solr对象
SolrServer solrServer = new HttpSolrServer(SOLR_PATH);
solrServer.deleteById("3");
//通过id来删除
solrServer.commit();
}
/**
* 通过查询条件来珊瑚
* @throws Exception
* @throws IOException
*/
public static void deleteByQuery() throws Exception, IOException {
// 实例化solr对象
SolrServer solrServer = new HttpSolrServer(SOLR_PATH);
solrServer.deleteByQuery("name:毛衣");
//需要提交,不提交,就没有删除
solrServer.commit();
}
完整代码
package day12_04;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
/**
*
* 作者:yellowcong 日期:2017/12/04 時間:13:19:59 描述:
*/
public class Demo {
private static final String SOLR_PATH = "http://192.168.66.100:8080/solr/";
public static void main(String[] args) throws Exception, IOException {
//添加索引
//addIndex();
//查询数据
// query();
//通过查询方式 删除
deleteByQuery();
query();
// deleteById();
}
/**
* 通过id来删除数据
* @throws Exception
* @throws IOException
*/
public static void deleteById() throws Exception, IOException {
// 实例化solr对象
SolrServer solrServer = new HttpSolrServer(SOLR_PATH);
solrServer.deleteById("3");
//通过id来删除
solrServer.commit();
}
/**
* 通过查询条件来珊瑚
* @throws Exception
* @throws IOException
*/
public static void deleteByQuery() throws Exception, IOException {
// 实例化solr对象
SolrServer solrServer = new HttpSolrServer(SOLR_PATH);
solrServer.deleteByQuery("name:毛衣");
//需要提交,不提交,就没有删除
solrServer.commit();
}
/**
* 查询solr里面的数据
* @throws Exception
*/
public static void query() throws Exception {
//查询索引
// 实例化solr对象
SolrServer solrServer = new HttpSolrServer(SOLR_PATH);
//添加查询
SolrQuery solrQuery = new SolrQuery();
//查询
solrQuery.set("q", "name:毛衣");
//获取查询结果
QueryResponse response = solrServer.query(solrQuery);
//获取查询到的文档
SolrDocumentList docs = response.getResults();
//查询到的条数
long cnt = docs.getNumFound();
System.out.println("查询到的条数\t"+cnt);
//获取查询结果
for(SolrDocument doc :docs) {
String id = doc.get("id").toString();
String price = doc.get("price").toString();
String url = doc.get("url").toString();
String name = doc.get("name").toString();
System.out.printf("%s:%s:%s:%s\r\n",id,price,url,name);
}
}
/**
* 添加索引到solr服务器
* @throws Exception
* @throws IOException
*/
public static void addIndex() throws Exception, IOException {
// 实例化solr对象
SolrServer solrServer = new HttpSolrServer(SOLR_PATH);
// 创建一个文档
//添加的Field 必须是在schema.xml 中配置了,不然就报错
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "6");
doc.addField("price", "100");
doc.addField("url", "http://test.com");
doc.addField("name", "毛衣6666");
// 创建文档2
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("id", "7");
doc2.addField("price", "100");
doc2.addField("url", "http://test.com");
doc2.addField("name", "毛衣77776");
// 单独添加的方式
// solrServer.add(doc);
// solrServer.add(doc2);
// 集合添加多个的方式
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add(doc2);
docs.add(doc);
solrServer.add(docs);
// 提交
solrServer.commit();
}
}
环境搭建
如果没有安装这个solr需要搭建基础环境,请查看Solr之单机安装-yellowcong
项目结构
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>yellowcong</groupId>
<artifactId>day12_04</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>day12_04</name>
<url>http://maven.apache.org</url>
<!-- 配置国内比较快的 阿里云的Maven仓库 -->
<repositories>
<repository>
<id>aliyunmaven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lucene.version>4.5.1</lucene.version>
<mmseg4j.version>1.9.1</mmseg4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- lucene核心包 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>${lucene.version}</version>
</dependency>
<!--QueryParser 查询类 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>${lucene.version}</version>
</dependency>
<!-- 分词器 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>${lucene.version}</version>
</dependency>
<!-- 高亮显示 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>${lucene.version}</version>
</dependency>
<!-- 庖丁解牛分词器 -->
<dependency>
<groupId>com.chenlb.mmseg4j</groupId>
<artifactId>mmseg4j-core</artifactId>
<version>${mmseg4j.version}</version>
</dependency>
<dependency>
<groupId>com.chenlb.mmseg4j</groupId>
<artifactId>mmseg4j-analysis</artifactId>
<version>${mmseg4j.version}</version>
</dependency>
<!-- ikanalyzer 分词器 -->
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>
<!-- solrj -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.5.1</version>
</dependency>
<!-- 日志配置文件,必须有,不然会报错 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.16</version>
</dependency>
</dependencies>
</project>
日志文件 log4j
#log4j.rootLogger=debug,Console
log4j.rootLogger=error,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
错误合集
日志环境
solr需要添加日志的jar包,不然就会报错
配置pom.xml文件,然后配置log4j
<!-- 日志配置文件,必须有,不然会报错 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.16</version>
</dependency>
配置log4j
log4j.rootLogger=debug,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
unknown field
在提交的数据,字段必须是在schema.xml的文件中配置过的
Exception in thread "main" org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: ERROR: [doc=7] unknown field 'nam21212e'
schema.xml的文件中,必须是存在的字段,如果不存在,我们添加,就会报错
vim /usr/local/solr/solr-4.10.3/example/solr/collection1/conf/schema.xml