美文网首页esElasticSearch
ES简单实用DSL查询

ES简单实用DSL查询

作者: 行智 | 来源:发表于2018-12-30 18:02 被阅读0次

ES版本信息

$ http://10.72.25.10:9200/
{
  "name" : "hdp4.0",
  "cluster_name" : "mycluster",
  "cluster_uuid" : "mlYXoX2nQLqoEg0mirQsdw",
  "version" : {
    "number" : "5.5.3",
    "build_hash" : "Unknown",
    "build_date" : "2017-11-20T04:23:31.244Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}

查看所有索引

$ http://10.72.25.10:9200/_cat/indices

查看字段类型

$ http://10.72.25.10:9200/books/_mapping?pretty=true

创建索引

curl -XPUT 'http://10.72.25.10:9200/books/' -d '{  
    "mappings": {
        "IT": {
            "properties": {
                "message": {
                    "type": "string"        
                }      
            }    
         }  
}}'

curl -XPUT 'http://10.72.25.10:9200/books/' -d '{  
    "mappings": {
        "IT": {
            "properties": {
                "id": {"type": "keyword"},      
                "title": {"type": "keyword"},      
                "language": {"type": "keyword"},      
                "author": {"type": "keyword"},      
                "price": {"type": "float"},      
                "year": {"type": "integer"},      
                "description": {"type": "keyword"},      
                "tel": {"type": "keyword"},
                "d_val": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
            }    
         }  
}}'

删除索引

$ curl -XDELETE http://10.72.25.10:9200/books

批量(_bulk)加载数据

$ cat book.json
{"index":{ "_index": "books", "_type": "IT", "_id": "1" }}
{"id":"1","title":"Java Book","language":"java","author":"Bruce Eckel","price":70.20,"year":2007,"description":"Java must read", "tel":"15313016138", "d_val":"2018-11-01 12:25:36"}
{"index":{ "_index": "books", "_type": "IT", "_id": "2" }}
{"id":"2","title":"Java perform","language":"java","author":"John Li","price":46.50,"year":2012,"description":"permformance ..", "tel":"13548621254", "d_val":"2018-11-01 08:25:50"}
{"index":{ "_index": "books", "_type": "IT", "_id": "3" }}
{"id":"3","title":"Python compte","language":"python","author":"Tohma Ke","price":81.40,"year":2016,"description":"py ...", "tel":"13245687956", "d_val":"2018-11-01 19:30:20"}
{"index":{ "_index": "books", "_type": "IT", "_id": "4" }}
{"id":"4","title":"Python base","language":"python","author":"Tomash Si","price":54.50,"year": 2014,"description":"py base....", "tel":"aefda1567fdsa13", "d_val":"2018-09-01"}
{"index":{ "_index": "books", "_type": "IT", "_id": "5" }}
{"id":"5","title":"JavaScript high","language":"javascript","author":"Nicholas C.Zakas","price":66.40,"year":2012,"description":"JavaScript.....", "tel":"a14512dfa", "d_val":"2018-08-01"}

$ curl -XPOST "http://10.72.25.10:9200/_bulk?pretty" --data-binary @book.json

查询数据

$ http://10.72.25.10:9200/books/IT/_search?pretty=true

查询1000条数据

curl -XPOST "http://10.72.25.10:9200/dam_snake/snake/_search?pretty" -d '{  
    "size":10000,
    "query": {
        "match_all": {}
  

单/多字段过虑查询 不包含查询

curl -XPOST "http://10.72.25.10:9200/dam_snake/snake/_search?pretty" -d '{  
    "size":10,
    "query": {
        "bool" : {
           "must_not" : {
              "term" : {
                "field" : "video_name"
              }
           }
         }
    }
}'

curl -XPOST "http://10.72.25.10:9200/dam_snake/snake/_search?pretty" -d '{  
  "query": {
      "bool": {
           "must_not": {
                 "terms": {  
                     "field" : ["video_name", "column_name", "areaid"]
                 }
            }
       }
  },
  "size": 10
}'

正则过虑

# 官方文档标明正则不支持text
curl -XPOST "http://10.72.25.10:9200/dam_snake/snake/_search?pretty" -d '{  
  "size" : 100,
  "query": {
        "regexp":{
                "s_value": "DV.*58"
        }
    }
}'

多字段过虑包含

curl -XPOST "http://10.72.25.10:9200/dam_snake/snake/_search?pretty" -d '{  
  "query": {
      "bool": {
           "must": [
                 { "match": { "port":  "9083" }},
                 { "match": { "name": "tt_vod_program_play_test" }},
                 { "match": { "s_timestamp": "1545719719164" }}
            ]
       }
  },
  "size": 10
}'

过虑COUNT

