上一篇学习了lucene原理及简单的使用.这次基于lucene上学习一下ES的使用.
之前在linux上部署过ES(之前的文章有部署流程),然后今天启动发现启动不起来,报错
No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender
于是搜了一下,ES启动报错No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender
是因为之前以root的身份进入log文件导致文件权限变成了root权限
将文件权限修改一下即可
执行命令: chown yyy[我的用户名] xxx.log
修改完后启动正常了.
ES集群:
创建es-clutser文件夹,在内部复制三个ES服务.
然后分别修改每个ES服务中elasticsearch-cluster\node*\config\elasticsearch.yml配置文件:
node1节点:
#节点1的配置信息:
#集群名称,保证唯一
cluster.name: my-elasticsearch
#节点名称,必须不一样
node.name: node-1
#必须为本机的ip地址
network.host: 127.0.0.1
#服务端口号,在同一机器下必须不一样
http.port: 9200
#集群间通信端口号,在同一机器下必须不一样
transport.tcp.port: 9300
#设置集群自动发现机器ip集合
discovery.zen.ping.unicast.hosts: [“127.0.0.1:9300”,“127.0.0.1:9301”,“127.0.0.1:9302”]
node2节点:
#节点2的配置信息:
#集群名称,保证唯一
cluster.name: my-elasticsearch
#节点名称,必须不一样
node.name: node-2
#必须为本机的ip地址
network.host: 127.0.0.1
#服务端口号,在同一机器下必须不一样
http.port: 9201
#集群间通信端口号,在同一机器下必须不一样
transport.tcp.port: 9301
#设置集群自动发现机器ip集合
discovery.zen.ping.unicast.hosts: [“127.0.0.1:9300”,“127.0.0.1:9301”,“127.0.0.1:9302”]
node3节点:
#节点3的配置信息:
#集群名称,保证唯一
cluster.name: my-elasticsearch
#节点名称,必须不一样
node.name: node-3
#必须为本机的ip地址
network.host: 127.0.0.1
#服务端口号,在同一机器下必须不一样
http.port: 9202
#集群间通信端口号,在同一机器下必须不一样
transport.tcp.port: 9302
#设置集群自动发现机器ip集合
discovery.zen.ping.unicast.hosts: [“127.0.0.1:9300”,“127.0.0.1:9301”,“127.0.0.1:9302”]
然后依次启动各个节点服务器.
ES使用(通过SpringDataES):
环境搭建
:
xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
<!--创建transportClient对象-->
<elasticsearch:transport-client id="transportClient"
cluster-name="my-elasticsearch"
cluster-nodes="127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302"/>
<!--ESTemplate对象-->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="transportClient"/>
</bean>
<!--dao扫描器-->
<elasticsearch:repositories base-package="com.ceeemall.es.dao" elasticsearch-template-ref="elasticsearchTemplate"/>
entity:
package com.ceeemall.es.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "test",type="article") //索引库名称 类型名称
public class Article {
@Id //主键标识
@Field(type = FieldType.Long,store = true) //type 数据类型 store 是否存储
private long id;
@Field(type = FieldType.text,store = true,analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.text,store = true,analyzer = "ik_max_word") //analyzer 分词器 使用ik
private String content;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
操作:
索引库的操作:
package com.ceeemall.es;
import com.ceeemall.es.entity.Article;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpEsTest {
@Autowired
private ElasticsearchTemplate template;
@Test
public void creatIndex(){
//初始化spring容器
//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//从容器中获取ElasticsearchTemplate对象
// ElasticsearchTemplate template = applicationContext.getBean(ElasticsearchTemplate.class);
//通过template对象创建索引库,指定entity可以基于entity创建索引库
template.createIndex(Article.class);
}
//删除索引库
@Test
public void deleteIndex(){
template.deleteIndex("test");
}
//设置mapping
@Test
public void putMapping(){
template.putMapping(Article.class);
}
}
文档的操作:
定义dao:
package com.ceeemall.es.dao;
import com.ceeemall.es.entity.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
public interface ArticleDao extends ElasticsearchCrudRepository<Article,Long> {
}
添加文档:
//添加文档
@Test
public void addDocument(){
Article article = new Article();
article.setId(1);
article.setTitle("你好,我是title");
article.setContent("你好,我是content");
//添加文档
articleDao.save(article);
}
添加成功:
删改就不说了,就是delete和save方法.
查询:
通过id查询:
@Test
public void findById(){
//返回optional对象
Optional<Article> optional = articleDao.findById(1L);
//判断optional中是否有值
if (optional.isPresent()){
Article article = optional.get();
System.out.println(article);
}
}
查询所有:
@Test
public void findAll(){
Iterable<Article> all = articleDao.findAll();
for (Article article : all) {
System.out.println(article);
}
}
传入page对象可以实现分页查询.
条件查询:
使用findBy+ 查询字段的名称,参数中就是查询的条件:
dao:
public interface ArticleDao extends ElasticsearchCrudRepository<Article,Long> {
List<Article> findByTitle(String title);
}
@Test
public void findByTitle(){
List<Article> articles = articleDao.findByTitle("title");
for (Article article : articles) {
System.out.println(article);
}
}
也可添加pageable实现分页
queryString查询:
需要使用原生的查询条件查询,通过NativeSearchQuery创建查询条件,查询时使用estemplate查询.
@Test
public void finfByQueryString(){
//创建原生查询条件
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().
//查询条件
withQuery(QueryBuilders.queryStringQuery("你好")
//设置默认查询字段
.defaultField("content")).build();
//通过estemplate对象执行查询
List<Article> articles = template.queryForList(nativeSearchQuery, Article.class);
for (Article article : articles) {
System.out.println(article);
}
}