elasticSearch集群搭建---入门篇二

Elasticsearch(二)

elasticSearch安装,基本操作,入门篇一

1 通过java客户端对es维护

1.1 工程搭建

  • 创建maven工程

elasticSearch集群搭建---入门篇二_第1张图片

  • 添加依赖,在pom.xml文件添加如下依赖:
<dependencies>
    <dependency>
        <groupId>org.elasticsearchgroupId>
        <artifactId>elasticsearchartifactId>
        <version>5.6.8version>
    dependency>
    <dependency>
        <groupId>org.elasticsearch.clientgroupId>
        <artifactId>transportartifactId>
        <version>5.6.8version>
    dependency>
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-to-slf4jartifactId>
        <version>2.9.1version>
    dependency>
    <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-apiartifactId>
        <version>1.7.24version>
    dependency>
    <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-simpleartifactId>
        <version>1.7.21version>
    dependency>
    <dependency>
        <groupId>log4jgroupId>
        <artifactId>log4jartifactId>
        <version>1.2.12version>
    dependency>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
dependencies>
  • 创建测试类ElasticSearchDemo

elasticSearch集群搭建---入门篇二_第2张图片

1.2 索引操作

1.2.1 创建索引

在测试类中添加testCreateIndex方法:

为方便测试:在测试类中添加before和after方法,分别在测试方法前执行,创建客户端连接对象,测试方法执行后执行,关闭资源

private Client client = null;
    @Before
    public void before() {
        client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(
                        new InetSocketAddress("localhost", 9300)));
    }

    @After
    public void after() {
        client.close();
    }
// 创建索引
    @Test
    public void testCreateIndex() {
        client.admin().indices().prepareCreate("blog3").get();
    }

1.2.2 删除索引

在测试类中添加testDeleteIndex方法:

 //删除索引
    @Test
    public void testDeleteIndex() {
        client.admin().indices().prepareDelete("blog3").get();
    }

1.3 创建映射

1.3.1 创建映射前

这里的映射表示创建索引类型结构,如果不创建映射,Elasticsearch会默认根据创建的文档中的数据用来创建映射。查看之前的blog3的映射。

elasticSearch集群搭建---入门篇二_第3张图片

1.3.2 创建映射

在测试类中添加方法:

	// 创建映射 注意创建映射时一定要有索引的存在,不然无法创建映射,
    // 映射就相当于表结构 创建表和表的结构时,必须要有数据库(索引)
    @Test
   public void testCreateMapping1() throws Exception {
        XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
        xContentBuilder
                .startObject()
                    .startObject("properties")
                        .startObject("id")
                            .field("type", "long")
                        .endObject()
                        .startObject("title")
                            .field("type", "text")
                            .field("store", false)
                            .field("analyzer", "ik_smart")
                        .endObject()
                        .startObject("content")
                            .field("type", "text")
                            .field("store", false)
                            .field("analyzer", "ik_smart")
                        .endObject()
                    .endObject()
                .endObject();

        PutMappingRequest mappingRequest = Requests.putMappingRequest("blog3").type("article").source(xContentBuilder);
        client.admin().indices().putMapping(mappingRequest);
    }

1.3.3 创建映射后

创建映射后的结果如下:

elasticSearch集群搭建---入门篇二_第4张图片

1.4 文档数据操作

1.4.1 创建文档数据

1.4.1.1 通过XContentBuilder创建

通过XContentBuilder构建文档数据。在测试中添加方法:

 // 创建文档数据
    @Test
    public void testCreateDocByXContentBuilder() throws IOException {
        XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
                .startObject()
                .field("id", 2)
                .field("title", "学习路线")
                .field("content", "计算机组成原理、计算机网络、数据结构、操作系统等")
                .endObject();
        client.prepareIndex("blog3","article","2").setSource(xContentBuilder).get();
    }

1.4.1.2 通过POJO创建

通过POJO构建文档数据。在测试中添加方法:

  • 添加Jackson依赖(需要将pojo转成json对象)

    
    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-coreartifactId>
        <version>2.8.1version>
    dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-databindartifactId>
        <version>2.8.1version>
    dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-annotationsartifactId>
        <version>2.8.1version>
    dependency>
    
  • 创建pojo,在src目录下创建Article对象

package com.demo;

public class Article {

