首页 > IT基础架构 > > ElasticSearch基本实操(1)
#ElasticSearch基本实操
PUT/lib/{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 0
}
}
}PUT /lib2
默认分片:5 复本数:1GET /lib/_settings
GET /lib2/_settingsGET _all/_settingsPUT /lib/user/1
{
"first_name":"Jane",
"last_name":"Smith",
"age":36,
"about":"I like to collect rock albums",
"interests":["music"]
}POST /lib/user/
{
"first_name":"Douglas",
"last_name":"Fir",
"age":23,
"about":"I like to build cabinets",
"interests":["forestry"]
}GET /lib/user/1
GET /lib/user/r9yyymwBwc87EyodP5HuGET /lib/user/1?_source=age,aboutPUT /lib/user/1
{
"first_name":"Jane",
"last_name":"Smith",
"age":30,
"about":"I like to collect rock albums",
"interests":["music"]
}POST /lib/user/1/_update
{
"doc":{
"age":33
}
}DELETE /lib/user/1DELETE /lib2GET /_mget
{
"docs":[
{
"_index":"lib",
"_type":"user",
"_id":1
},
{
"_index":"lib",
"_type":"user",
"_id":2
},
{
"_index":"lib",
"_type":"user",
"_id":3
}
]
}GET /_mget
{
"docs":[
{
"_index":"lib",
"_type":"user",
"_id":1,
"_source":["age","interests"]
},
{
"_index":"lib",
"_type":"user",
"_id":2,
"_source":["age"]
}
]
}GET /lib/user/_mget
{
"docs":[
{
"_id":1
},
{
"_type":"user",
"_id":2
}
]
}GET /lib/user/_mget
{
"ids":["1","2","3"]
}blulk的格式: {action:{metadata}}\n {requestbody}\n
action:(行为)
create:文档不存在时创建
update:更新文档
index:创建新文档或替换已有文档
delete:删除一个文档
metadata:_index, _type, _id
create和index区别: 如果数据存在,使用create操作失败,会提示文档已经存在,使用index则可以成功执行。
示例:
{
"delete": {
"_index": "lib",
"_type": "user",
"_id": "1"
}
}POST /lib2/books/_bulk
{"index":{"_id":1}}
{"title":"Java","price":55}
{"index":{"_id":2}}
{"title":"Html5","price":45}
{"index":{"_id":3}}
{"title":"PHP","price":35}
{"index":{"_id":4}}
{"title":"Python","price":50}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":"zhaosi"}
{"update":{"_index":"lib2","_type":"books","_id":4}} #更新
{"doc":{"price":50}}一般建议是1000-5000个文档,大小建议是5-15MB,默认不能超过100M,可以在es的配置文件(即$ES_HOME 下的config下的elasticsearch.yml)中。
ElasticSearch采用了乐观锁来保证数据的一致性,也就是说,当用户对document进行操作时,并不需要对该document作加锁和解锁的操作,只需要指定要操作的版本即可。
当版本号一致时,ElasticSearch会允许该操作顺利执行,而当版本号在冲突时,ElasticSearch会提示冲突并抛出异常(VersionConflictEngineException异常)。
内部版本控制:使用的是_verison
外部版本控制:elasticsearch在处理外部版本号时会与内部版本号的处理有些不同。
它不再是检查_version是否与请求中指定的数值相同,而是检查当前的_version是否比指定的数值小。 如果请求成功,那么外部的版本号就会被存储到文档中的_version中。
为了保持_version与外部版本控制的数据一致使用 version_type=external
使用version_type=external 外部版本号时,version的值必须大于原来version版本号
PUT /lib/user/3?version=6&version_type=external
{
"first_name":"Jane",
"last_name":"Smith",
"age":35,
"about":"I like to collect rock albums",
"interests":["music"]
}mapping定义了type中的每个字段的数据类型以及这些字段如何分词等相关属性。
创建索引的时候,可以预先定义字段的类型以及相关属性,这样就能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理字符串值等。
核心数据类型
text 和 keyword
text 类型被用来索引长文本,在建立索引前会将这些文本进行分司,转化为词的组合,建立索引,允许es来检索这些词语,text类型不能用来排序和聚合。
keyword类型不需要进行分词,可以被用来检索过滤、排序和聚合,keyword类型字段只能用本身来进行检索。
数字类型:long, integer, short, byte, double, float 日期型: date (不分词) String类型 (分词) 布尔型: boolean
......
dynamic:控制mapping的自动更新
data_detection:是否自动识别日期类型 data_detection:true(缺省)| false - dynamic和data_detection的详解:Elasticsearch dynamic mapping(动态映射) 策略
analyzer:指定分词器,默认分词器为standard analyzer "analyzer": "ik" - boost:字段级别的分数加权,默认值是1.0 "boost": 1.23 - fields:可以对一个字段提供多种索引模式,同一个字段的值,一个分词,一个不分词 "fields": {"raw": {"type": "string", "index": "not_analyzed"}} - ignore_above:超过100个字符的文本,将会被忽略,不被索引 "ignore_above": 100 - include_in_all:设置是否此字段包含在_all字段中,默认时true,除非index设置成no "include_in_all": true - null_value:设置一些缺失字段的初始化,只有string可以使用,分词字段的null值也会被分词 "null_value": "NULL" - position_increament_gap:影响距离查询或近似查询,可以设置在多值字段的数据上或分词字段上,查询时可以指定slop间隔,默认值时100 "position_increament_gap": 0 - search_analyzer:设置搜索时的分词器,默认跟analyzer是一致的,比如index时用standard+ngram,搜索时用standard用来完成自动提示功能 "search_analyzer": "ik" - similarity:默认时TF/IDF算法,指定一个字段评分策略,仅仅对字符串型和分词类型有效 "similarity": "BM25" - trem_vector:默认不存储向量信息,支持参数yes(term存储),with_positions(term+位置),with_offsets(term+偏移量),with_positions_offsets(term+位置+偏移量)对快速高亮fast vector highlighter能提升性能,但开启又会加大索引体积,不适合大数据量用 "trem_vector": "no"
GET /lib2/books/_mapping
{
"lib2": {
"mappings": {
"books": {
"properties": {
"index": {
"properties": {
"_id": {
"type": "long"
}
}
},
"price": {
"type": "long"
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}GET /lib2/books/_searchGET /lib2/books/_search?q=title:html5GET /lib2/books/_search?q=post_date:2019-09-01PUT /lib5/person/1
{
"name":"Tome",
"age":25,
"birthday":"1985-09-01",
"address":{
"country":"china",
"province":"guangdong",
"city":"shenzhen"
}
}GET /lib5/_settingsPUT /lib6
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"books":{
"properties":{
"title":{"type":"text"},
"name":{"type":"text","analyzer":"standard"},
"publish_date":{"type":"date","index":false},
"price":{"type":"double"},
"number":{"type":"integer"}
}
}
}
}
GET /lib6/_settings
GET /lib6/_mapping-
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":"u1",
"address:":"address1 guangzhou",
"age":31,
"birthday":"2011-09-08",
"interests":"duanlian aaaaaaaaaaaaaaaaaa"
}
PUT /lib3/user/2
{
"name":"u2",
"address:":"address shenzhen",
"age":32,
"birthday":"2012-09-08",
"interests":"duanlian bbbbbbbbbbbbbbb"
}
PUT /lib3/user/3
{
"name":"u3",
"address:":"address beijing",
"age":33,
"birthday":"2013-09-08",
"interests":"duanlian cccccccccccccccc"
}
PUT /lib3/user/4
{
"name":"u4",
"address:":"address shanghai",
"age":34,
"birthday":"2014-09-04",
"interests":"duanlian hejj44444444444444"
}GET /lib3/user/_search?q=name:u3GET /lib3/user/_search?q=interests:duanlian&sort=age:descGET /lib3/user/_search
{
"query":{
"term":{"name":"u1"}
}
}GET /lib3/user/_search
{
"query":{
"terms":{"interests":["aaaaaaaaaaaaaaaaaa","bbbbbbbbbbbbbbb"]}
}
}GET /lib3/user/_search
{
"from":0,
"size":2,
"query":{
"terms":{"interests":["duanlian"]}
}
}GET /lib3/user/_search
{
"query": {
"match": {
"name": "亮"
}
}
}
GET /lib3/user/_search
{
"query": {
"match": {
"age": "28"
}
}
}GET /lib3/user/_search
{
"query": {"match_all": {}}
}GET /lib3/user/_search
{
"query": {
"multi_match": {
"query": "28",
"fields": ["name","age"]
}}
}GET /lib3/user/_search
{
"query": {
"match_phrase": {
"interests": "跑步,听音乐"
}
}
}GET /lib3/user/_search
{
"_source": ["age","name","address","birthday"],
"query": {
"match": {
"interests": "跑步"
}
}
}
GET /lib3/user/_search
{
"query": {"match_all": {}},
"_source": {
"includes": ["name","address","interests"],
"excludes": ["age","birthday"]
}
}
GET /lib3/user/_search
{
"query": {"match_all": {}},
"_source": {
"includes": ["name","addr*","interests"],
"excludes": ["age","birthday"]
}
}GET /lib3/user/_search
{
"query": {
"match_all": {}
},
"sort":[
{
"age":{
"order": "desc"
}
}
]
}GET /lib3/user/_search
{
"query": {
"match_phrase_prefix": {
"name":{
"query": "明"
}
}
}
}GET /lib3/user/_search
{
"query": {
"range": {
"age": {
"gte": 30,
"lt": 35
}
}
}
}GET /lib3/user/_search
{
"query": {
"wildcard": {
"name":"小*"
}
}
}GET /lib3/user/_search
{
"query": {
"wildcard": {
"name":"w?m"
}
}
}GET /lib3/user/_search
{
"query": {
"fuzzy": {
"name":"明"
}
}
}value 查询的关键字
GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests":{
"value":"步"
}
}
}
}GET /lib3/user/_search
{
"query":{
"match":{
"interests":"唱歌"
}
},
"highlight": {
"fields": {
"interests": {}
}
}
}ik_max_word: 会将文本做最细粒度的拆分;尽可能多的拆分出词语 ik_smart:会做最粗粒度的拆分;已经分出的词语将不会再次被其它词语占有
DELETE /lib3
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"},
"interests":{"type":"text","analyzer":"ik_max_word"},
"birthday":{"type":"date"}
}
}
}
}
PUT /lib3/user/1
{
"name":"小明 XM",
"address":"广州中山大道",
"age":31,
"birthday":"2011-09-08",
"interests":"跑步,听音乐"
}
PUT /lib3/user/2
{
"name":"晚秋明 WQM",
"address":"广州黄鹂大道西",
"age":18,
"birthday":"2012-09-08",
"interests":"跑步,听音乐,唱歌"
}
PUT /lib3/user/3
{
"name":"明亮 ML",
"address":"中山市中山大道",
"age":28,
"birthday":"2013-09-08",
"interests":"跑步,听音乐,唱歌"
}
PUT /lib3/user/4
{
"name":"明明 MM",
"address":"深圳市宝安大道",
"age":35,
"birthday":"2014-09-08",
"interests":"听音乐,唱歌,跳舞"
}GET /lib3/user/_search
{
"query": {
"term": {
"name":"小明"
}
}
}filter是不计算相关性的,同时可以cache。因此,filter速度要快于query.
GET /lib2/books/_search
{
"post_filter": {"term":{"price":35}}
}
GET /lib2/books/_search
{
"post_filter": {"terms":{"price":[35,55]}}
}GET /lib2/books/_search
{
"query": {
"bool": {
"filter": [
{"term":{"title":"php"}}
]
}
}
}格式:
{"bool":{"must":[],"should":[],"must_not":[]}}must:必须满足的条件---and should:可以满足也可不满足的条件---or must_not:不需要满足的条件---not
GET /lib2/books/_search
{
"query": {
"bool": {
"should": [
{"term":{"title":"php"}},
{"term":{"title":"java"}}
],
"must_not": [
{"term":{"price":35}}
]
}
}
}GET /lib2/books/_search
{
"query": {
"bool": {
"should": [
{"term":{"title":"java"}},
{
"bool": {
"must": [
{"term":{"price":45}},
{"term":{"title":"html5"}}
]
}
}
]
}
}
}GET /lib3/user/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 55
}
}
}
}
}
}GET /lib3/user/_search
{
"query": {"bool": {
"filter": {
"exists": {
"field": "name"
}
}
}}
}PUT /lib7
{
"mappings": {
"items":{
"properties":{
"itemID":{
"type":"text",
"index":false
}
}
}
}
}
GET /lib7/_mapping来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31509949/viewspace-3169656/ ,如需转载,请注明出处,否则将追究法律责任。