ES常用笔记

0.ES常用命令:
启动 开机自启
启动kibana . brew services start kibana 或 kibana
es 浏览器访问 : http://localhost:9200/
kibana 浏览器访问: http://localhost:5601/
es查看健康: http://localhost:9200/_cat/health
1.ES常用语法:
在ElasticSearch中,Index可以类比为关系型数据库中的库,Type可以类比为关系型数据库的表,Document可以类比为关系型数据库中的行。在ElasticSearch中如何建表,其实就是在ElasticSearch中如何建立Index,并且指定哪些field需要使用全文索引,指定field是什么类型等等。
创建库
PUT app_test
{
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 1
}
}

创建表
PUT app_test/persions
{
“mappings”: {
“_doc”:{
“properties”: {
“id”: { “type”: “text” },
“name”: { “type”: “text” },
“sex”: { “type”: “text” },
“sect”: { “type”: “text” },
“age”: { “type”: “integer” }
//“created”: {
//“type”: “date”,
//“format”: “strict_date_optional_time||epoch_millis”
//}
}
}
}
}
查看所有索引(库)
GET _cat/indices
查看类型结构 (表结构)
GET my_index/_doc/_mapping
GET app_test/persions/_mapping
查看类型 数据(表数据)
GET app_test/persions/_search
参考:https://www.jianshu.com/p/56b5cbcaf460

1.插入 1为指定序号, 可以不指定
POST app_test/persions/1
{
“id”:“000”,
“name”:“李四”,
“sex”:“男”,
“sect”:“明教”,
“age”:1
}
2.更新
一种是通过PUT的全覆盖方式,旧数据将被删除,以新的代替。
PUT app_test/persions/1
{
“id”:“000”,
“name”:“李四2”,
“sex”:“男”,
“sect”:“明教”,
“age”:1
}
另一种是通过POST方式,只对部分字段进行修改。
POST app_test/persions/1/_update
{
“doc”:{
“name”:“李四3”
}
}
3.删除
DELETE app_test/persions/1
其他mget (同时取多条)或 bulk批量操作(批量执行create 、 index 、 update 或 delete )参照参考
参考:http://wjhsh.net/aaanthony-p-7380662.html
4.查询
term :查询等价SQL中的等值=查询 ,terms 多值相等 等价SQL中的 in
match :查询等价SQL中的like模糊匹配,multiMatch 多值匹配 等价SQL中的 like A and like B and like C
wildcard :模糊查询且要使用通配符时不用match而用wildcard % *,也可以用prefix(前缀)等
range :范围查询等价SQL中的><=范围 (gt >,gte >= ,lt <,lte <=)
bool :多条件查询, 相当于 where A条件and B条件or C条件,使用条件类型大于2个外边套bool.
包含must/must_not 和should 还有filter,
must/must_not 相当于and,
should 相当于 or,
filter 与must,should并排一起使用时相当于在套一层查询(子查询),
单独使用时filter与must基本一样
将must、must_not置于filter下时
aggregations :查询等价SQL中的等值group 查询 。 cardinality 去重
sort:排序相当于order by的使用方法“sort”: “title.sort” 或“title.sort”:{ “order”: “desc” },
其他关键字 : keyword 相当于value/boost 搜索条件的权重/ fields field 字段
highlight 设置高亮 _index 索引 _type 类型 _shards分片信息 total总计分片数
successful查询成功的分片数 skipped跳过查询的分片数 failed查询失败的分片数
hits 命中结果
查询实例查看 4.练习实例
查询语法参考:https://blog.csdn.net/weixin_38405253/article/details/124009568
分页在query外加from size;
{
“from”: 0,
“size” : 10,
query{
}
}

2.Java调用ES常用API:

1.更新 插入
采用参考elasticsearchTemplate
插入 bulkIndex
List persionsAllList = persionsDao.selectall();
Persions persions=persionsAllList.get(0);
Item item=new Item(persions.getId(),persions.getName(),persions.getSex(),persions.getSect(),persions.getAge());
List list = new ArrayList();
list.add(new IndexQueryBuilder().withObject(item).build());
elasticsearchTemplate.bulkIndex(list);
实例见github elastic_search_test
参考: elasticsearchTemplate https://blog.csdn.net/hzj_java/article/details/118096157
更新
Map<String, Object> params = new HashMap<>();
params.put(“age”, new Integer(“21”));
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.doc(params);
UpdateQuery updateQuery = new UpdateQueryBuilder()
.withClass(Item.class)
.withId(“001”)
.withUpdateRequest(updateRequest)
.build();
elasticsearchTemplate.update(updateQuery);
实例见github elastic_search_test
参考 elasticsearchTemplate https://blog.csdn.net/justlpf/article/details/121493781
其他参考: RestHighLevelClient https://www.cnblogs.com/zaevn00001/p/15801940.html
2.查询
采用参考elasticsearchTemplate
1.基本查询:
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery(“spring boot OR 书籍”))//查询简单字符串
//.withPageable(PageRequest.of(0, 10))//分页查询
.build();
List resultItemLists =elasticsearchTemplate.queryForList(searchQuery,Item.class);

2.常用查询
QueryBuilders.queryStringQuery(“张无忌”);//term 简单查询

QueryBuilders.termQuery(“name.keyword”,“张无忌”);//term 简单等值查询

QueryBuilders.matchQuery(“name.keyword”,“张无忌”);//match 。 模糊查询

QueryBuilders.rangeQuery(“age”).gte(18).lte(22)//range 范围查询

QueryBuilders.wildcardQuery(“name.keyword”,“张*忌”);//wildcard 模糊查询 ,带通配符

QueryBuilders.boolQuery();//bool 。 多条件
List queryarr=new ArrayList<>();
queryarr.add(QueryBuilders.termQuery(“sect.keyword”,“明教”));
queryarr.add(QueryBuilders.termQuery(“sex.keyword”,“女”));
boolQueryBuilder.must().addAll(queryarr);

AggregationBuilders.cardinality(“sect_name”).field(“sect.keyword”);//cardinality 。 去重

QueryBuilders.termQuery(“name.keyword”,“张无忌”);//aggregations 聚合
TermsAggregationBuilder agg = AggregationBuilders.terms(“sect_count”).field(“sect.keyword”);
TermsAggregationBuilder agg2 = AggregationBuilders.terms(“sex_count”).field(“sex.keyword”);
agg.subAggregation(agg2);

实例见 elastic_search_test
参考: elasticsearchTemplate https://blog.csdn.net/hzj_java/article/details/118096157
聚合用法查询 https://blog.csdn.net/wenwen513/article/details/85163168

ES练习项目: https://github.com/QingYang12/elastic_search_test

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值