    private Integer id;
    private String title;
    private String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer 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;
    }

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}
  • 编写代码
//根据bean创建文档数据
    @Test
    public void testCreateDocByPojo() throws IOException {
        Article article = new Article();
        article.setId(3);
        article.setTitle("elasticSearch");
        article.setContent("elasticSearch时搜索引擎服务,查询速度快");
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] source = objectMapper.writeValueAsBytes(article);
        client.prepareIndex("blog3","article","3").setSource(source).get();
    }

1.4.2 更新文档数据

1.4.2.1 通过prepareUpdate方法修改

在测试类添加方法:

//根据prepareUpdate更新文档数据
    @Test
    public void testUpdateDoc() throws JsonProcessingException {
        Article article = new Article();
        article.setId(2);
        article.setTitle("java学习路线-更新了");
        article.setContent("需要学习数据结构、计算机网络、计算机组成原理、操作系统等-更新了");
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] sources = objectMapper.writeValueAsBytes(article);
        client.prepareUpdate("blog3","article","2").setDoc(sources, XContentType.JSON).get();
    }

1.4.2.2 通过update方法修改

添加测试方法:

 //根据update更新文档数据
    @Test
    public void testUpdate() throws JsonProcessingException, ExecutionException, InterruptedException {
        Article article = new Article();
        article.setId(3);
        article.setTitle("elasticSearch--更新了");
        article.setContent("elasticSearch时搜索引擎服务,查询速度快--更新了");
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] bytes = objectMapper.writeValueAsBytes(article);
        UpdateRequest request = new UpdateRequest("blog3", "article", "3").doc(bytes,XContentType.JSON);
        client.update(request).get();
    }

1.4.3 删除文档数据

在测试类中添加方法:

 //删除文档
    @Test
    public void testDelte(){
        client.prepareDelete("blog3","article","3").get();
    }

1.4.4 批量增加数据

在测试类中添加方法:

 //批量增加数据
    @Test
    public void testBatchDocs() throws JsonProcessingException, ExecutionException, InterruptedException {
        BulkRequestBuilder bulk = client.prepareBulk();
        ObjectMapper objectMapper = new ObjectMapper();
        for (int i = 1; i <= 10; i++) {
            Article article = new Article();
            article.setId(i);
            article.setTitle("如何学好es"+i);
            article.setContent("理解Elasticsearch需要掌握倒排索引,版本为:"+i);
            byte[] bytes = objectMapper.writeValueAsBytes(article);
            IndexRequestBuilder indexRequestBuilder = client.prepareIndex("blog3", "article", String.valueOf(i)).setSource(bytes, XContentType.JSON);
            bulk.add(indexRequestBuilder);
        }
        bulk.execute().get();
    }

1.5 查询

1.5.1 根据字符串查询

