Elasticsearch

文档连接Elasticsearch文档API - Elasticsearch 教程 | BootWiki.com

B站视频连接:https://www.bilibili.com/video/BV1hh411D7sb?p=10

一:介绍

内部端口 9200

web接口 9300 本地访问地址:localhost:9300

使用restful接口风格

二:索引操作

_cat 表示查询索引

_doc 表示文档操作

_search查询文档

q 表示query

from 页码

size 每页查询条数,可单独使用,指查询几条数据

match 全量检索 参数会进行分词查询,每个词是个搜索关键字

match_phrase 完全匹配

highlight高亮显示

aggs 聚合操作{"aggs":{"字段名_group随机起名":{"terms":{"field":"字段名(分组字段)"}}}}

terms 分组名称

avg 平均值

properties 分词查询

1、创建索引方式

(put)通过postman,调用http://127.0.0.1:9200/index(索引名称),使用put类型

2、 取出索引信息

(get)postman,使用get方式,http://127.0.0.1:9200/index(索引名称)

3、获取当前所有索引

(get)通过postman,调用http://127.0.0.1:9200/_cat/indices?v,使用put类型

4、删除索引

(delete)通过postman,调用http://127.0.0.1:9200/index(索引名称),使用delete类型

5、创建文档(在索引中创建,_doc代表创建文档)

(post)postman,使用get方式,http://127.0.0.1:9200/index(索引名称)/_doc,body中增加rwa类型json

(put)地址后面不加ID,每次创建新的文档,且ID不同,增加/123(id),指定ID,幂等性操作。

6、文档查询

单条数据:(get)postman,使用get方式,http://127.0.0.1:9200/index(索引名称)/_doc/10001(id)

全部查询:(get)postman,使用get方式,http://127.0.0.1:9200/index(索引名称)/_search

7、文档修改

局部数据更新 (post请求,常用更新方式),调用http://127.0.0.1:9200/index(索引名称)/_update/1001(id) ,body中增加rwa类型json {"doc":{}}

全量数据更新 (put),调用http://127.0.0.1:9200/index(索引名称)/_doc/1001(id) ,body中增加rwa类型json {}

8、删除文档

(delete)通过postman,调用http://127.0.0.1:9200/index(索引名称)/_doc/1001(id),使用delete类型

9、搜索数据

(get)http://127.0.0.1:9200/index(索引名称)/_search?q=参数:值

(post)http://127.0.0.1:9200/index(索引名称)/_search body中增加raw格式json,{"query":{"match":{参数:值}}}

全量查询(get)http://127.0.0.1:9200/index(索引名称)/_search,body中增加raw格式json,{"query":{"match_all:"""}}

全量分页查询(get)http://127.0.0.1:9200/index(索引名称)/_search,body中增加raw格式json,{"query":{"match_all:"""},"from":0,"size":10}

查询指定参数,根据指定参数排序{"query":{"match_all:"""},"from":0,"size":10,"_source":["参数1","参数2"],"sort":{"参数名":{"order":"desc"}}}

多条件查询:,同时成立(must,类似and){"query":{"bool":{"must":[{"match":{"参数":"值"}},{"match":{"参数2":"值2"}}]}}}

多条件查询:,或者(should,类似or){"query":{"bool":{"should":[{"match":{"参数":"值"}},{"match":{"参数2":"值2"}}]}}}

范围查询(增加filter,range,gt代表大于):,{"query":{"bool":{"should":[{"match":{"参数":"值"}},{"match":{"参数2":"值2"}}],"filter":{"range:":{"参数1":{"gt":1000}}}}}}

三:创建ES客户端,代码操作ES

增加elasticsearch maven依赖和高级客户端maven依赖

//创建客户端

RestHighLevelClient esClient=new ResthighLevelClient(RestClient.build(new HttpHost("localhost",9200,"http")));

//创建索引 create(CreateIndexRequest) , 查询get(GetndexRequest) 删除delete

CreateIndexRequest request=new CreateIndexRequest("user");

CreateIndexResponse response=esClient.indices().create(request,RequestOptions.DEFAULT);

//响应状态

boolean flag=response.isAcknowledged();

//输出索引操作状态

//关闭客户端

esClient.close();

四:集群部署操作

复制多份es项目,修改每个项目的配置文件

打开:config/elasticsearch.yml

放开集群名称注释,集群名称要相同,cluster.name:my-application

设置节点类型,数据节点类型

node.master:true

node.data:true

放开节点名称注释,节点名称不同,node.name:node-1001

放开主机名称注释,每台机器IP,network.host:location

放开端口号注释postman外部调用端口,http.port:1001

设置tcp端口号:transport.tcp.port:9301

设置跨域配置

http.cors.enabled:true

http.cors.allow-origin:"*"

第二台机器,配置主机器的ip

dicovery.seed_hosts:["localhost:9301"]

discovery.zen.fd.ping_timeout:1m

discovery.zen.fd.ping_retries:5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值