elasticsearch基本操作

GET _search
{
  "query": {
    "match_all": {}
  }
}
#创建索引 指定分片和副本
PUT /lib/
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 0
  }
}
#创建索引
PUT lib2

#查看索引的配置
GET /lib/_settings
GET /lib2/_settings

GET _all/_settings
#添加文档put方式指定id
PUT /lib/user/1
{
  "first_name":"jane",
  "last_name":"Smith",
  "age":32,
  "about":"I like to collect rock albums",
  "interests":["music"]
}
#添加文档,不指定id用post请求
POST /lib/user
{
   "first_name":"Dougla",
  "last_name":"Fir",
  "age":32,
  "about":"I like to collect rock albums",
  "interests":["song"]
}
#根据搜索文档
GET /lib/user/1
GET /lib/user/4bkvl3EBP7ndcPt4ZWWc
#查询age和about字段
GET /lib/user/1?_source=age,about
#修改 覆盖
PUT /lib/user/1
{
  "first_name":"jane",
  "last_name":"Smith",
  "age":38,
  "about":"I like to collect rock albums",
  "interests":["music"]
}
#update修改
POST /lib/user/1/_update
{
  "doc":{
    "age":30
  }
}
#删除文档
DELETE /lib/user/1
DELETE /lib2
#MultiGet批量获取文档
GET /_mget
{
  "docs":[
    {
      "_index":"lib",
      "_type":"user",
      "_id":1
    },
    {
      "_index":"lib",
      "_type":"user",
      "_id":2
    },
    {
      "_index":"lib",
      "_type":"user",
      "_id":3
    }
  ]
}
DELETE /lib2

#bulk实现批量操作
POST /lib2/books/_bulk
{"index":{"_id":1}}
{"title":"java","price":55}
{"index":{"_id":2}}
{"title":"html","price":45}
{"index":{"_id":3}}
{"title":"php","price":35}
{"index":{"_id":4}}
{"title":"python","price":25}

GET /lib2/books/_mget
{
  "ids":["1","2","3","4"]
}

POST /lib2/books/_bulk
{"delete":{"_index":"lib2","_type":"books","_id":4}}
{"create":{"_index":"tt","_type":"ttt","_id":"100"}}
{"name":"lisi"}
{"index":{"_index":"tt","_type":"ttt"}}
{"name":"zhangsan"}
{"update":{"_index":"lib2","_type":"books","_id":"4"}}
{"doc":{"price":58}}


PUT /myindex/article/1
{
  "post_date":"2018-05-10",
  "title":"Java",
  "content":"java is the best language",
  "author_id":119
}
PUT /myindex/article/2
{
  "post_date":"2018-01-10",
  "title":"html",
  "content":"I like html",
  "author_id":120
}
PUT /myindex/article/3
{
  "post_date":"2018-05-12",
  "title":"es",
  "content":"Es is distributed document store",
  "author_id":110
}

PUT /myindex/_doc/1
{
  "post_date":"2018-05-10",
  "title":"Java",
  "content":"java is the best language",
  "author_id":119
}


GET /myindex/_mapping


GET /myindex/article/_search


GET /myindex/article/_search?q=post_date:2018-05-10

PUT /lib5/person/1
{
  "name":"Tom",
  "age":25,
  "birthday":"1985-12-12",
  "address":{
    "country":"China",
    "province":"guangzhou",
    "city":"shenzhen"
  }
}

GET /lib5/_mapping

PUT /lib3
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties":{
      "name":{"type":"text"},
      "address":{"type":"text"},
      "age":{"type":"text"},
      "birthday":{"type":"date"}
    }
  }
}
GET /lib3/_mapping

PUT /lib3/_doc/1
{
  "name":"zhaoliu2",
  "address":"hei long jiang sheng tie ling shi",
  "age":50,
  "birthday":"1970-12-18"

}

GET /lib3/_doc/1

DELETE /lib4

POST /lib4/items/_bulk
{"index": {"_id":1}}
{"price":40,"itemID":"ID100123"}
{"index": {"_id":2}}
{"price":50,"itemID":"ID100124"}
{"index": {"_id":3}}
{"price":30,"itemID":"ID100125"}
{"index": {"_id":4}}
{"price":25,"itemID":"ID100126"}
{"index": {"_id":5}}
{"price":null,"itemID":"ID100127"}