curl -XPOST "http://10.72.25.10:9200/dam_snake/snake/_search?pretty" -d '{  
  "query": {
      "term" : { "source" : "Hive" }
  }
}'

日期范围查询

curl -XPOST "http://10.72.25.10:9200/books/IT/_search?pretty" -d '{  
  "query": {
    "range": {
        "d_val": {
            "gte": "2018-09-01",
            "lte": "2018-12-30"
        }
    }
  }
}'

查询数字范围

curl -XPOST "http://10.72.25.10:9200/books/IT/_search?pretty" -d '{  
    "query": {
        "range" : {
            "price" : {
                "gte" : 50,
                "lte" : 70
            }
        }
    }
}'

分组聚合

$ curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '{  
"size": 0,
"aggs": {
    "user_type": {
      "terms": {
        "field": "year"
      }
    }
  }
}'

统计price最大值

$ curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '{  
"size": 0,
"aggs": {
    "max_price": {
      "max": {
        "field": "price"
      }
    }
  }
}'

去重COUNT

curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '
{
  "aggs":{
    "uniq_attr":{
      "cardinality": {
        "field": "name"
      }
    }
  }
}
'

统计price最小值

$ curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '{  
"size": 0,
"aggs": {
    "min_price": {
      "min": {
        "field": "price"
      }
    }
  }
}'

分组并求平均

$ curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '{  
    "size": 0,
    "aggs": {
        "per_count": {
            "terms": {
                "field": "year"
            },
            "aggs": {
                "avg_price": {
                    "avg": {
                        "field": "price"
                    }
                }
            }
        }
    }
}'

求和

$ curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '{  
    "size": 0,
    "aggs": {
        "sum_count": {
            "sum": {
                "field": "price"
            }
        }
    }
}'

基本统计

返回字段的最大值、最小值、平均值、求和

$ curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '{  
    "size": 0,
    "aggs": {
        "grades_stat": {
            "stats": {
                "field":"price"
            }
        }
    }
}'

高级统计

除基本的外, 高级统计还会返回方差、标准差等

$ curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '{  
    "size": 0,
    "aggs": {
        "grades_stat": {
            "extended_stats": {
                "field":"price"
            }
        }
    }
}'

百分比统计

$ curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '{  
    "size": 0,
    "aggs": {
        "load_time_outlier": {
            "percentiles": {
                "field":"year"
            }
        }
    }
}'

分段统计

统计价格小于50、50-80、大于80的百分比

$ curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '{  
    "size": 0,
    "aggs": {
        "price_ranges": {
            "range": {
                "field":"price",
                "ranges": [ { "to": 50}, {"from":50, "to": 80}, {"from": 80} ]
            }
        }
    }
}'

过虑加SUM

$ curl -XPOST "http://10.72.25.10:9200/books/_search?pretty" -d '{  
    "size": 0,
    "query":{
        "match": {
            "language":"java"
        }
    },
    "aggs": {
            "sum_cnt": {
                "sum": {
                    "field":"price"
                }
            }
    }
}'

相关文章

  • ES简单实用DSL查询

    ES版本信息 查看所有索引 查看字段类型 创建索引 删除索引 批量(_bulk)加载数据 查询数据 查询1000条...

  • ES搜索(二)query查询

    ES可以使用URI或DSL进行查询 URI 由于DSL可以提供更多功能,以及可视化更好,一般都使用DSL进行查询 ...

  • ElasticSearch查询DLS

    查询和过滤的区别 ES提供基于JSON的完整DSL来定义查询,查询DSL包括两种子句:叶查询子句:在特定的字段上查...

  • Elasticsearch 查询语法 --- 2022-04-0

    通过ES查询表达式(Query DSL),可以实现复杂的查询功能,ES查询表达式主要由JSON格式编写,可以灵活的...

  • Request Body Search

    介绍 本章会介绍怎样将查询语句通过http request body发送给es,并介绍简单的DSL 分页 使用fr...

  • elasticsearch常用命令(使用kibana)

    基于版本es7.17,kibana7.17 注意:更复杂的就要使用DSL,以下都是简单的语法 测试分词器 查询索引...

  • ES的多种检索方式

    ES的多种检索方式 查询全部: 生产中并不常用 DSL(Domain Specified Language):特定...

  • elasticsearch类似like的语法

    背景 在项目中需要使用类似与sql中like的方式查询es的数据。 方法 使用wildcard进行查询 dsl语法...

  • 2019-06-28

    ES 正则 查询手机号为移动运营商DSL(性能巨慢): POST tbcrm_*/memberInfo/_upda...

  • Elasticsearch v1.7简单实用DSL查询

    返回指定字段:_source 统计查询分类:facets

网友评论

    本文标题:ES简单实用DSL查询

    本文链接:https://www.haomeiwen.com/subject/uznvlqtx.html