Elasticsearch客户端工具

ElasticSearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用ElasticSearch的水平伸缩性,能使数据在生产环境变得更有价值。ElasticSearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elastic Search 数据库中,再通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据,当用户搜索数据时候,再根据权重将结果排名,打分,再将返回结果呈现给用户。

引入工具包

<dependency>
    <groupId>com.wueasy</groupId>
    <artifactId>wueasy-data-elasticsearch</artifactId>
    <version>最新版本</version>
</dependency>

3.4使用

连接配置

  • hosts: elasticsearch连接地址配置,多个逗号分隔
wueasy:
  data:
    elasticsearch:
      demo: #环境key配置
        hosts: http://localhost:9200

启动类

启动类上,要禁止elasticsearch自动注入。ElasticsearchDataAutoConfiguration.class

@SpringBootApplication(exclude={ElasticsearchDataAutoConfiguration.class,CassandraDataAutoConfiguration.class,MongoAutoConfiguration.class,MongoDataAutoConfiguration.class,DruidDataSourceAutoConfigure.class,DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,RedisAutoConfiguration.class,RedisRepositoriesAutoConfiguration.class})

java中引用redis客户端

通过@Resource注解注入,并指定name属性。

  • name说明:规则由elasticsearch配置的连接key + ElasticsearchRestTemplate组成
@Resource(name = "demoElasticsearchRestTemplate")
private ElasticsearchRestTemplate demoElasticsearchRestTemplate;

@Resource(name = "demo2ElasticsearchRestTemplate")
private ElasticsearchRestTemplate demo2ElasticsearchRestTemplate;

使用示例

package com.wueasy.demo.bus.pojo;

import java.io.Serializable;

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;

import lombok.Data;

/**
 * 常见问题
 * @author: fallsea
 * @version 1.0
 */
@Document(indexName = "faq_index")
@Data
public class FaqES implements Serializable {

    private static final long serialVersionUID = -3752223268310508581L;

    /**
     * id
     */
    @Id
    private String id;

    /**
     * 问题id
     */
    @Field(store = true, type = FieldType.Long)
    private Long faqId;

      /**
     * 分类id
     */
    @Field(store = true, type = FieldType.Long)
    private Long categoryId;

      /**
     * 问题标题
     */
    @Field(store = true, type = FieldType.Text)
    private String faqTitle;

    /**
       * 问题摘要
       */
    @Field(store = true, type = FieldType.Text)
    private String faqSummary;

      /**
     * 问题内容
     */
      @Field(index = false,store = true, type = FieldType.Text)
    private String faqContent;

}
import javax.annotation.Resource;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.wueasy.Application;
import com.wueasy.base.util.JsonHelper;
import com.wueasy.demo.bus.pojo.FaqES;

/**
 * @author: fallsea
 * @version 1.0
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class ElasticsearchTests3 {


    @Resource(name = "demoElasticsearchRestTemplate")
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 保存对象
     * @author: fallsea
     */
    @Test
    public void saveObj() {

        try {
            FaqES faqES = new FaqES();
            faqES.setId("123");
            faqES.setFaqId(5L);
            faqES.setFaqTitle("4444");
            elasticsearchRestTemplate.save(faqES,IndexCoordinates.of("faq_index"));

//            elasticsearchRestTemplate.save(entity, index)
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Test
    public void find() {

        try {
            NativeSearchQuery query = new NativeSearchQuery(new BoolQueryBuilder());
            SearchHits<FaqES> list = elasticsearchRestTemplate.search(query, FaqES.class, IndexCoordinates.of("faq_index"));
            for (SearchHit<FaqES> searchHit : list.toList()) {
                System.err.println(JsonHelper.toJsonString(searchHit.getContent()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

3.x使用

连接配置

连接配置和spring-boot-starter-data-elasticsearch配置一致

spring:
  data:
    elasticsearch:
      cluster-nodes: 127.0.0.1:9300
      cluster-name: elasticsearch

客户端能使用

请参考spring-boot-starter-data-elasticsearch使用方式。

Copyright © wueasy.com 2017-2020 all right reserved,powered by Gitbook未经允许,禁止以任何形式传播 修订时间: 2020-06-24

results matching ""

    No results matching ""