elasticSearch基本操作

1、查看所有索引

GET _cat/indices

  

2、创建一个新的索引

PUT /asif-test
{
  "mappings": {
        "_doc": {
            "properties": {
                "id": {"type": "keyword"},      
                "title": {"type": "keyword"},
       "price": {"type": "integer"},
                "esDate": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
            }
         }  
  }
}

  

3、删除索引

DELETE /asif-test

  

4、添加索引数据

//制定ID添加:
PUT /asif-test/_doc/1
{      
  "title": "title1"
}

//自动生成ID添加:
POST /asif-test/_doc
{      
  "title": "title2"
}

  

5、查询数据

//查询所有:
POST /asif-test/_doc/_search?pretty=true

//单条件查询:
POST /asif-test/_doc/_search?pretty=true
{
  "size":10,
    "query": {
        "term" : {
          "title" : "title1"
        }
    }
}
POST /asif-test/_doc/_search?pretty=true
{
  "size":10,
    "query": {
      "bool": {
        "filter": {
          "term" : {
            "title" : "title1"
          }
        }
      }
    }
}
POST /asif-test/_doc/_search?pretty=true 
{
 "size":10,
 "query": {
    "bool": {
       "must": {
          "term" : {
              "title" : "title1"
          }
       }
     }
   }
 }

//单条件不包含查询 
POST /asif-test/_doc/_search?pretty=true 
{
 "size":10,
 "query": {
    "bool": {
       "must_not": {
          "term" : {
             "title" : "title1"
          }
       }
    }
   }
 } 
//多条件查询 
POST /asif-test/_doc/_search?pretty=true 
{
    "size":10,
    "query":{
        "bool":{
            "must":{
                "terms":{
                    "title":[
                        "title1",
                        "title2"
                    ]
                }
            }
        }
    }
} 
//多字段查询 
POST /asif-test/_doc/_search?pretty=true 
{
    "size":10,
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "title":"title1"
                    }
                },
                {
                    "match":{
                        "_id":1
                    }
                }
            ]
        }
    }
}

  

 

6、正则查询

POST /asif-test/_doc/_search?pretty=true
{
  "size":10,
  "query": {
      "regexp":{
              "title": "title.*"
      }
  }
}

  

7、范围查询

POST /asif-test/_doc/_search?pretty=true 
{ "query": { "range" : { "price" : { "gte" : 50, "lte" : 70 } } } }

  

8、分组聚合

POST /asif-test/_doc/_search?pretty=true
{
  "size":10,
  "aggs": {
    "type": {
      "terms": {
        "field": "title"
      }
    }
  }
}

  

9、基本求值

//最大值
POST /asif-test/_doc/_search?pretty=true
{
  "size":10,
  "aggs": {
    "max_price": {
      "max": {
        "field": "price"
      }
    }
  }
}

//最小值
POST /asif-test/_doc/_search?pretty=true
{
  "size":10,
  "aggs": {
    "min_price": {
      "min": {
        "field": "price"
      }
    }
  }
}

//平均值
POST /asif-test/_doc/_search?pretty=true
{
  "size":10,
  "aggs": {
    "avg_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}

//求和
POST /asif-test/_doc/_search?pretty=true
{
  "size":10,
  "aggs": {
    "sum_price": {
      "sum": {
        "field": "price"
      }
    }
  }
}

//去重count
POST /asif-test/_doc/_search?pretty=true
{
  "size":10,
  "aggs": {
    "uniq_title":{
      "cardinality": {
        "field": "title"
      }
    }
  }
}

  

 10、排序

POST /asif-test/_search
{
    "query": {
        "bool": {
            "filter": [
                {
                    "range": {
                        "actionTime": {
                            "gte": 0,
                            "lt": 1621929029
                        }
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "actionTime": {
                "order": "desc"
            }
        }
    ],
    "from": 0,
    "size": 1000
}

  

posted on 2021-06-21 18:25  asif  阅读(43)  评论(0编辑  收藏  举报

导航