Elasticserach学习记录(一)

注意 这些所以的东西版本都要一样 ElasticSearch安装 jdk至少1.8 ElasticSearch客户端,界面工具
注意 jdk必须与cpu的位数是一样的 否则会报JNA错误
【Elasticserach学习记录(一)】下载地址
https://www.elastic.co/cn/ shearch和kibabn
https://github.com/medcl/elas... ik分词器
https://github.com/mobz/elast... head插件下载
这些都是解压可用
Elasticearch 启动 点击 bin\下的elasticsearch.bat
如果电脑内存不够大 启动前 记得去 config\jvm.options 修改
-Xms256m

-Xmx256m

访问 http://127.0.0.1:9200/
head插件 必须要有 nodejs 因为他是前端写的
  1. cnpm install 安装依赖
  2. cnpm run start
注意如果9100端口被占用
netstat -ano | findstr "9100"
然后去任务管理器详细信息里去关
启动成功访问 http://localhost:9100/
9200 和 9100 是跨域的
需要在elasticearch.yaml中配置跨域
http.cors.enabled: true http.cors.allow-origin: "*"

然后重启es服务
head就相当于navicat
kibana 这也是一个标准的前端化工程
bin\kibana.bat
http://localhost:5601
找到开发工具
Elasticserach学习记录(一)
文章图片

汉化!!!!
改配置kibana.yaml
i18n.locale: "zh-CN"
IK分词器(中文) elasticsreach默认的不是中文的 放在elasticsreach中的plugins中
有两种算法
  1. ik_smart 默认分词器 他所能理解 不重复的单词 或者字
  2. ik_max_word 最细粒度 穷进词库可能
    GET _analyze { "analyzer": "ik_smart", "text": "我超级喜欢多帅哦" }GET _analyze { "analyzer": "ik_max_word", "text": "我超级喜欢多帅哦" }

你自己需要的词,需要自己加到字典中
IKAnalyzer.cfg.xml编写自己的kb.dic然后在配置文件中添加kb.dic重启es

Rest风格 Elasticserach学习记录(一)
文章图片

1、创建一个索引 随到在请求体中插入数据
PUT /索引名/类型名(可无)/文档id { json请求体 }

Elasticserach学习记录(一)
文章图片

添加 创建一个索引,指定字段类型 但是没有插入信息
PUT /test2 { "mappings": { "properties": { "name":{ "type": "text" }, "age": { "type": "long" }, "birthday":{ "type": "date" } } } }

获得信息
GET test2

如果不写就是默认类型 _doc keyword是不可分割的
PUT /test3/_doc/1 { "name": "多帅哦", "age": 18, "birthday": "1999-10-20" }

GET _cat/health数据库状态 GET _cat/indices?v数据库信息

修改 1、直接暴力PUT
PUT /test3/_doc/1{"name": "多帅哦123","age": 18,"birthday": "1999-10-20"}但是索引信息会改变版本啊状态啊

2、POST
POST /test3/_doc/1/_update{"doc":{"name":"shuaikb"}}

3、删除索引
DELETE test1 根据你的请求是删除什么 索引或者文档

条件查询
GET /test3/_search?q=name:多帅哦/"_score" : 0.5753642,这个_score是匹配度匹配度越高分数越高

复杂搜索
GET /test3/_search{"query": {"match": {"name": "多帅"}}}name里包含 多帅 的都会出来"hits"里面包含了 索引的信息 和查询的结果GET /test3/_search{"query": {"match": {"name": "多帅"}},"_source": ["name","age"]}查询数据中只显示 name和age以后在java中操作es所有的方法都是这里的key排序"sort": [{"age": {"order": "desc"}}]通过什么字段进行排序desc降序asc升序分页"from": 0,第几个数据开始"size": 1返回多少条

布尔值查询
GET /test3/_search{"query": {"bool": {"must": [{"match": {"name": "多帅"}},{"match":{"age": 18}}]}}}精确匹配多条件查询must相当于 andshould 想当于 ornot相当于!=GET /test3/_search{"query": {"bool": {"must": [{"match": {"name": "多帅"}}],"filter": [{"range": {"age": {"gte": 10,"lte": 17}}}]}}}filter 可以进行数据过滤 gt> gte>=lt< lte<=eq

GET /test3/_search{"query": {"match": {"tag": "男 技术"}}}tag查询的多个属性 用空格隔开term,查询时通过倒排是索引进行精确查询match,会使用分词器解析 (先分析文档,然后通过分析文档进行查询!)

两个类型
  1. text 能被分词器解析
  2. keyword 不能被分词器解析
