ElasticSearch

一、ElasticSearch

Elasticsearch是一个基于Apache Lucene库实现的,Restful风格的,分布式搜索和数据分析引擎。基于倒排索引技术,实现了高性能的全文检索和数据分析功能。

倒排索引:搜索速度快

1、安装ElasticSearch、Kibana、IK分词器

docker run -d \
  --name es \
  -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
  -e "discovery.type=single-node" \
  -v es-data:/usr/share/elasticsearch/data \
  -v es-plugins:/usr/share/elasticsearch/plugins \
  --privileged \
  --network fitzone\
  -p 9200:9200 \
  -p 9300:9300 \
  elasticsearch:7.12.1
docker run -d \
--name kibana \
-e ELASTICSEARCH_HOSTS=http://es:9200 \
--network=fitzone \
-p 5601:5601  \
kibana:7.12.1
docker exec -it es ./bin/elasticsearch-plugin  
install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip

2、Mapping映射属性

  • type:字段数据类型,常见的简单类型有:

    • 字符串:text(可分词的文本)、keyword(精确值)

    • 数值:longintegershortbytedoublefloat

    • 布尔:boolean

    • 日期:date

    • 对象:object

  • index:是否创建索引,默认为true

  • analyzer:使用哪种分词器

  • properties:该字段的子字段

3、索引库操作

①创建索引库

PUT /索引库名称
{
  "mappings": {
    "properties": {
      "字段名":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "字段名2":{
        "type": "keyword",
        "index": "false"
      },
      "字段名3":{
        "properties": {
          "子字段": {
            "type": "keyword"
          }
        }
      },
    }
  }
}

②修改索引库

PUT /索引库名/_mapping
{
  "properties": {
    "新字段名":{
      "type": "integer"
    }
  }
}

③查询索引库:GET /索引库名

④删除索引库:DELETE /索引库名

4、文档操作

①新增文档

POST /索引库名/_doc/文档id
{
    "字段1": "值1",
    "字段2": "值2",
    "字段3": {
        "子属性1": "值3",
        "子属性2": "值4"
    },
}

②全局修改文档

PUT /{索引库名}/_doc/文档id
{
    "字段1": "值1",
    "字段2": "值2",
}

③局部修改文档

POST /{索引库名}/_update/文档id
{
    "doc": {
         "字段名": "新的值",
    }
}

④查询文档:GET /索引库名/_doc/文档id

⑤删除文档:DELETE /索引库名/_doc/文档id

5、RestClient操作

初始化RestClient

//导依赖

    org.elasticsearch.client
    elasticsearch-rest-high-level-client
//指定版本
  
      8
      8
      7.12.1
  
@Bean
public RestHighLevelClient highLevelClient(){
    return new RestHighLevelClient(RestClient.builder(
         HttpHost.create("http://islunatic.icu:9200")
    ));
}

6、数据导入es

    @Autowired
    private ActivityService activityService;

    @Autowired
    private RestHighLevelClient client;

    @Test
    public void testA() throws IOException {
        List list = activityService.list();
        //数据导入
        BulkRequest bulkRequest = new BulkRequest("activity");
        for (Activity activity : list) {
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(activity.getId().toString()).source(JSON.toJSONString(activity),XContentType.JSON);
            //批量添加数据
            bulkRequest.add(indexRequest);
        }
        client.bulk(bulkRequest,RequestOptions.DEFAULT);
    }

 7、查询操作

//获取request
SearchRequest request = new SearchRequest("activity");
//DSL语句
Integer limit = activityPageDTO.getLimit();
Integer page = activityPageDTO.getPage();
//分页查询、高亮显示、排序操作
request.source().from(page)
                .size(limit)
                .query(QueryBuilders.matchAllQuery())
                .sort("createTime", SortOrder.ASC)
                .highlighter(SearchSourceBuilder.highlight()
                        .field("cover")
                        .preTags("")
                        .postTags(""));
SearchResponse search = client.search(request, RequestOptions.DEFAULT);
//解析响应
return handleResponse(search);
//解析数据 
 private Page handleResponse(SearchResponse response) {
     SearchHits searchHits = response.getHits();
     // 1.获取总条数
     long total = searchHits.getTotalHits().value;
     System.out.println("共搜索到" + total + "条数据");
     // 2.遍历结果数组
     SearchHit[] hits = searchHits.getHits();
     for (SearchHit hit : hits) {
        // 3.得到_source,也就是原始json文档
        String source = hit.getSourceAsString();
        // 4.反序列化并打印
        JSON.parseObject(source, Activity.class);
    }
     return null;
}

你可能感兴趣的:(中间件,elasticsearch,大数据,搜索引擎)