ElasticSearch

概述

  • 分布式全文搜索引擎

    • 传统用sql,对于大量数据很慢
    • 可以当做一个数据库,可以将里面的索引就是库
  • Lucene 用于文本搜索的函数库,java编写的,全文搜索功能 ,jar包,不包含搜索引擎

    • 索引结构
    • 读写索引,排序,搜索规则
  • image-20210129162516427

  • elasticsearch是基于lucene做了一些封装和增强

  • solr

    • 封装了lucene,可独立运行
    • 通过发送请求可以操作数据
    • 有管理界面,
  • image-20210129164623442

安装

目录

  • bin
  • config
    • jvm配置
    • log4j2
    • elasticSearch.yml
      • 集群
      • 端口:9200
      • http.cors.enabled: true 设置允许跨域,让界面插件访问
        • http.cors.allow-origin: "*"
  • lib 相关架包
  • logs 日志
  • modules 功能模块
  • plugins 插件,例如IK分词器

界面插件

  • elasticsearch head :前端项目,需要npm
    • 下载
    • npm install
    • npm run start
    • 启动后连接到elasticsearch即可
  • 索引:可以当做数据库,
  • 文档:库中的数据
  • http://localhost:9100

Kibana

  • es的分析及可视化平台http://localhost:5601
  • 汉化:config/kibana.yml
    • i18n.locale: "zh_CN"

使用

es是面向文档的, 全部是json

image-20210129173559785

  • 索引
  • 字段索引
  • 文档

ik分词器

​ 把句子划分为关键字,会把数据库中数据分词后匹配,ik_smart最少切分,ik_max_word最细粒度切分

  • 下载analysis-ik
  • 解压到es plugins下
  • 重启es

image-20210129175644428

  • 插件中建立自己的xxx.dic
  • 配置文件修改为xxx.dic

索引的增删改查

// 添加库
PUT /索引名/类型/文档名
{
  "name":"dean",
  "age":3
}
{
  "_index" : "test1",
  "_type" : "type",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

// 建索引 不放数据
PUT /test2
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "age":{
        "type":"long"
      },
      "birthday":{
        "type": "date"
      }
    }
  }
}
GET /test/type/1
POST /test/type/1/_update
{
    "doc":{
        "name":"dean2"
    }
}
DELETE test

文档操作

  • 根据属性查询

    GET /test1/type/_search
    {
      "query": {
        "match": {
          "name": "dean3"
        }
      },
       "_source": ["name"],
      "sort": [
        {
          "age": {
            "order": "desc"
          }
        }
      ],
      "from": 0,
      "size": 20
    }
    
    #! Deprecation: [types removal] Specifying types in search requests is deprecated.
    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.2876821,
        "hits" : [
          {
            "_index" : "test1",
            "_type" : "type",
            "_id" : "1",
            "_score" : 0.2876821,
            "_source" : {
              "name" : "dean3"
            }
          }
        ]
      }
    }
    
    

Springboot集成

依赖,配置,使用

@Configuration
public class ElasticSearchConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        return new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
    }
}
package top.dean0731.esapi;

import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import top.dean0731.esapi.pojo.User;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @Title: ElasticSearchTest
 * @Package top.dean0731.esapi
 * @Description: 测试类
 * @date 2021/1/30 14:22
 */
@SpringBootTest
public class ElasticSearchTest {
    @Autowired
            @Qualifier("restHighLevelClient")
    RestHighLevelClient restHighLevelClient;
    @Test
    public void test() throws IOException {
        // 索引操作
        CreateIndexRequest request = new CreateIndexRequest("es");
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }
    @Test
    public void tes2() throws IOException {
        // 索引操作
        GetIndexRequest getIndexRequest = new GetIndexRequest("es");
        boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }
    @Test
    public void test3() throws IOException {
        // 索引操作
        DeleteIndexRequest request = new DeleteIndexRequest("es");
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);

    }
    @Test
    public void test4() throws IOException {
        // 索引操作
        CreateIndexRequest request = new CreateIndexRequest("es");
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }

    // 文档操作
    @Test
    void testDocument() throws IOException {
        User user = new User("dean", 1234);
        IndexRequest index = new IndexRequest("es");
        index.id("1");
        index.timeout(TimeValue.timeValueSeconds(1));

        index.source(JSON.toJSONString(user), XContentType.JSON);

        IndexResponse index1 = restHighLevelClient.index(index, RequestOptions.DEFAULT);
        System.out.println(index1.toString());
        System.out.println(index1.status());
    }
    // 文档操作
    @Test
    void testDocument2() throws IOException {
        GetRequest doucementRequest = new GetRequest("es", "1");
        // 不获取_source的上下文
        doucementRequest.fetchSourceContext(new FetchSourceContext(false));
        doucementRequest.storedFields("_none_");
        boolean exists = restHighLevelClient.exists(doucementRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }
    // 文档操作
    @Test
    void testDocument3() throws IOException {
        GetRequest doucementRequest = new GetRequest("es", "1");
        GetResponse documentFields = restHighLevelClient.get(doucementRequest, RequestOptions.DEFAULT);
        System.out.print(documentFields.getSourceAsString());
    }
    // 更新文档操作
    @Test
    void testDocument4() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("es","1");
        updateRequest.timeout("1s");
        String z = JSON.toJSONString(new User("z", 18));
        updateRequest.doc(z,XContentType.JSON);

        restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
    }
    // 删除文档操作
    @Test
    void testDocument5() throws IOException {
        DeleteRequest request = new DeleteRequest("es","1");
        request.timeout("1s");
        restHighLevelClient.delete(request,RequestOptions.DEFAULT);
    }
    // 批量文档操作
    @Test
    void testDocument6() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");
        ArrayList<User> list = new ArrayList<>();
        list.add(new User("1",1));
        list.add(new User("2",2));
        list.add(new User("3",3));
        for (int i = 0; i < list.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("es")
                            .id(i+"")
                            .source(JSON.toJSONString(list.get(i)),XContentType.JSON));
        }
        restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
    }
    // 查询文档操作
    @Test
    void testDocument7() throws IOException {
        SearchRequest searchRequest = new SearchRequest("es");
        // 构建搜索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "1");// 精确查询
//        searchSourceBuilder.query(termQueryBuilder);
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery(); // 匹配所有
        searchSourceBuilder.query(matchAllQueryBuilder);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        searchRequest.source(searchSourceBuilder);

        SearchResponse search = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(search));
        System.out.println("===========================");
        for (SearchHit documentFields: search.getHits().getHits()) {
            System.out.println(documentFields.getSourceAsMap());
        }
    }
}

image-20210130164922789

image-20210130165329853

posted @ 2021-03-03 19:51  下雨天,真好  阅读(89)  评论(0编辑  收藏  举报