GET _analyze{"analyzer": "standard","text": "多帅哦Shuaikb name1"}只要分词器不是keyword就不会被拆分GET testdb/_search{"query": {"term": {"desc": {"value": "多帅哦Shuaikb desc"}}}}精确匹配keyword类型的字段不会被分词器解析你会发现多帅哦Shuaikb desc2不会被查询出来

多个值匹配的精确查询
GET testdb/_search{"query": {"bool": {"should": [{"term": {"t1": {"value": "22"}}},{"term": {"t1": {"value": "33"}}}]}}}

高亮查询
GET testdb/_search{ "query": {"match": {"name": "帅"} }, "highlight": {"fields": {"name": {}} }}"多哦Shuaikb name2"这个就是高亮的html自定义搜索高亮条件GET testdb/_search{ "query": {"match": {"name": "帅"} }, "highlight": {"pre_tags": "","post_tags": "
","fields": {"name": {}} }}

Spirngboot集成es
原生依赖es-snapshotselasticsearch snapshot repohttps://snapshots.elastic.co/maven/实际我们导入的依赖org.springframework.bootspring-boot-starter-data-elasticsearch注意依赖的版本必须要和自己的本地的es版本一致所以需要自定义版本依赖初始化RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http"),new HttpHost("localhost", 9201, "http"))); 用完记得关client.close(); @Beanpublic RestHighLevelClient restHighLevelClient(){RestHighLevelClient client =new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http"),new HttpHost("localhost", 9201, "http"))); return client; }

API 索引操作
@Testpublic void CreateIndex() throws IOException {//创建索引CreateIndexRequest requst = new CreateIndexRequest("shuaikb_index"); //执行请求 获得响应CreateIndexResponse createIndexResponse =client.indices().create(requst, RequestOptions.DEFAULT); System.out.println(createIndexResponse); }@Testvoid testExisIndex() throws IOException {//判断存在GetIndexRequest request =new GetIndexRequest("shuaikb_index"); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); }@Testvoid testDeleteIndex() throws IOException {//删除索引DeleteIndexRequest request =new DeleteIndexRequest("shuaikb_index"); AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete); }

文档操作
@Testvoid testAddDocument() throws IOException {//添加文档Dog dog = new Dog(); dog.setName("多帅哦"); dog.setAge(12); IndexRequest requst = new IndexRequest("shuaikb_index"); //规则 PUT /shuaikb_index/_doc/1requst.id("1"); requst.timeout(TimeValue.timeValueSeconds(1)); requst.timeout("1s"); //将我们的数据放入请求 jsonrequst.source(JSON.toJSONString(dog), XContentType.JSON); //客户端发送请求IndexResponse indexResponse = client.index(requst, RequestOptions.DEFAULT); System.out.println(indexResponse.toString()); System.out.println(indexResponse.status()); }@Testvoid testExisDocument() throws IOException {//判断文档存在GetRequest getRequest = new GetRequest("shuaikb_index","1"); //不获取返回的_source的上下文getRequest.fetchSourceContext(new FetchSourceContext(false)); getRequest.storedFields("_noe_"); boolean exists = client.exists(getRequest, RequestOptions.DEFAULT); System.out.println(exists); }@Testvoid testGetDocument() throws IOException {//获取文档信息GetRequest getRequest = new GetRequest("shuaikb_index","1"); GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT); System.out.println(documentFields.getSourceAsString()); System.out.println(documentFields); }@Testvoid testUpdateDocument() throws IOException {//更新文档信息UpdateRequest updateRequest = new UpdateRequest("shuaikb_index","1"); updateRequest.timeout("1s"); Dog dog = new Dog("你怎么说",20); updateRequest.doc(JSON.toJSONString(dog),XContentType.JSON); client.update(updateRequest,RequestOptions.DEFAULT); }@Testvoid testDeleteDocument() throws IOException {//删除文档信息DeleteRequest deleteRequest = new DeleteRequest("shuaikb_index","1"); deleteRequest.timeout("1s"); DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(delete); }

大量数据操作
//大量数据操作@Testvoid testBulkRequest() throws IOException {BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("10s"); ArrayList dogArrayList = new ArrayList(); dogArrayList.add(new Dog("test1",1)); dogArrayList.add(new Dog("test2",1)); dogArrayList.add(new Dog("test3",1)); dogArrayList.add(new Dog("test4",1)); dogArrayList.add(new Dog("shuaikb1",1)); dogArrayList.add(new Dog("shuaikb2",1)); dogArrayList.add(new Dog("shuaikb3",1)); dogArrayList.add(new Dog("shuaikb4",1)); for (int i = 0; i

    推荐阅读