GET /lib4/items/1
GET /lib4/_search
{
  "query":{
    "bool":{
      "filter": [
        {"term": {
          "itemID": "id100123"
        }}
      ]
    }
  }
}
GET lib4/items/_search
{
  "query": {
    "bool": {
      "filter": [
        {"range": {
          "price": {
            "gte": 25,
            "lte": 45
          }
        }}
      ]
    }
  }
}
GET /lib4/items/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
          "itemID":"id100123"
        }},
        {
          "bool": {"must": [
            {"term": {
              "itemID": "id100124"
            }},
            {"term": {
              "price": {
                "value": "40"
              }
            }}
          ]}
        }
      ]
    }
  }
}

GET /lib4/items/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
          "itemID": {
            "value": "id100123"
          }
        }},
        {
          "bool":{
            "must": [
              {"term":{"itemID":"100124"}},
              {"term":{"price":40}
              }
            ]
          }
        }
      ]
    }
  }
}


GET lib4/items/_search
{
  "query":{
    "bool": {
      "filter": [
        {"exists": {
          "field": "price"
        }}
      ]
    }
  }
}
GET /lib4/items/_search
{
  "query":{
    "bool": {
      "filter": [
        {"exists": {
          "field": "price"
        }}
      ]
    }
  }
}


#聚合查询
GET /lib4/items/_search
{
  "size": 0, 
  "aggs": {
    "price_of_sum": {
      "sum": {"field": "price"}
    }
  }
}
GET /lib4/items/_search
{
  "size": 0, 
  "aggs": {
    "price_of_min": {
      "min": {"field": "price"}
    }
  }
}

GET /lib4/items/_search
{
  "size": 0, 
  "aggs": {
    "price_of_max": {
      "max": {"field": "price"}
    }
  }
}
GET /lib4/items/_search
{
  "size": 0, 
  "aggs": {
    "price_of_avg": {
      "avg": {"field": "price"}
    }
  }
}


#cardinality求基数
GET /lib4/items/_search
{
  "size": 0, 
  "aggs": {
    "price_of_cardi": {
      "cardinality": {"field": "price"}
    }
  }
}
#分组
GET /lib4/items/_search
{
  "size": 0, 
  "aggs": {
    "price_of_group": {
      "terms": {"field": "price"}
    }
  }
}


GET /lib3/_user/_search
{
  "query":{
    "match": {"interests": "changge"}
  }
}
#对那些有唱歌兴趣的用户按年龄分组
GET /lib3/user/_search
{
  "query": {
    "match":{
      "interests":"changge"
    }
  },
  "aggs": {
    "age_of_group": {
      "terms": {
        "field": "age",
        "order": {
          "age_of_avg": "desc"
        }
      },
      "aggs": {
        "age_of_avg": {
          "avg": {"field": "age"}
        }
      }
    }
  }
}

GET /lib3/usr/_search
{
  "query": {
    "bool": {
      "must": {
        "match":{"interests":"changge"}
      },
      "must_not": [
        {"match":{"interests":"lvyou"}}
      ],
      "should": [
        {"match": {
            "address": "beijing"
          }
        },{
            "range":{
              "birthday":{
                "gte":"1996-01-01"
              }
            }
          }
      ]
    }
  }
}

GET /lib3/usr/_search
{
  "query": {
    "bool": {
      "must": [
        {"match":{"interests":"changge"}}
      ],
      "must_not": [
        {"match":{"interests":"lvyou"}}
      ],
      "should": [
        {"match": {
          "address": "bei jing"
        }}
      ],
      "filter": [
        {"range": {
          "birthday": {
            "gte": "1996-01-01"
          }
        }}
      ]
    }
  }
}

GET /lib3/usr/_search
{
  "query": {
    "bool": {
      "must": [
        {"match":{"interests":"changge"}}
      ],
      "must_not": [
        {"match":{"interests":"lvyou"}}
      ],
      "should": [
        {"match":{"address":"bei jing"}}
      ],
      "filter": {
        "bool": {
          "must": [
            {"range":{"birthday":{"gte":"1990-01-01"}}},
            {"range":{"age":{"lte":30}}}
          ],
          "must_not":[
            {"term":{"age":29}}  
          ]
        }
      }
    }
  }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值