小工具知识网

当前位置: 首页 > 行业知识 > Elasticsearch简介

行业知识

Elasticsearch简介

2026-11-16 07:45:31 来源:互联网转载

���录

1.安装ES及相关插件

elasticsearch head

kibana 组件

ik分词器

2.ES介绍

倒排索引

3.索引库操作

4.文档操作

5.springboot集成ES

 索引库操作

文档操作


        Elasticsearch 为各种数据类型提供接近实时的搜索和分析。不论你有结构化或非结构化的文本、数字数据,还是地理空间数据,Elasticsearch 能以支持快速搜索的方式高效地存储和索引它。你可以远超简单数据检索和聚合信息的方式去发现你数据中的趋势和模式。而且,随着你数据和查询量的增长,Elasticsearch 分布式的特性允许你的部署能随着它无缝地增长匹配。简单来说Elasticsearch用户大量数据的站内搜索

1.安装ES及相关插件

https://www.elastic.co/cn/downloads/elasticsearchhttps://www.elastic.co/cn/downloads/elasticsearch

默认下载的为最新版本本次使用为7.6.1版本,解压完成后在bin目录下启动elasticsearch.bat 

elasticsearch head

需要提前下载node环境,github 下载: https://github.com/mobz/elasticsearch-head/,并解决跨域访问问题。在 config 目录中的 elasticsearch.yml 文件中加入下面配置代码即可

# 开启跨域http.cors.enabled: true# 所有人访问http.cors.allow-origin: "*"

最后在当前目录页进入cmd环境进行npm install

kibana 组件

        Kibana 是一个针对 Elasticsearch 的开源分析及可视化平台,用来搜索、查看交互 存储在 Elasticsearch 索引中的数据,安装组件的版本要与es版本相同。

https://artifacts.elastic.co/downloads/kibana/kibana-7.6.1-windows-x86_64.ziphttps://artifacts.elastic.co/downloads/kibana/kibana-7.6.1-windows-x86_64.zip在config 目录下的 kibana.yml 文件 下添加 i18n.locale: "zh-CN"进行汉化

ik分词器

我们在进行汉语进行分词处理时不会将每个汉字都进行拆分,会将有些特点的词组当成一个词进行处理.此时显然不符合平时的搜索规则,此时需要新的规则来进行划分词组,

7.6.1 版下载

https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.1/elasticsearch- analysis-ik-7.6.1.zip

解压,在elasticsearch-7.6.1\plugins目录下创建名称为ik的文件夹,将解压后的文件复制到 ik 目录.

2.ES介绍

  • 索引:同类型文档的集合,对应mysql中的表
  • 文档:一条数据就是一个文档,es中是json格式,对应mysql表中的一条数据
  • 字段:json文档中的字段,对应mysql中的属性
  • 映射:索引中的约束,类似于mysql中的表结构

    倒排索引

    对搜索词进行划分,将每个词条的出现次数进行倒叙排序 ,而在mysql中则是将id进行排序,生成b+树,从前到后进行匹配查询词条时必须先找到文档,而后判断是否包含搜索的内容。

    3.索引库操作

    # 新增索引库PUT /news{  "mappings":{    "properties":{      "id":{        "type":"keyword",        "index":false      },      "title":{        "type":"text",        "analyzer":"ik_max_word"      }    }  }}# 查询索引库GET /news# 删除索引库DELETE /news# 修改索引库,由于创建后无法修改原索引,所以只能通过新建文档来实现修改PUT /news/_mapping{  "properties":{    "count":{      "type":"long"    }  }}

    4.文档操作

    # 新增文档POST /news/_doc/1{  "id":1,  "count":100,  "title":"111120241111"}# 查询文档GET /news/_doc/1# 更新文档POST /news/_update/1{  "doc":{    "count":200  }}# 搜索文档GET /news/_search{  "query": {    "match": {      "title": "2024"    }  }}

    5.springboot集成ES

    指定版本,版本必须与安装的 ES 版本一致

        1.8    7.6.1

    添加依赖

        org.elasticsearch.client    elasticsearch-rest-high-level-client

     初始化配置类

    @Configurationpublic class ElasticSearchConfig {    @Bean    public RestHighLevelClient restHighLevelClient(){        RestHighLevelClient client = new RestHighLevelClient(                RestClient.builder(                        new HttpHost("localhost", 9200, "http")));        return client;    }}

     索引库操作

    1.创建索引库

    CreateIndexRequest createIndexRequest = new CreateIndexRequest("users");restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);

    2.判断是否存在

    GetIndexRequest request = new GetIndexRequest("users");boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);

    3.删除索引库

    DeleteIndexRequest indexRequest = new DeleteIndexRequest("users");AcknowledgedResponse delete = restHighLevelClient.indices().delete(indexRequest, RequestOptions.DEFAULT);delete.isAcknowledged();//返回 true 删除成功,返回 false 删除失败

    文档操作

    1.添加文档

    News news = new News();news.setId(1);news.setTitle("1111112024111111");UpdateRequest updateRequest = new UpdateRequest("news",news.getId().toString());updateRequest.doc(new ObjectMapper().writeValueAsString(news), XContentType.JSON);restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);

    2.查询文档

    GetRequest getRequest = new GetRequest("news","1");GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);//获取查询的内容,返回 json 格式String json = getResponse.getSourceAsString();//使用 jackson 组件将 json 字符串解析为对象News news = new ObjectMapper().readValue(json, News.class);

    3.删除文档

    DeleteRequest deleteRequest = new DeleteRequest("news","1");DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);

    4.搜索文档

    SearchRequest searchRequest = new SearchRequest("news");//精确条件查询searchRequest.source().query(QueryBuilders.termQuery("title","2024"));//发送查询请求SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//接收查询结果SearchHits hits = search.getHits();//组装查询结果ArrayList list = new ArrayList();//取出结果集for (SearchHit searchHit : hits.getHits()){String json = searchHit.getSourceAsString();News news = new ObjectMapper().readValue(json,News.class);list.add(news);}

    5.高亮显示

    searchRequest.source().highlighter(newHighlightBuilder().field("title").requireFieldMatch(false));//组装查询结果ArrayList list = new ArrayList();for (SearchHit searchHit : hits.getHits()){String json = searchHit.getSourceAsString();News news = new ObjectMapper().readValue(json,News.class);//获取高亮名称Map highlightFields = searchHit.getHighlightFields();HighlightField titleField = highlightFields.get("title");String highttitle = titleField.getFragments()[0].string();news.setTitle(highttitle);//用添加了高亮的标题替换原始文档标题内容list.add(news);}

     6.前端高亮显示

    v-html进行标签的解析

上一篇:苹果手机下载限制内容的方法

下一篇:游戏软件开发全过程揭秘,从创意诞生到成功上线!