您好,欢迎访问一九零五行业门户网

springboot集成elasticsearch7(springboot集成elasticsearch6.5.4)

本文主要介绍spring boot integrated elastic search 7(spring boot integrated elastic search 6 . 5 . 4),下面一起看看spring boot integrated elastic search 7(spring boot integrated elastic search 6 . 5 . 4)相关资讯。
首先,概述一下什么是elasticsearch?
elasticsearch,简称es,是一个开源的、高度可扩展的分布式全文搜索引擎。
它可以近乎实时地存储和检索数据;它的扩展性非常好,可以扩展到上百台服务器处理pb级数据。
es也是用java开发的,以lucene为核心实现所有的索引和搜索功能,但其目的是通过一个简单的restful api隐藏lucene的复杂性,从而使全文搜索变得简单。
知道了es是什么之后,es的核心概念就需要知道es是如何存储数据的,数据结构是什么,如何实现搜索。
在学习这些之前,你需要了解elasticsearch的一些相关概念。
elasticsearch是一个面向文档的数据库。
相信学过mysql的同学都知道mysql是关系数据库,那么es和关系数据库有什么区别呢?
让 让我们做一个简单的比较:
关系数据库(mysql、oracle等。)elasticsearch数据库(数据库)索引(表)类型(行)文档(列)字段:elasticsearch(集群)可以包含多个索引(数据库)。,每个索引可以包含多个类型(表),每个类型包含多个文档(行),每个文档包含多个字段(列)。
物理设计:
elasticsearch在后台将每个索引分成多个切片,每个切片可以在集群中的不同服务器之间迁移。
逻辑设计:
一个索引类型包含多个文档,如文档1、文档2和文档3。
当我们索引一个文档时,我们可以通过这样的顺序找到它的:索引类型文档id,并且我们可以通过这种组合索引一个特定的文档。注意:id不 不必是整数,但它实际上是一个字符串。
指数
索引是映射类型的容器,而elasticsearch中的索引是非常大的文档集合。索引存储映射类型的字段和其他设置。然后它们被存储在每个片上。让 让我们研究切片是如何工作的。
物理设计:节点和片段如何工作
一个集群至少有一个节点,一个节点就是一个elasricsearch。默认情况下,一个节点可以有多个索引。如果您创建一个索引,该索引将由五个主碎片组成,并且每个主碎片都有一个副本。
上图是一个有三个节点的集群。可以看出,主碎片和对应的副本碎片都不会在同一个节点,有利于节点死亡和数据丢失。事实上,片段是一个lucene索引,一个包含倒排索引的文件目录。倒排索引的结构使elasticsearch能够告诉您哪些文档包含特定的关键字,而无需扫描所有文档。其中,什么是倒排索引?
倒排索引
elasticsearch使用了一种叫做倒排索引的结构,底层是luc:中。
$ term doc _ 1 doc _ 2 study√xtoxxeevery√永远√87
$ term doc _ 1 doc _ 2 to√×for:。
如果要搜索带python标签的文章,查找倒排索引数据会比查找所有原始数据快很多。只需查看标签列并获得相关的文章id。
弹性搜索指数与lucene指数的比较
在elasticsearch中,索引这个词的使用频率很高,它是术语的使用。。在elasticsearch中,索引分为多个段,每个段都是lucene的一个索引。所以一个elasticsearch索引是由几个lucene索引组成的。
类型
类型是文档的逻辑容器,就像关系数据库一样,表是行的容器。类型中字段的定义叫做映射,比如名称映射到字符串类型。
我们说文档是无模式的,它们不。;不需要在地图中定义所有的字段,比如添加一个新字段,那么elasticsearch是如何做到的呢?elasticsearch会自动将新字段添加到地图中,但如果字段不确定是什么类型,elasticsearch就会开始猜测。如果值是18,elasticsearch会认为是塑料。但是elasticsearch可能是错误的,所以最保险的方法是提前定义需要的映射,这和关系数据库是一样的。首先定义字段,然后使用它们。
文件
elasticsearch是面向文档的,这意味着索引和搜索数据的最小单位是文档。
在弹性搜索中,文档有几个重要的属性,:。
自带,一个文档既包含字段又包含对应的值,也就是说还包含k:value!它可以是分层的,一个文档包含一个自文档,并且 逻辑实体是如何复杂的!灵活的结构,文件不 不依赖于预定义的模式。我们知道,在关系数据库中,字段只有在预先定义的情况下才能使用。在弹性搜索中,字段非常灵活。有时候,我们可以忽略这个字段,或者动态添加一个新字段。虽然我们可以随意添加或忽略一个字段,但是每个字段的类型都很重要,比如一个年龄字段类型,可以是字符串,也可以是塑料。因为elasticsearch会保存字段和类型之间的映射以及其他设置。这种映射特定于每种映射的每种类型,这就是为什么在elasticsearch中,类型有时被称为映射类型。
二、-analysis-ik/(版本要对应)
2.下载后解压并将目录复制到elasticsearch根目录下的plugins目录。
3.重新启动elasticsearch服务。在启动过程中,您可以看到提示消息 分析-ik 正在加载插件。服务启动后,在命令行上运行elasticsearch-plugin list命令,确认ik插件安装成功。
ik提供了ik_smart和ik_max_word两种分词算法,其中ik_smart是最少切分,ik_max_word是最细粒度切分!
ik_max_word:细粒度的分词会穷尽一个句子中所有的分词可能性。ik_smart:粗粒度分词,优先匹配最长的词,只有一个词!例如,如果一些单词不存在于默认词库中,我们需要 我热爱学习要被识别为一个单词,那么我们需要编辑自定义词库。
步骤:
(1)进入弹性搜索/插件/ik/config目录。
(2)创建一个新的my.dic文件并编辑内容:
我爱学习(3)修改ikanalyzer.cfg.xml(在ik/config目录下)。
propertiescommentik分析器扩展配置/注释!-用户可以在这里配置自己的扩展字典ext _ dict my.dic/entry!-用户可以在这里配置他们自己的扩展停用词词典ext _ stopwords /entry/properties注意:修改配置后,需要重启elasticsearch。
添加、删除和检查基本命令rest样式描述
软件架构风格,而不是标准,只提供了一套设计原则和约束。它主要用于客户端和服务器之间的交互软件。基于这种风格设计的软件可以更简洁,层次更分明,更容易实现缓存等机制。
基本rest命令描述(添加、删除、修改和搜索命令):
methodur地址描述putlocalhost:9200/索引名/类型名/文档id创建文档(指定文档id)post localhost:9200/索引名/类型名创建文档(随机文档id)post localhost:9200/索引名/类型名/文档id/_update修改文档。delete localhost:9200//搜索名称/类型名称/文档id删除文档get localhost:9200/索引名称/类型名称/文档id查询文档并通过文档idpostloclhost:9200/索引名称/类型名称/_search iii查询所有数据。springboot integration es1。创建一个新项目,并创建一个spring boot(版本2.2.5)项目弹性搜索。
2.配置相关性配置弹性搜索的相关性:
properties java . version 1.8/java . version!-这里springboot默认配置的版本不匹配,需要我们自己配置版本!-elastic search . version 7 . 6 . 1/elastic search . version/propertiesdependencygroupidorg . spring framework . boot/groupid artifactid spring-boot-starter-data-elastic search/artifact id/dependency 3。编写elasticsearch的配置类,提供bean resthighlevelclient进行操作。
包com . hzx . config;导入org . apache . http . httphost;导入org . elastic search . client . rest client;导入org . elastic search . client . resthighlevelclient;导入org . spring framework . context . annotation . bean;导入org . spring framework . context . annotation . configuration;@configurationpublic类elasticsearchclientconfig { @ bean public resthighlevelclient resthighlevelclient{ resthighlevelclient = new resthighlevelclient(rest client . builder(new httphost( 127.0.0.1,9200, http )));回访客户;} }4.配置工具类封装了es通用方法工具类。
包com . hzx . utils;导入com . alibaba . fast json . json;import org . elastic search . action . admin . indexes . delete . deleteindexrequest;导入org . elastic search . action . bulk . bulk request;导入org . elastic search . action . bulk . bulk response;导入org . elastic search . action . delete . delete request;导入org . elastic search . action . delete . delete response;导入org . elastic search . action . get . get request;导入org . elastic search . action . get . getresponse;导入org . elastic search . action . index . index request;导入org . elastic search . action . index . index response;导入org . elastic search . action . search . search request;导入org . elastic search . action . search . search response;导入org . elastic search . action . support . master . acknowledged response;导入org . elastic search . action . update . update request;导入org . elastic search . action . update . update response;导入org . elastic search . client . request options;导入org . elastic search . client . resthighlevelclient;导入org.elasticsearch . client . indexes . createindexrequest;导入org . elastic search . client . indexes . createindexresponse;导入org . elastic search . client . indexes . getindexrequest;导入org . elastic search . common . unit . time value;导入org . elastic search . common . x content . xcontenttype;导入org . elastic search . index . query . query builders;导入org . elastic search . rest . rest status;导入org . elastic search . search . builder . search source builder;导入org . elastic search . search . fetch . sub phase . fetchsourcecontext;导入org . spring framework . beans . factory . annotation . auto wired;导入org . spring framework . beans . factory . annotation . qualifier;导入org . spring framework . stereotype . component;导入java . io . io exception;导入java . util . list;导入java . util . concurrent . time unit;@ component public class e sutilst { @ auto wired @ qualifier( resthighlevelclient )私有resthighlevelclient客户端;/* * *判断索引是否存在* * @ param index * @ return * @ throwsioexception */public boolean exists index(string index)throwsioexception { getindexrequestst = new getindexrequest(index);boolean exists = client.indices。存在(请求,请求选项。默认);退货存在;}/* * * create index * * @ param index * @ throwsioexception */public boolean create index(string index)throwsioexception { createindexrequest = newcreateindexrequest(index);createindexresponse createindexresponse = client . indexes。创建(请求,请求选项。默认);返回createindexresponse . isacknowledged;}/* * * delete index * * @ param index * @ return * @ throwsioexception */public boolean delete index(string index)throwsioexception { deleteindexrequest deleteindexrequest = new deleteindexrequest(index);acknowledgedresponse response = client . indexes。delete(deleteindexrequest,requestoptions。默认);返回response . isackknowledged;}/* * *确定某个索引下的文档id是否存在* * @ param index * @ param id * @ return * @ throwsioexception */public boolean doc exists(string index,string id)抛出io exception { getrequest getrequest = new getrequest(index,id);//只判断索引是否存在。您需要获取_ sourcegetrequest。fetchsourcecontext(new fetchsourcecontext(false));get request . stored fields( _ none _ );boolean exists = client . exists(get request,requestoptions。默认);退货存在;}/* * *添加文档记录* * @ paramindex * @ paramid * @要添加的paramtdata实体类* @ return * @ throwsioexception */public boolean add doc(stringindex,stringind,t t)抛出io exception { index request request = new index request(index);request . id(id);//time out request . time out(time value . time value seconds(1));request . time out( 1s );request . source(json . tojsonstring(t),xcontenttype。json);indexresponse indexresponse = client . index(request,requestoptions。默认);reststatus status = index response . status;返回状态== reststatus。ok || status == reststatus。已创建;}/* * *根据id获取记录* * @ param index * @ param id * @ return * @ throwsioexception */public getresponse getdoc(string index,string id)抛出io exception { getrequest request = new getrequest(index,id);getresponse getresponse = client . get(请求,r等式选项。默认);返回getresponse}/* * *批量添加单据记录*如果没有设置id es,系统会自动生成一条。如果要设置对象。indexrequest的id,可以* * @ param index * @ param list * @ return * @ throwsioexception */public boolean bulk add(string index,listt list)抛出io exception { bulk request bulk request = new bulk request;//time out bulk request . time out(time value . time value minutes(2));bulk request . time out( 2m );for(int i = 0;i list . size;i){ bulk request . add(new index request(index))。source(json . tojsonstring(list . get(i))));} bulkresponse bulkresponse =客户端. bulk(bulkrequest,requestoptions。默认);回归!bulk response . has failures;}/* * *更新文档记录* @ param index * @ paramid * @ paramt * @ return * @ throwsioexception */public boolean updatedoc(string index,stringinid,t t)抛出io exception { update request request = new update request(index,id);request . doc(json . tojsonstring(t));request . time out(time value . time value seconds(1));request . time out( 1s );update response update response = client . update(请求,请求选项。默认);返回update response . status= = rest status。ok;}/* * *删除文档记录* * @ param index * @ paramid * @ return * @ throwsioexception */public boolean deleted doc(string index,string id)抛出io exception { delete request request = new delete request(index,id);//time out request . time out(time value . time value seconds(1));request . time out( 1s );delete response delete response = client . delete(request,requestoptions。默认);返回delete response . status= = rest status。ok;}/* * *根据某个字段进行搜索* * @ paramindex * @ paramfield * @要搜索的paramkey关键字* @ throwsioexception */public void search(string index,stringfield,string key,integer from,integer size)抛出io exception { search request search request = new search request(index);search source builder source builder = new search source builder;source builder . query(query builders . term query(field,key));//控制搜索元素source builder . from(from);sourcebuilder.size(大小);//最大搜索时间。sourcebuilder.timeout(新时间值(60,时间单位。秒));搜索请求est . source(source builder);search response search response = client . search(search request,requestoptions。默认);system . out . println(json . tojsonstring(search r测试测试创建了一个索引:
@testvoid testcreateindex抛出io exception { createindexrequest request = new createindexrequest( 测试索引 );createindexresponse createindexresponse = resthighlevelclient . indexes。创建(请求,请求选项。默认);system . out . println(cr测试得到指数:
@testvoid testexistsindex抛出io exception { getindexrequest request = new getindexrequest( 测试索引 );boolean exists = resthighlevelcli测试删除索引:
@ test void testdeleteindexrequest抛出io exception { deleteindexrequest deleteindexrequest = new deleteindexrequest( 测试索引 );acknowledgedresponse response = resthighlevelclient . indexes。delete(deleteindexrequest,requestoptions。默认);system . out . println(response . isacknowl测试补充文件记录:
创建实体类用户。
@ data @ allargsconstructor @ noargsconstructor @ component public class us测试添加文档记录。
@ test void testadddocumentthrowsioexception {//创建对象用户用户=新用户( 张三 , 3);//create request index request = new index request( 测试索引 );//rule request . id( 1 );request . time out(time value . time value seconds(1));request . time out( 1s );request . source(json . tojsonstring(user),xcontenttype。json);//发送请求indexresponse indexresponse = resthighevelcli测试:判断某个索引下的文档id是否存在。
@ testvoid testisexists抛出io exception { get request get request = new get request( 测试索引 , 1 );//唐 t获取_source上下文存储字段getrequest。fetchsource上下文(新的fetchsource上下文xt(false));get request . stored fields( _ none _ );//判断该id是否存在!boolean exists = resthighlevelcli测试:根据id获取文档记录。
@testvoid testgetdocument抛出io exception { get request get request = new get request( 测试索引 , 3 );getresponse getresponse = resthighlevelclient . get(getrequest,requestoptions。默认);//打印文档内容系统。out . println(getresponse . getsourc测试:更新文档记录。
@testvoid testupdatedocument抛出io exception { update request request = new update request( 测试索引 , 1 );request . time out(time value . time value seconds(1));request . time out( 1s );用户user =新用户( 张三 , 18);request . doc(json . tojsonstring(user),xcontenttype。json);update response update response = resthighlevelcli测试:删除文档记录。
@testvoid testdelete抛出ioexception { delete request request = new delete request( 测试索引 , 3 );//time out request . time out(time value . time value seconds(1));request . time out( 1s );delete response delete response = resthighlevelcli测试:批量添加文档。
@testvoid testbulkrequest抛出io exception { bulk request bulk request = new bulk request;//time out bulk request . time out(time value . time value minutes(2));bulk request . time out( 2m );arraylistuser userlist = new arraylist;userlist.add(新用户( 张三 ,3));userlist.add(新用户( 张三 ,3));userlist.add(新用户( 张三 ,3));userlist.add(新用户( lisi1 ,3));userlist.add(新用户( lisi2 ,3));userlist.add(新用户( lisi3 ,3));for(int i = 0;iuser list . size;i){ bulk request . add(new index request( 测试索引 ).id( (i 1))。source(json . tojsonstring(userlist . get(i)),xcontenttype。json));}//bulk bulk response bulk response = resthighlevelcli测试:
/* * *使用查询生成器* $ term查询( 钥匙和钥匙,obj)来精确匹配* terms query( 钥匙和钥匙,obj1,obj2...)一次匹配多个值* match query( 钥匙和钥匙,obj)进行单匹配,字段不支持通配符,前缀具有高级特性* multimatchquery( 文本 ,对象2..).匹配多个字段,字段有通配符te line * matchallquery;匹配所有文件*/@ test void test searchthrowsioexception { searchrequestsearchrequest = new search request( 测试索引 );search source builder source builder = new search source builder;//termquerybuilder termquerybuilder = query builders . term query( 姓名和名称, 张三 );matchallquerybuilder matchallquerybuilder = query builders . matchallquery;source builder . query(matchallquerybuilder);sourcebuilder.timeout(新时间值(60,时间单位。秒));search request . source(source builder);search response response = resthighlevelclient . search(search request,requestoptions。默认);system . out . println(json . tojsonstring(response . get hits));系统。out . println( = = = = = = = = = = = =查询突出显示= = = = = = = = == = = = = = = = = = = );for(search hit docum:响应. gethits。get hits){ system . out . println(document fields . getsourceasmap);}}标签:
文件索引
了解更多spring boot integrated elastic search 7(spring boot integrated elastic search 6 . 5 . 4)相关内容请关注本站点。
其它类似信息

推荐信息