zoukankan      html  css  js  c++  java
  • 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
    }
    

      

  • 相关阅读:
    MySQL 中 truncate 和 delete 的坑
    理解 Linux 中的 inodes
    Linux 下date命令的常见用法
    Linux 下du命令详解
    nc
    启动Tomcat时报错:错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1101; nested exception is: java.net.BindException: Address already in use: JVM_Bind
    ssm调用webservice接口并进行文件传输
    mybatis 中 执行INSERT操作后获取自增主键
    mysql时间差8小时
    生成验证码工具类
  • 原文地址:https://www.cnblogs.com/asif/p/14915075.html
Copyright © 2011-2022 走看看