在测试类中添加方法:

 // 根据字符串查询
    @Test
    public void testQueryByString() throws Exception {
        SearchResponse searchResponse = client.prepareSearch("blog3")
                                            .setTypes("article")
                                            .setQuery(QueryBuilders.queryStringQuery("倒排索引"))// 根据字符串查询
                                            .get();
        SearchHits hits = searchResponse.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

1.5.2 根据词条查询

在测试类中添加方法:

// 根据词条查询
    /*
    * IK分词器,在建立索引的时候将英文都变成了小写,这样方便我们在搜索的时候可以实现“不区分大小写”的搜索,
    * 因此在编写程序时,因此我们在根据英文单词查询时,需要将大写字母转成小写字母
    * */
    @Test
    public void testQueryByTerm() throws Exception {
        SearchResponse searchResponse = client.prepareSearch("blog3")
                .setTypes("article")
                .setQuery(QueryBuilders.termQuery("content","Elasticsearch".toLowerCase()))//根据词条查询
                .get();
        SearchHits hits = searchResponse.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

elasticSearch集群搭建---入门篇二_第5张图片

1.5.3 结果集处理

在上面的查询中,获取到的结果都是json对象,因此我们可以转成pojo。在测试中添加方法:

 //结果集处理
    @Test
    public void testQueryForResult() throws IOException {
        SearchResponse searchResponse = client.prepareSearch("blog3")
                .setTypes("article")
                .setQuery(QueryBuilders.termQuery("content", "Elasticsearch".toLowerCase()))
                .get();
        SearchHits hits = searchResponse.getHits();
        ArrayList<Article> articles = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        for (SearchHit hit : hits) {
            String source = hit.getSourceAsString();
            Article article = objectMapper.readValue(source, Article.class);
            articles.add(article);
        }
        for (Article article : articles) {
            System.out.println(article);
        }

    }

1.5.4 其他查询实现

在测试类中添加方法:

@Test
    public void testQueryByOtherCondition() throws Exception {
        SearchResponse searchResponse = client.prepareSearch("blog3")
                .setTypes("article")
                //.setQuery(QueryBuilders.queryStringQuery("倒排索引"))// 字符串查询
                //.setQuery(QueryBuilders.termQuery("title", "如何"))
                //.setQuery(QueryBuilders.termQuery("content","Elasticsearch".toLowerCase()))//根据词条查询
                //.setQuery(QueryBuilders.matchAllQuery())//查询所有
                //.setQuery(QueryBuilders.wildcardQuery("content","?引"))//匹配一个
                //.setQuery(QueryBuilders.wildcardQuery("content","*索引"))//零个或者多个
                //.setQuery(QueryBuilders.rangeQuery("id").from(1,true).to(5,true))//区间段查询
                .setQuery(QueryBuilders.fuzzyQuery("content","elastiasaarch")) 相似度查询,匹配的错误字符个数[0,2]
                .get();
        SearchHits hits = searchResponse.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

1.5.5 组合查询

组合查询:即添加多个条件。

 must(QueryBuilders) : AND,求交集
 mustNot(QueryBuilders): NOT,求差集
 should(QueryBuilders):OR ,求并集

在测试类中添加方法:

 //组合查询
    @Test
    public void testQueryByBoolean(){
        SearchResponse searchResponse = client.prepareSearch("blog3")
                .setTypes("article")
                .setQuery(QueryBuilders
                        .boolQuery()
                        .must(QueryBuilders.matchAllQuery())
                        .mustNot(QueryBuilders.rangeQuery("id")
                        .from(1, true).to(5, true))).get();
        SearchHits hits = searchResponse.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

1.5.6 使用DSL表达式

使用restful风格编程,传递消息体,使用head插件查看索引库的信息,进行查询:

请求体:

{
  "query" : {
    "bool" : {
      "must" : {
        "term" : {
          "title" : "es"
        }
      },
      "must" : {
        "range" : {
          "id" : {
            "from" : 5,
            "to" : 55
          }
        }
      }
    }
  }
}

elasticSearch集群搭建---入门篇二_第6张图片

1.5.7 分页并排序

在测试类中添加方法:

// 分页并排序
    @Test
    public void test() throws IOException {
        // 调用查询方法
        SearchResponse searchResponse = client.prepareSearch("blog3")
                .setTypes("article")
                .setQuery(QueryBuilders.matchAllQuery())
                .addSort("id", SortOrder.DESC)// 排序
                .setFrom(0) // 起始行=(当前页码 - 1)* 每页显示的条数
                .setSize(20) // 每页显示的条数
                .get();
        // 结果集处理
        SearchHits hits = searchResponse.getHits();
        System.out.println("查询总数"+hits.getTotalHits());
        ObjectMapper objectMapper = new ObjectMapper();
        for (SearchHit hit : hits) {
            String source = hit.getSourceAsString();
            Article article = objectMapper.readValue(source, Article.class);
            System.out.println(article);

        }
    }

1.6 结果高亮显示

1.6.1 概念

  • 高亮:在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮。
  • 高亮的本质:其始就对检索到的结果集中包含的关键字添加HTML标签,并且通过相关样式进行修饰。

例如:

elasticSearch集群搭建---入门篇二_第7张图片

1.6.2 代码实现

 //关键字高亮显示
    @Test
    public void testQueryByHighLight(){

        //设置高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("");
        highlightBuilder.postTags("");
        highlightBuilder.field("title"); // 对哪个字段进行高亮
        //查询
        SearchResponse searchResponse = client.prepareSearch("blog3")
                .setTypes("article")
                .setQuery(QueryBuilders.termQuery("title", "如何"))
                .addSort("id", SortOrder.ASC) // 排序
                .setFrom(0)// 起始行 = (当前页码 - 1) * 每页显示的条
                .setSize(20)// 每页显示的条数
                .highlighter(highlightBuilder)// 添加高亮条件
                .get();

        //结果集处理
        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        for (SearchHit hit : hits) {
            HighlightField title = hit.getHighlightFields().get("title");
            Text[] fragments = title.getFragments();
            if (fragments!=null && fragments.length>0){
                System.out.println(fragments[0].toString());
            }

        }
    }
  • 效果如下:

elasticSearch集群搭建---入门篇二_第8张图片

2 通过Spring Data对es集成

2.1 Spring Data介绍

Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客户端API 进行封装,通过ElasticsearchTemplate操作。Spring Data为Elasticsearch项目提供集成搜索引擎。Spring Data Elasticsearch POJO的关键功能区域为中心的模型与Elastichsearch交互文档和轻松地编写一个存储索引库数据访问层。

2.2 Spring Data Elasticsearch入门

2.2.1 工程搭建

  • 创建工程:springdata_elasticSearch
    elasticSearch集群搭建---入门篇二_第9张图片

  • 添加依赖:


<dependencies>
    
    <dependency>
        <groupId>org.elasticsearchgroupId>
        <artifactId>elasticsearchartifactId>
        <version>5.6.8version>
    dependency>
    <dependency>
        <groupId>org.elasticsearch.clientgroupId>
        <artifactId>transportartifactId>
        <version>5.6.8version>
    dependency>

    
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-to-slf4jartifactId>
        <version>2.9.1version>
    dependency>
    <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-apiartifactId>
        <version>1.7.24version>
    dependency>
    <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-simpleartifactId>
        <version>1.7.21version>
    dependency>
    <dependency>
        <groupId>log4jgroupId>
        <artifactId>log4jartifactId>
        <version>1.2.12version>
    dependency>

    
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-testartifactId>
        <version>5.0.8.RELEASEversion>
    dependency>

    
    <dependency>
        <groupId>org.springframework.datagroupId>
        <artifactId>spring-data-elasticsearchartifactId>
        <version>3.0.7.RELEASEversion>
    dependency>
dependencies>

2.2.2 增加索引数据

1、创建索引

2、创建映射

3、向索引库中添加文档数据

springdata集成es ElasticsearchTemplate

1、创建pojo:

  • 添加相关es注解

2、创建dao:需要继承ElasticsearchRepository

3、编写service:

4、编写spring.xml文件:管理 ElasticsearchTemplate

2.2.2.1 创建pojo

在工程的src目录下创建Article。

package com.demo.pojo;

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 = "blog4",type = "article")
public class Article {
    @Id
    private Integer id;
    @Field(index = true,store = false,analyzer = "ik_smart",searchAnalyzer = "ik_smart",type = FieldType.Text)
    private String title;
    @Field(index = true,store = false,analyzer = "ik_smart",searchAnalyzer = "ik_smart",type = FieldType.Text)
    private String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer 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;
    }

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

注解说明

@Document(indexName=“blob4”,type=“article”): 文档与pojo映射
indexName:索引的名称(必填项),type:索引的类型

@Id:主键的唯一标识
@Field(index=true,store=true,analyzer=“ik_smart”,searchAnalyzer=“ik_smart”,type =FieldType.Text)
index:是否设置分词
store:是否存储,默认值是false。如果默认设置为false,Elasticsearch默认使用_source存放我们数据内容
analyzer:存储时使用的分词器
searchAnalyze:搜索时使用的分词器
type: 数据类型

2.2.2.2 编写dao接口

在工程src目录下,创建ArticleDao接口,需要继承ElasticsearchRepository.

package com.demo.dao;

import com.demo.pojo.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ArticleDao extends ElasticsearchRepository<Article,Integer> {
    
}

2.2.2.3 编写service接口以及实现类

  • 编写ArticleService接口
package com.demo.service;

import com.demo.pojo.Article;

public interface ArticleService {
    //添加数据
    void save(Article article);
}

  • 编写ArticleServiceImpl实现类
package com.demo.service.impl;

import com.demo.dao.ArticleDao;
import com.demo.pojo.Article;
import com.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;
    
    @Override
    public void save(Article article) {
        articleDao.save(article);
    }
    
}

2.2.2.4 编写spring.xml文件

在resources目录下创建spring.xml文件:


<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">

    
    <context:component-scan base-package="com.demo.service"/>
    
    <elasticsearch:repositories base-package="com.demo.dao" />
    
    <elasticsearch:transport-client id="client" cluster-nodes="localhost:9300" cluster-name="elasticsearch" />
    
    <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        
        <constructor-arg name="client" ref="client"/>
    bean>
beans>

2.2.2.5 单元测试

在test目录下创建测试类:

package com.demo;

import com.demo.pojo.Article;
import com.demo.service.ArticleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(locations = {"classpath:spring.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class EsTemplateDemo {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    // 创建索引和映射
    @Test
    public void testCreateIndexAndMapping(){
        //创建索引
        elasticsearchTemplate.createIndex(Article.class);
        elasticsearchTemplate.putMapping(Article.class);
    }

    //测试添加数据
    @Test
    public void testSave(){
        Article article = new Article();
        article.setId(1);
        article.setTitle("什么是es");
        article.setContent("Elasticsearch是一个全文检索系统,并且是基于lucene开发");
        articleService.save(article);
    }
    
}

elasticSearch集群搭建---入门篇二_第10张图片

2.3 对索引数据维护-CRUD

2.3.1 编写service接口以及实现类

  • 编写service接口:在接口中添加其他方法
public interface ArticleService {

    // 创建索引数据
    void save(Article article);

    // 批量保存
    void saveAll(List<Article> articles);

    // 根据id删除:通过pojo封装条件
    void delete(Article article);

    // 根据id删除
    void deleteById(Integer id);

    // 查询所有
    Iterable<Article> findAll();

    // 分页查询
    Page<Article> findAll(Pageable pageable);
}
  • 编写实现类:实现接口中的相关方法
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    // 保存索引数据
    public void save(Article article) {
        articleDao.save(article);
    }

    // 批量保存
    public void saveAll(List<Article> articles) {
        articleDao.saveAll(articles);
    }

    // 根据id删除:通过pojo封装条件
    public void delete(Article article) {
        articleDao.delete(article);
    }

    // 根据id删除
    public void deleteById(Integer id) {
        articleDao.deleteById(id);
    }

    // 查询所有
    public Iterable<Article> findAll() {
        return articleDao.findAll();
    }

    public Page<Article> findAll(Pageable pageable) {
        return articleDao.findAll(pageable);
    }
}

2.3.2 编写单元测试

在测试类中添加相关方法:

package com.demo;

import com.demo.pojo.Article;
import com.demo.service.ArticleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;

@ContextConfiguration(locations = {"classpath:spring.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class EsTemplateDemo {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    // 创建索引和映射
    @Test
    public void testCreateIndexAndMapping(){
        //创建索引
        elasticsearchTemplate.createIndex(Article.class);
        elasticsearchTemplate.putMapping(Article.class);
    }

    //测试添加数据
    @Test
    public void testSave(){
        Article article = new Article();
        article.setId(1);
        article.setTitle("什么是es");
        article.setContent("Elasticsearch是一个全文检索系统,并且是基于lucene开发");
        articleService.save(article);
    }

    //测试批量添加
    @Test
    public void testSaveAll(){
        ArrayList<Article> articles = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            Article article = new Article();
            article.setId(i);
            article.setTitle("es版本"+i);
            article.setContent("spring data elasticsearch可以简化原生api操作:" + i);
            articles.add(article);
        }
        articleService.saveAll(articles);
    }

    @Test
    public void update(){
        Article article = new Article();
        article.setId(1);
        article.setTitle("es版本更新1");
        article.setContent("spring data elasticsearch可以简化原生api操作更新");
        articleService.save(article);
    }

    //根据id删除
    @Test
    public void delete(){
        Article article = new Article();
        article.setId(1);
        articleService.delete(article);
    }

    //根据id删除
    @Test
    public void deleteById(){
        articleService.deleteById(1);
    }

    //查询所有
    @Test
    public void findAll(){
        Iterable<Article> articles = articleService.findAll();
        articles.forEach(article -> System.out.println(article));

    }

    //分页查询
    @Test
    public void findByPage(){
        //new PageRequest(getPageNumber() + 1, getPageSize(), getSort());
        PageRequest pageRequest = new PageRequest(0, 10, Sort.Direction.ASC, "id");
        Page<Article> articles = articleService.findByPage(pageRequest);
        System.out.println(articles.getTotalPages());//总页数
        System.out.println(articles.getTotalElements());//总条数
        List<Article> list = articles.getContent();
        for (Article article : list) {
            System.out.println(article);
        }
    }
}

2.4 常用查询命名规则

关键字 命名规则 解释 示例
and findByField1AndField2 根据Field1和Field2获得数据 findByTitleAndContent
or findByField1OrField2 根据Field1或Field2获得数据 findByTitleOrContent
is findByField 根据Field获得数据 findByTitle
not findByFieldNot 根据Field获得补集数据 findByTitleNot
between findByFieldBetween 获得指定范围的数据 findByPriceBetween
lessThanEqual findByFieldLessThan 获得小于等于指定值的数据 findByPriceLessThan

2.4.1 需求

  • 根据标题查询
  • 根据标题查询并且分页

2.4.2 编写dao

在dao中添加方法:

public interface ArticleDao extends ElasticsearchRepository<Article, Integer> {

    // 根据标题查询
    List<Article> findByTitle(String title);

    // 根据标题查询并且分页
    Page<Article> findByTitle(String title, Pageable pageable);

}

2.4.3 编写service

  • service接口:在接口中添加方法
// 根据标题查询
List<Article> findByTitle(String title);

// 根据标题查询并且分页
Page<Article> findByTitle(String title, Pageable pageable);
  • service实现类:实现接口中方法
// 根据标题查询
public List<Article> findByTitle(String title) {
    return articleDao.findByTitle(title);
}

// 根据标题查询并且分页
public Page<Article> findByTitle(String title, Pageable pageable) {
    return articleDao.findByTitle(title, pageable);
}

2.4.4 单元测试

在测试类中添加方法:

// 根据标题查询
    @Test
    public void testFindByTitle(){
        Iterable<Article> articles = articleService.findByTitle("版本");
        articles.forEach(article -> System.out.println(article));
    }
    // 根据标题查询 并分页
    @Test
    public void testFindByTitleAndPage(){
        Page<Article> page = articleService.findByTitle("es", (PageRequest.of(0, 10)));
        int totalPages = page.getTotalPages();
        long totalElements = page.getTotalElements();
        System.out.println("总页数:"+totalPages);
        System.out.println("总条数:"+totalElements);

        List<Article> articles = page.getContent();
        articles.forEach(article -> System.out.println(article));
    }

3 搭建ES集群

3.1 集群搭建

3.1.1 创建目录

在磁盘的任意盘符下创建es-cluster目录,并且在该目录下创建三个文件夹,分别为node1,node2以及node3,如图所示:

elasticSearch集群搭建---入门篇二_第11张图片

3.1.2 复制文件

将安装好的es目录下的相关文件分别复制到node1\node2\node3目录下:(删除head插件和历史数据)

elasticSearch集群搭建---入门篇二_第12张图片

复制后结果如下:

elasticSearch集群搭建---入门篇二_第13张图片

3.1.3 修改配置

  • 修改node1节点中的node1\config\elasticsearch.yml配置文件,添加如下配置:

    #节点1的配置信息:
    #集群名称,保证唯一
    cluster.name: my-elasticsearch
    #节点名称,必须不一样
    node.name: node-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节点中的node2\config\elasticsearch.yml配置文件,添加如下配置:

    #节点2的配置信息:
    #集群名称,保证唯一
    cluster.name: my-elasticsearch
    #节点名称,必须不一样
    node.name: node-2
    #服务端口号,在同一机器下必须不一样
    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"]
    
  • 修改node1节点中的node3\config\elasticsearch.yml配置文件,添加如下配置:

    #节点3的配置信息:
    #集群名称,保证唯一
    cluster.name: my-elasticsearch
    #节点名称,必须不一样
    node.name: node-3
    #服务端口号,在同一机器下必须不一样
    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"]
    

3.1.3 启动服务

依次启动3台es服务。

elasticSearch集群搭建---入门篇二_第14张图片

elasticSearch集群搭建---入门篇二_第15张图片

elasticSearch集群搭建---入门篇二_第16张图片

elasticSearch集群搭建---入门篇二_第17张图片

elasticSearch集群搭建---入门篇二_第18张图片

3.2 测试

3.2.1 通过postman测试

  • 创建索引和映射
# url
PUT     http://localhost:9200/blog1

# 请求体
{
    "mappings": {
		"article": {
			"properties": {
				"id": {
					"type": "long",
					"store": true,
					"index":"not_analyzed"
				},
				"title": {
					"type": "text",
					"store": true,
					"index":"analyzed",
					"analyzer":"standard"
				},
				"content": {
					"type": "text",
					"store": true,
					"index":"analyzed",
					"analyzer":"standard"
				}
			}
		}
	}
}

  • 效果

elasticSearch集群搭建---入门篇二_第19张图片

elasticSearch集群搭建---入门篇二_第20张图片

  • 添加文档
# url
POST    localhost:9200/blog1/article/1

# 请求体
{
	"id":1,
	"title":"ElasticSearch是一个基于Lucene的搜索服务器",
	"content":"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTfulweb接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够时搜索,稳定,可靠,快速,安装使用方便。"
}

  • 效果

elasticSearch集群搭建---入门篇二_第21张图片

elasticSearch集群搭建---入门篇二_第22张图片

3.2.2 通过java代码测试

  • 创建索引和映射
package com.demo.test;

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
//集群客户端下创建索引
public class testCreateMappingByCluster {

    public static void main(String[] args) throws IOException {
        // 1、创建客户端并且建立连接
        Map<String, String> map = new HashMap();
        map.put("cluster.name", "my-elasticsearch");
        InetSocketTransportAddress address1 = new InetSocketTransportAddress(new InetSocketAddress("localhost", 9300));
        InetSocketTransportAddress address2 = new InetSocketTransportAddress(new InetSocketAddress("localhost", 9301));
        InetSocketTransportAddress address3 = new InetSocketTransportAddress(new InetSocketAddress("localhost", 9302));
        Client client = new PreBuiltTransportClient(Settings.builder().put(map).build())
                .addTransportAddresses(address1, address2, address3);
        // 创建映射
        client.admin().indices().prepareCreate("blog2").get();
        // 创建索引
        XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
        xContentBuilder
                .startObject()
                    .startObject("properties")
                        .startObject("id")
                            .field("type", "long")
                        .endObject()
                        .startObject("title")
                            .field("type", "text")
                            .field("store", false)
                            .field("analyzer", "ik_smart")
                        .endObject()
                        .startObject("content")
                            .field("type", "text")
                            .field("store", false)
                            .field("analyzer", "ik_smart")
                        .endObject()
                    .endObject()
                .endObject();

        PutMappingRequest mappingRequest = Requests.putMappingRequest("blog2").type("article").source(xContentBuilder);
        client.admin().indices().putMapping(mappingRequest);
        // 3、关闭资源
        client.close();
    }
}

elasticSearch集群搭建---入门篇二_第23张图片

  • 添加文档
package com.demo.test;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.util.HashMap;
// 创建文档
public class testCreateDoc {
    public static void main(String[] args) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("cluster.name","my-elasticsearch");
        Settings settings = Settings.builder().put(map).build();
        //创建客户端访问对象
        PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
        InetSocketTransportAddress address1 = new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300);
        InetSocketTransportAddress address2 = new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301);
        InetSocketTransportAddress address3 = new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302);
        client.addTransportAddresses(address1,address2,address3);
        // 创建文档
        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                    .field("id", 1)
                    .field("title", "es是什么")
                    .field("content", "它是基于Lucene的实现的搜索服务器")
                .endObject();

        // 添加文档到指定索引库
        client.prepareIndex("blog2","article","1").setSource(builder).get();
        client.close();
    }
}

elasticSearch集群搭建---入门篇二_第24张图片

elasticSearch集群搭建---入门篇二_第25张图片

3.3 SpringDataElasticsearch配置


<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
      ">

    
    <context:component-scan base-package="com.demo.service" />

    
    <elasticsearch:repositories base-package="com.demo.dao"/>

    
    <elasticsearch:transport-client id="client" cluster-nodes="localhost:9301,localhost:9302,localhost:9303" cluster-name="my-elasticsearch"/>

    
    <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client">constructor-arg>
    bean>
beans>

你可能感兴趣的:(elasticSearch,elasticsearch,客户端操作es,Spring,Data集成es,es集群,集群的配置文件)