zoukankan      html  css  js  c++  java
  • Elasticsearch初级语句入门

    # 创建索引
    PUT test
    {
      "settings": {
        "index":{
          "number_of_shards":5,
          "number_of_replicas":1
        }
      }
    }
    
    # 使用默认配置创建索引pretty只是为了如果json返回打印更  加的漂亮一点
    PUT test2?pretty
    # 更改索引值
    PUT test/_settings 
    {
      "number_of_replicas":0
    }
    
    # 查看指定的索引配置信息
    GET test/_settings
    
    # 查看所有的索引配置信息
    GET _all/_settings
    
    # 查看所有的索引的健康信息
    GET /_cat/indices?v
    
    # 向索引库中添加文档
    PUT /test/zjj/1
    {
      "name":"xiaou",
      "age":18,
      "desc":"我是xiaou"
    }
    
    # 通过id获得信息
    GET /test/zjj/1
    
    # GET /test/zjj出现问题
    # 对查询数据筛选
    GET /test/zjj/1?_source=age,desc
    
    # 修改文档 
    # 1覆盖id
    PUT /test/zjj/1
    {
        "age":"20"
    }
    
    # 2修改
    POST /test/zjj/1
    {
      "age":18
    }
    
    # 更新
    
    POST /test/zjj/1/_update?pretty
    {
      "script" : "ctx._source.age += 5"
    }
    # 删除
    
    DELETE test/zjj/2
    
    # 删除索引库
    DELETE test

     查询语句

    PUT xiaou
    {
    
      "settings":{
      
          "index":{
            "number_of_shards": 3,
            "number_of_replicas": 0
            }
          }
    }
    
    
    POST /xiaou/test/_bulk
    {"index":{"_id":1}}
    {"title":"Java","price":55}
    {"index":{"_id":2}}
    {"title":"php","price":60}
    {"index":{"_id":3}}
    {"title":"ww","price":75}
    
    GET xiaou/test/_mget
    {
      "ids":["1","2","3","4"]
    }
    
    POST /xiaou/test/_bulk
    {"delete":{"_id":1}}
    {"create":{"_id":4}}
    {"title":"javascript","price":75}
    {"index":{"_index":"test3","_type":"t","_id":1}}
    {"name":"xiaou"}
    {"update":{"_index":"xiaou","_type":"test","_id":3}}
    {"doc":{"price":58}}
    
    POST /xiaou/test/_bulk
    {"index":{"_id":1}}
    {"title":"i love java"}
    GET /xiaou/_mapping
    
    GET xiaou/test/_search?q=java
    
    PUT test4
    {
      "settings": {
          "index":{
            "number_of_shards": 3,
            "number_of_replicas": 0
          }
      },"mappings": {
        "books":{
          "properties": {
            "title":{"type":"text"},
            "name":{"type":"text","index":false},
            "price":{"type":"double"},
            "number":{"type":"object",
                  "dynamic":true}
          }
        }
      }
    }
    
    GET test4/_mapping
    
    PUT test4/books/1
    {
      "name":"xiaou",
      "number":{"1":1,"2":3},
      "price":56.55,
      "title":"this java"
    }
    
    GET /test4/books/1
    
    # 数据准备
    PUT /lib3
    {
        "settings":{
        "number_of_shards" : 3,
        "number_of_replicas" : 0
        },
         "mappings":{
          "user":{
            "properties":{
                "name": {"type":"text",
                         "analyzer": "ik_max_word"},
                "address": {"type":"text",
                          "analyzer": "ik_max_word"},
                "age": {"type":"integer"},
                "desc": {"type":"text",
                         "analyzer": "ik_max_word"},
                "birthday": {"type":"date"}
            }
          }
         }
    }
    
    PUT /lib3/user/1
    {"name":"小美",
      "address":"浙江桐乡",
      "age":18,
      "desc":"喜欢唱歌,吃饭,睡觉",
      "birthday":"1999-03-05"}
      
    PUT /lib3/user/2
    {"name":"小u",
      "address":"浙江杭州",
      "age": 25,
      "desc":"喜欢唱歌,睡觉",
      "birthday":"1999-03-05"}
    
    PUT /lib3/user/3
    {"name":"小啦",
      "address":"浙江下沙",
      "age":16,
      "desc":"喜欢玩游戏,睡觉",
      "birthday":"1999-03-05"}
      
    PUT /lib3/user/4
    {"name":"小打",
      "address":"浙江呜呜",
      "age":12,
      "desc":"喜欢玩游戏",
      "birthday":"2000-03-05"}
    
    # term查询
    GET /lib3/user/_search
    {
      "query": {
        "term": {"name":"小美"}
      }
    }
    
    # terms查询
    GET /lib3/user/_search
    {
       "query": {
         "terms": {"desc":["吃饭","游戏"]}
       }
    }
    
    # 分页
    GET /lib3/user/_search
    {
       "from": 0,
       "size": 2, 
       "query": {
         "terms": {"desc":["吃饭","游戏"]}
       }
    } 
    
    # 返回版本号
    GET /lib3/user/_search
    {
       "from": 0,
       "size": 2,
       "version": true, 
       "query": {
         "terms": {"desc":["吃饭","游戏"]}
       }
    } 
    
    # match查询
    GET /lib3/user/_search
    {
      "query": {
          "match": {
            "address": "拉你桐乡"
          }
      }
    }
    
    # 查询所有文档
    GET /lib3/user/_search
    {
      "query": {
          "match_all": {}
      }
    }
    
    # 指定多个字段
    GET lib3/user/_search
    {
      "query": {
          "multi_match": {
            "query": "游戏桐乡",
            "fields": ["address","desc"]
          }
      }
    }
    
    # 短语匹配查询
    GET  lib3/user/_search
    {
      "query": {
        "match_phrase": {
          "desc": "玩游戏"
        }
      }
    }
    
    # 指定返回的字段
    GET  lib3/user/_search
    {
      "_source": ["address","desc"], 
      "query": {
        "match_phrase": {
          "desc": "玩游戏"
        }
      }
    }
    
    # 控制加载的字段
    GET /lib3/user/_search
    {
        "query": {
            "match_all": {}
        },
        "_source": {
            "includes": ["name"],
            "excludes": ["age"]
        }
    }
    
    # 通配符
    GET /lib3/user/_search
    {
      "query": {
            "match_all": {}
        },
      "_source": {
            "includes": ["n*"],
            "excludes": ["a?e"]
      }
    }
    
    # sort
    GET /lib3/user/_search
    {
      "query": {
            "match_all": {}
        },
      "sort": [
        {
          "age": {"order": "asc"}
        }
      ]
    }
    
    # 前缀匹配查询
    GET /lib3/user/_search
    {
      "query": {
        "match_phrase_prefix": {
          "name": "小"
        }
      }
    }
    
    # 范围查询
    GET /lib3/user/_search
    {
        "query": {
          "range": {
            "birthday": {
              "gte": "1999-01-01",
              "lte": "2000-01-01"
            }
          }
        }
    }
    
    # wildcard查询
    GET /lib3/user/_search
    {
      "query": {
        "wildcard": {
          "name": {
            "value": "*u"
          }
        }
      }
    }
    
    # fuzzy实现模糊查询
    GET /lib3/user/_search
    {
        "query": {
            "fuzzy": {"name":"u"}
        }
    }
    
    # 高亮
    GET /lib3/user/_search
    {
      "query": {
          "match": {
            "desc": "唱歌"
          }
      },
      "highlight": {
          "fields": {
            "desc": {}  
          }
      }
    }
    
    POST /lib4/items/_bulk
    {"index": {"_id": 1}}
    {"price": 40,"itemID": "ID100123"}
    {"index": {"_id": 2}}
    {"price": 50,"itemID": "ID100124"}
    {"index": {"_id": 3}}
    {"price": 25,"itemID": "ID100124"}
    {"index": {"_id": 4}}
    {"price": 30,"itemID": "ID100125"}
    {"index": {"_id": 5}}
    {"price": null,"itemID": "ID100127"}
    
    GET lib4/items/_search
    {
      "post_filter": {
        "term": {
          "price": "40"
        }
      }
    }
    
    GET lib4/items/_search
    {
      "post_filter": {
        "terms": {
          "price":[25,40]
        }
      }
    }
    
    GET lib4/items/_search
    {
      "post_filter": {
        "term": {
          "itemID": "id100123"
        }
      }
    } 
    
    # 组合过滤查询
    GET lib4/items/_search
    {
      "post_filter": {
        "bool": {
          "should":[
              {"term":{"price":25}},
              {"term":{"itemID":"id100123"}}
          ],
          "must_not":{"term":{"price":30}}
        }
      }
    }
    
    GET lib4/items/_search
    {
      "post_filter": {
        "bool": {
          "should":[{"term":{"price":25}}],
          "must_not":{"term":{"price":30}}
        }
      }
    }
    
    # 嵌套使用bool
    GET lib4/items/_search
    {
        "post_filter": {
          "bool": {
            "should":[{"term":{"itemID":"id100123"}},
                    {"bool":{
                      "must":[
                          {"term":{"itemID":"id100124"}},
                          {"term":{"price":40}}
                        ]
                    }
                  }
                ]
          }
        }
    }
    
    # 范围过滤
    GET lib4/items/_search
    {
      "post_filter": {
        "range": {
          "price": {
            "gte": 40,
            "lt": 50
          }
        }
      }
    }
    
    # 过滤非空
    GET lib4/items/_search
    {
      "query": {
        "bool": {
          "filter": {
            "exists": {
              "field": "price"
            }
          }
        }
      }
    }
    
    # sum
    GET lib4/items/_search
    {
      "size": 0,
      "aggs": {
        "price_sum": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
    
    # avg
    GET lib4/items/_search
    {
      "size": 0,
      "aggs": {
        "price_avg": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
    
    #max
    GET lib4/items/_search
    {
      "size": 0,
      "aggs": {
        "price_max": {
          "max": {
            "field": "price"
          }
        }
      }
    }
    
    #min
    GET lib4/items/_search
    {
      "size": 0,
      "aggs": {
        "price_min": {
          "min": {
            "field": "price"
          }
        }
      }
    }
    
    # cardinality:求基数
    GET lib4/items/_search
    {
      "size": 0,
      "aggs": {
        "price_card": {
          "cardinality": {
            "field": "price"
          }
        }
      }
    }
    
    # 分组
    GET lib4/items/_search
    {
      "size": 0,
      "aggs":{
        "price_group":{
          "terms": {
            "field": "price"
          }
        }
      }
    }
    
    #
    GET lib3/user/_search
    {
      "query": {
        "match": {
          "desc": "唱歌"
        }
      }, 
      "size": 0
      , "aggs": {
        "avg_group_by": {
          "terms": {
            "field": "age",
            "order": {"avg_of_age": "desc"}
          },
          "aggs": {
            "avg_of_age": {
              "avg": {
                "field": "age"
              }
            }
          }
        }
      }
    }
    
    # 复合查询
    GET lib3/user/_search
    {
     "query": {
       "bool": {
         "must": [
           {"match": {
             "desc": "唱歌"
           }}
         ],
         "must_not": [
           {"match": {
             "desc": "吃饭"
           }}
         ],
         "should": [
           {"match": {
             "address": "杭州"
           }},
           {"range": {
             "birthday": {
               "gte": "1999-03-05",
               "lte": "2000-03-05"
             }
           }}
         ]
        } 
      }
    }
    
    GET lib3/user/_search
    {
      "query": {
        "bool": {
          "must": [
            {"match": {
              "desc": "唱歌"
            }}
          ],
          "filter": {
            "bool": {
                "must":{
                "range": {
                   "birthday": {
                   "gte": "1999-03-05",
                   "lt": "2000-03-05"
                 }
                }
              }
            }
          }
        }
      }
    }
  • 相关阅读:
    基于设备的回声消除
    Libcurl细说
    抓包分析YY音频
    合唱音效解释
    EXCEL-COUNTIF()统计符合区间上的值个数
    EXCEL-对筛选出(单独手动隐藏行还是在统计范围内)的表格数据进行统计
    Perl字符串处理函数用法集锦
    Selenium-IDE,在网页上模拟人的操作
    Perl哈希%hash
    Perl if条件判断
  • 原文地址:https://www.cnblogs.com/FlyBlueSky/p/9623721.html
Copyright © 2011-2022 走看看