ES简单实用DSL查询

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查询)