ElasticSearch ——(三)基本操作 Getting Started

ElasticSearch增删改查

image


PUT /lib/
{
  "settings": {
    "index":{
      "number_of_shards":3,
      "number_of_replicas":0
    }
  }
}


GET _search
{
  "query": {
    "match_all": {}
  }
}

PUT /lib/
{
  "settings": {
    "index":{
      "number_of_shards":3,
      "number_of_replicas":0
    }
  }
}

GET /lib/_settings

GET _all/_settings

POST /lib/user/
{
  
  "first_name":"fz11j",
  "last_name":"liu",
  "age":121,
  "about":"h1ehe",
  "interest":["music","chess"]
  
}

GET /lib/user/1


GET /lib/user/6-klsWkB7CN0XtYTcnJU

GET /lib/user/1?_source=age,about


DELETE /lib/user/7OkosWkB7CN0XtYTNnKE

GET /_mget
{
  
  "docs":[
    
    {"_index":"lib",
      "_type":"user",
      "_id":"7ekks2kB7CN0XtYTZXLL"
    },
    {"_index":"lib",
      "_type":"user",
      "_id":"1"
    }
  
  ]
  
  
}
GET /lib/user/_mget
{
  
  "ids":[
    "7ekks2kB7CN0XtYTZXLL",
    "1"
  ]
  
  
}

Bulk API

bulk格式

{action:{metadata}}\n
{requestbody}\n

action行为
create、update、index、delete

index和create的区别
create文档不存在时创建
index创建新文档或替换已有文档

metadata
_index,_type,id

批量添加


POST /lib/user/_bulk
{"index":{"_id":3}}
{"title":"java","price":555}
{"index":{"_id":4}}
{"title":"python","price":5556}

删除没有请求体

POST /lib/user/_bulk
{"delete":{"_id":4}}
{"index":{"_id":6}}
{"title":"cpp","price":998}

_bulk操作建议添加1000-5000的文档,最大不超过100M

版本控制

ES用什么方式保证数据一致性?这里就要思考什么是数据一致性?

分布式下的一致性指的是,多个数据副本能否保持一致的特性。

如果对一个数据进行修改,所有的用户或线程都能读取到最新的值,则成为强一致性。

乐观锁与悲观锁

悲观锁是假定修改冲突会发生,是要保证每一次修改都是串行的,缺点是性能不好。
乐观锁是假定修改冲突不会发生。优点是性能好,缺点是,会导致一些修改失败。

ES保证数据一致性是使用乐观锁机制,用版本号来实现。有内部版本号和外部版本号之分。内部版本号,每次修改时,必须与当前的版本号一致才能成功。外部版本号,则是要大于当前版本好才能成功。

为什么要这样?我的猜想和减少内部和外部修改数据时的冲突。若版本号要求都是一致的,则内部和外部容易产生更多冲突。可能外部的修改,导致内部的失败。

Mapping

将定义的字段自动映射成对应的类型。ES是支持数据类型的。
text、keyword,text格式会进行分词,keyword不会进行分词,不进行分词的类型,就只能精确匹配。


GET /lib/user/_mapping
GET /lib/user/_search?q=title:Java

Mapping的Object类型

创建语句

POST /lib/user/33
{
  "title": "java编程思想",
  "publish_date": "2018-06-21",
  "price": 21.5,
  "author": {"address": "shandong","age": 21}
}

查询结果

    "author" : {
            "properties" : {
              "address" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "age" : {
                "type" : "long"
              }
            }
          },

基本查询(Query查询)

创建索引


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

插入文档

PUT /lib3/user/1
{
  "name":"zhangsan",
  "address":"shandong weihai",
  "age":90,
  "birthday":"1970-01-01",
  "interest":"xiaqi,paobu,pashan,kanshu"
}

Term 查询

GET /lib3/user/_search?q=name:liuying

另一种查询写法

GET /lib3/user/_search
{
  "query":{
    "term":{"name":"liuying"}
  }
}

查询结果

{
  "took" : 21,//耗时21ms
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,//总共命中文档数
    "max_score" : 0.18232156,//最高分数
    "hits" : [//命中文档
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "zhangsan",
          "address" : "shandong weihai",
          "age" : 90,
          "birthday" : "1970-01-01",
          "interest" : "xiaqi,paobu,pashan,kanshu"
        }
      },

Terms 查询(可以查询多个term)

from 从index开始(es默认从0算作第一个),size,总共返回的大小。version显示版本


GET /lib3/user/_search
{
  "version":true,
  "from":0,
  "size":1,
  "query":{
    "terms":{
      "interest":["paobu","xiaqi"]
    }
    
  }
}

排序查询


GET /lib3/user/_search?q=address:weihai&sort=age:desc

创建索引


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

插入文档

PUT /lib3/user/1
{
  "name":"zhangsan",
  "address":"shandong weihai",
  "age":90,
  "birthday":"1970-01-01",
  "interest":"xiaqi,paobu,pashan,kanshu"
}

查询

GET /lib3/user/_search?q=name:liuying

