solr start –p p_num 启动solr
solr restart –p p_num 重启solr
solr stop –p p_num 关闭solr
solr create –c c_name 创建一个核心
每个核心都是solr的一个实例,一个solr服务可以创建多个核心,每个核心都可以进行自己独立配置。
方式一:打开solr界面,进行如图顺序操作。(推荐)
方式二:bin目录下输入命令 solr create –c new_core
schame文件可以对索引库的数据类型进行定义,对字段是否进行索引、储存等进行设置,要针对核心单独进行配置。
schame的数据类型基本够用,如果不能满足需求,比如说对中文分词、拼音分词等,就可以自定义分词器。
Solr 7.5的schema配置文件名为managed-schema,路径为solr_home\server\solr\new_core\conf\managed-schema(D:\Program Files\solr-7.5.0\server\solr\new_core\conf\managed-schema)。
(1) fieldType:为field定义类型,最主要作用是定义分词器,分词器决定着如何从文档中检索关键字。
(2) analyzer:fieldType的子元素,是分词器,由tokenizer和filter组成。例如
<fieldType name="text_tr" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.TurkishLowerCaseFilterFactory"/>
<filter class="solr.StopFilterFactory" words="lang/stopwords_tr.txt" ignoreCase="false"/>
<filter class="solr.SnowballPorterFilterFactory" language="Turkish"/>
analyzer>
fieldType>
(3) field:字段,用来创建索引,如果这个字段需要生成索引,则需要设置的indexed为true,需要存储设置stored属性为true。例如:
<field name="age" type="pint" indexed="true" stored="true"/>
方式一:直接修改managed-schema配置文件(不推荐,修改后需要重启服务),例如:
<field name="age" type="pint" indexed="true" stored="true"/>
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false" conf="ik.conf"/>
<filter class="solr.LowerCaseFilterFactory"/>
analyzer>
<analyzer type="query">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true" conf="ik.conf"/>
<filter class="solr.LowerCaseFilterFactory"/>
analyzer>
fieldType>
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solr_test"
user="root" password=""/>
<document>
<entity name="user" query="select * from user">
<field column="id" name="id"/>
<field column="age" name="age"/>
<field column="name" name="name"/>
<field column="hobby" name="hobby"/>
entity>
document>
dataConfig>
<field name="name" type="string" indexed="true" stored="true"/>
<field name="age" type="pint" indexed="true" stored="true"/>
<field name="hobby" type="string" indexed="true" stored="true"/>
请勿添加id字段,该字段已存在,添加会报错
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">my-data-config.xmlstr>
lst>
requestHandler>
<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.0modelVersion>
<groupId>com.freeskygroupId>
<artifactId>solr-testartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.apache.solrgroupId>
<artifactId>solr-solrjartifactId>
<version>7.5.0version>
dependency>
dependencies>
project>
public class User {
@Field(value = "id")
private String id;
@Field(value = "name")
private String name;
@Field(value = "age")
private Integer age;
@Field(value = "hobby")
private String hobby;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", hobby=" + hobby +
'}';
}
}
public class Main {
private static final String SOLR_URL = "http://localhost:8888/solr";
private static final String CORE_NAME = "new_core";
public static void main(String[] args) throws IOException, SolrServerException {
HttpSolrClient client = new HttpSolrClient.Builder(SOLR_URL).withConnectionTimeout(10000).withSocketTimeout(60000).build();
SolrQuery query = new SolrQuery();
//设置要查询的字段
query.setFields("id","name", "age","hobby");
//查询全部
QueryResponse response = client.query(CORE_NAME,query);
//查询结果
SolrDocumentList results = response.getResults();
System.out.println("结果总数:" + results.getNumFound());
//遍历列表
System.out.println("文档结果:");
for (SolrDocument doc : results) {
System.out.printf("[id:%s,name:%s,age:%s,hobby:%s]\n", doc.get("id"), doc.get("name"), doc.get("age"), doc.get("hobby"));
}
//得到实体对象
List<User> userList = response.getBeans(User.class);
System.out.println("转为实体对象:\n"+Arrays.toString(userList.toArray()));
}
}
按条件单个查询:
//查询name为“陈龙”的user
query.set("q","name:陈龙" );
按条件模糊查询:
//查询name以“四”结尾的user
query.set("q","name:*四" );
//查询name以“陈”开头的user
query.set("q","name:陈*" );
//查询name包含“玉”的user
query.set("q","name:*玉*" );
多条件查询:
//查询id为“1”,并且name为“陈龙”,或者age为“28”岁,或者爱好包含“钓鱼”的user
query.set("q","id:1 AND name:陈龙 OR age:28 OR hobby:钓鱼" );
过滤查询:
//过滤查询,age为1到30岁的user
query.set("fq", "age:[1 TO 30]");
设置排序:
//以age降序
query.addSort("age", SolrQuery.ORDER.desc);
设置分页:
//开始位置
query.setStart(0);
//每页3条
query.setRows(3);