es基本查询
java语言es查询的常用api 参考:https://www.tizi365.com/archives/938.html
// 通过SearchSourceBuilder构建搜索参数
SearchSourceBuilder builder = new SearchSourceBuilder();
// 通过QueryBuilders构建ES查询条件,这里查询所有文档,复杂的查询语句设置可以详细了解QueryBuilders
builder.query(QueryBuilders.matchAllQuery())
es自带的head页面查询
根据id查询:GET /索引/索引类型/id
示例:GET /my_index/index_type/_123455 查询 id=_123455 的数据
kibana查询
GET /index/type/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"test_sequence": "34134123ffasfdafdpeqwrr"
}
},
{
"terms": {
"test_staus": [
1,
2,
3
]
}
},
{
"terms": {
"test_keyword": [
"hello world",
"are you ok?",
"test"
]
}
},
{
"range": {
"test_time": {
"gt": "2023-07-07 00:00:00",
"lt": "2023-07-12 00:00:00"
}
}
},
{
"wildcard": {
"test_text": "abc*"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10000,
"sort": [
{
"id":{
"order": "desc"
}
}
],
"_source": [
"test_a",
"test_b"
],
"aggs": {}
}
-- term:相当于mysql中的 ==
-- terms:相当于mysql中的 in
-- range:相当于mysql中的 >、<、>=、<=,gt:>,lt:<,ge:>=,le:<=
-- wildcard:相当于mysql中的 like,不同的是通配符用* 而不是 %
-- must:相当于mysql中的 and
-- must_not:相当于mysql中的 not
-- should:相当于mysql中的 or
-- "from": 0,"size": 10000:表示返回查询结果的前10000条数据,es默认支持最大返回的数量为10000,size大于10000时,es查询的时候会直接报错
-- sort:上面的示例表示,将查询结果,按照 id 倒叙排列
-- source:查询结果需要展示的字段,如果没有指定则反馈所有字段信息
-- aggs:聚合条件
- es查询示例,把查询条件到query后面就行了,可以参考上面那一条
{
"query": {},
"from": 0,
"size": 10000,
"sort": [],
"aggs": {}
}
es基本操作
加字段
基本格式
PUT /索引/索引类型/_mapping/
{
"properties":{
"新加的字段名":{
"type":"字段类型"
}
}
}
示例
PUT /my_index/index_type/_mapping/
{
"properties":{
"my_field":{
"type":"keyword"
}
}
}
注意:es加完字段后,直接查询数据,历史数据不会显示这个字段,需要执行脚本,
该脚本慎重使用,参考:https://blog.youkuaiyun.com/qq_33999844/article/details/108985290
POST /my_index/index_type/_update_by_query/
{
"script": {
"lang": "painless",
"inline": "if (ctx._source.my_field== null) {ctx._source.my_field= null}"
}
}
修改数据值
基本格式
POST /索引/索引类型/文档id/_update
{
"doc": {
"字段名称": 字段值
}
}
注意:文档id,要与该数据的查询结果一致(一般是带下划线的,示例:_123455)
示例
POST /my_index/index_type/_123455/_update
{
"doc": {
"my_field": "hello world",
"my_fileld2":"ok"
}
}