另一种查询写法

GET /lib3/user/_search
{
  "query":{
    "term":{"name":"liuying"}
  }
}

查询结果

{
  "took" : 21,//耗时21ms
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,//总共命中文档数
    "max_score" : 0.18232156,//最高分数
    "hits" : [//命中文档
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "zhangsan",
          "address" : "shandong weihai",
          "age" : 90,
          "birthday" : "1970-01-01",
          "interest" : "xiaqi,paobu,pashan,kanshu"
        }
      },

match查询

match查询和term查询的区别

match查询能感知到分词器的存在,而term不能。match查询会先将查询词进行分词,然后再去查找。

该语句会将查询内容分词,分成shandong,weihai,同时包括的分数最高。


GET /lib3/user/_search
{
  "query":{
    "match":{
      "address":"shandong weihai"
    }
  }
}


match_all

查询所有文档

GET /lib3/user/_search
{
  "query":{
    "match_all":{}
  }
}

multi_match

指定多个字段查询关键词

GET /lib3/user/_search
{
  "query":{
    "multi_match":{
      "query":"youyong",
      "fields":["interest","name"]
    }
  }
}

短语匹配 match_phrase

属于精准匹配



GET /lib3/user/_search
{
  "query":{
    "match_phrase":{
      "address":"shandong weihai"
    }
  },
  "_source":{
    "includes": ["name","address"],
    "excludes": ["age"]
  }
}

_source是声明字段,includes包含,excludes不包含

match_phrase_prefix前缀匹配

sort排序

GET /lib3/user/_search
{
  "query": {
    "match_phrase_prefix": {
      "address": "shandong"
    }
  },
  "_source": {
    "includes": [
      "name",
      "age"
    ],
    "excludes": [
      "address"
    ]
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

range


GET /lib3/user/_search
{
  "query": {
    "range":{
      "birthday": {
        "from": "1978-01-01",
        "to": "1993-01-01"
      }
    }
  },
  "_source": {
    "includes": [
      "name",
      "age","birthday"
    ],
    "excludes": [
      "address"
    ]
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

highlight 高亮


GET /lib3/user/_search
{
  "query": {
    "match":{
      "interest": "pashan"
    }
  },
  "_source": {
    "includes": [
      "name",
      "age","birthday","interest"
    ],
    "excludes": [
      "address"
    ]
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "highlight": {
    "fields": {"interest": {}}
  }
}


GET /lib3/user/_search
{
  "query":{
    "match_all":{}
  }
}

GET /lib3/user/_search
{
  "query":{
    "multi_match":{
      "query":"youyong",
      "fields":["interest","name"]
    }
  }
}


fuzzy 模糊查询


GET /lib3/user/_search
{
  "query": {
    "fuzzy":{
      "interest": "pasha"
    }
  },
  "_source": {
    "includes": [
      "name",
      "age","birthday","interest"
    ],
    "excludes": [
      "address"
    ]
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "highlight": {
    "fields": {"interest": {}}
  }
}


中文分词

ik的两个分词器,ik_max_word,ik_smart

创建索引

PUT /lib4
{
  "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"},
        "interests":{"type":"text","analyzer":"ik_max_word"},
        "birthday":{"type":"date"}
      }
    }
  }
}

进行查询

GET /lib4/user/_search
{
  "query": {
 
      "wildcard":{"name":"张*"}
  
  },
  "_source": {
    "includes": [
      "name",
      "age","birthday","interest"
    ],
    "excludes": [
      "address"
    ]
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "highlight": {
    "fields": {"interest": {}}
  }
}

Filter过滤

shoud 相当于or
must_not 相当于not
must and 相当于and


PUT /lib5/item/_bulk
{"index":{"_id":1}}
{"price":50,"itemID":"ID10001"}
{"index":{"_id":2}}
{"price":70,"itemID":"ID10002"}
{"index":{"_id":3}}
{"price":10,"itemID":"ID10003"}
{"index":{"_id":4}}
{"price":20,"itemID":"ID10004"}
{"index":{"_id":6}}
{"price":80,"itemID":"ID10005"}



GET /lib5/item/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "price": 20
        }
      }
    }
  }
}


GET /lib5/item/_search
{
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "price": [20,10]
        }
      }
    }
  }
}


GET /lib5/item/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "itemID":"ID10004"
        }
      }
    }
  }
}

GET /lib5/item/_mapping






GET /lib5/item/_search
{
  "query": {
    "bool": {
      "should":[
        {"term":{"price":20}},
        {"term":{"itemID":"id10001"}}
      ]
    }
  }
}

检查字段不为空,筛选在指定范围的文档


GET /lib5/item/_search
{
  "query":{
    "bool":{
      "filter":{
        "range":{
            "price":{
              "gt":25,
              "lt":100
            }
        }
      }
    }
  }
}


GET /lib5/item/_search
{
  "query":{
    "bool":{
      "filter":{
        "exists":{
            "field":"price"
        }
      }
    }
  }
}

参考文档

ES官网-快速开始

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值