您的位置:首页 > 其它

Elasticsearch常用操作命令大全

2017-05-06 10:29 531 查看
---------------------------

单模式下索引的创建,更新以及删除操作

---------------------------

---------------------------

初始化索引

创建索引之前可以对索引做初始化操作

比如指定shards数量以及replicas的数量

PUT http://localhost:9200/library/ {
“settings”:{
“index”:{
“number_of_shards”: 5,
“number_of_replicas”: 1
}
}
}


上面的number_of_replicas还可以换成:

blocks.read_only: 设为true,则当前索引只允许读,不允许写或更新

blocks.read: 设为true,则禁止读操作

blokcs.write: 设为true,则禁止写操作

blocks.metadata: 设为true,则禁止对metadata操作

---------------------------

创建一个索引

PUT /library/books/1
{
“title”: “Elasticsearch: The Definitive Guide”,
“name”: {
“first”: “Zachary”,
“last”: “Tong”
},
“publish_data”: “2015-02-06”,
“price”: “49.99”
}


---------------------------

ID可以选择不设置

POST /library/books/
{
“title”: “Elasticsearch: Blueprints”,
“name”: {
“first”: “Vineeth”,
“last”: “Mohan”
},
“publish_data”: “2015-06-06”,
“price”: “35.99”
}


---------------------------

通过ID获取文档信息

GET /library/books/1

GET /library/books/2

GET /library/books/AU_A-EDnU9duEv19TRm8

---------------------------

通过_source获取指定的字段

GET /library/books/1?_source=title

GET /library/books/1?_source=title,price

GET /library/books/1?_source

---------------------------

更新同一个ID下的文档,可以通过覆盖的方式更新

PUT /library/books/1
{
“title”: “Elasticsearch: The Definitive Guide”,
“name”: {
“first”: “Zachary”,
“last”: “Tong”
},
“publish_data”: “2015-02-06”,
“price”: “59.99”
}


-----------------------------

通过_update API的方式单独更新你想要更新的字段

POST /library/books/1/_update
{
“doc”: {
“price”: 10
}
}


------------------------------

删除一个文档的方法

DELETE /library/books/1

DELETE /library/books

DELETE /library

------------------------------

同时获取多个文档

GET /_megt{
“docs”: [
{
“_index”: “shakespeare”,
“_type”: “line”,
“_id”: 6,
“_source”: “play_name”
},
{
“_index”: “shakespeare”,
“_type”: “line”,
“_id”: 28,
“_source”: “play_name”
}
]
}


------------------------------

指定多个_source字段,数组的形式

GET /_mget
{
“docs”: [
{
“_index”: “shakespeare”,
“_type”: “line”,
“_id”: 6
},
{
“_index”: “shakespeare”,
“_type”: “line”,
“_id”: 28,
“_source”: [“play_name”, “speaker”, “text_entry”]
}
]
}


-------------------------------

获取相同index相同type下不同ID的文档

GET /shakespeare/line/_megt
{
“ids”: [“6”, “28”]
}


-------------------------------

多重模式

-------------------------------

-------------------------------

同时操作bulk

POST /library/books/_bulk
{“index”: {“_id”: 1}}
{“title”: “Elasticsearch: The Definitive Guide”, “price”: 5}
{“index”: {“_id”: 2}}
{“title”: “The ElasticSearch cookbook”, “price”: 12}


-------------------------------

还可以delete,update等操作

注意delete下面没有具体的request body

POST /library/books/_bulk
{“delete”: {“_index”: “library”, “type”: “books”, “_id”: “1”}}
{“create”: {“_index”: “music”, “_type”: “classical”, “_id”: “1”}}
{“title”: “Ave Verum Corpus”}
{“index”: {“_index”: “music”, “_type”: “classical”}}
{“title”: “Litaniac de Venerabili Altaris Sacromento”}
{“update”: {“_index”: “library”, “_type”: “books”, “_id”: “2”}}
{“doc”: {“price”: “18”}}


-------------------------------

Mapping映射

定义类型:

定义属性:

-------------------------------

建立映射

POST /library
{
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 1
},
“mappings”: {
“books”: {
“properties”: {
“title”: {“type”: “string”},
“name”: {“type”: “string”, “index”: “not_analyzed”},
“publish_date”: {“type”: “date”, “index”: “not_analyzed”},
“price”: {“type”, “double”},
“number”: {“type”: “integer”}
}
}
}
}


-------------------------------

动态映射

PUT /library
{
“mappings”: {
“books”: {
“dynamic”: “strict”,
“properties”: {
“title”: {“type”: “string”},
“name”: {“type”: “string”, “index”: “not_analyzed”},
“publish_date”: {“type”: “date”, “index”: “not_analyzed”},
“price”: {“type”, “double”},
“number”:
{
“type”: “object”,
“dynamic”: true
}
}
}
}
}


-------------------------------

获取某个索引的映射信息

GET /library/_mapping


-------------------------------

获取某个索引下某个type的映射信息

GET /library/_mapping/books


-------------------------------

获取这个集群内所有的映射信息

GET /_all/_mapping


-------------------------------

获取之个集群内某两个或多个type的映射信息

GET /_all/_mapping/books,bank_account


-------------------------------

更新修改Mapping映射

mapping一旦建立,就不能修改现有的字段映射

如果要推倒现有的映射,你得重新建立一个索引,然后重新定义映射

然后把之前索引里的数据导入到新建立的索引里

具体的方法:

1.给现有的索引里的数据定义一个别名,并且把现有的索引指向这个别名

2.运行:PUT /现有索引/_alias/别名A

3.新创建一个索引,定义好最新的映射

4.将别名指向新的索引,并且取消之前索引的指向

5.运行:

POST /_aliases
{
“actions”: [
{“remove”: {“index”: “现有索引名”, “alias”: “别名A”}},
{“add”: {“index”: “新建索引名”, “alias”: “别名A”}}
]
}


-------------------------------

删除映射

DELETE /library/books
DELETE /library/books/_mapping
DELETE /library/_mapping/books


-------------------------------

基本查询

-------------------------------

-------------------------------

简单的查询

指定index名以及type名的搜索

GET /library/books/_search?q=title:elasticsearch


指定index名没有type的搜索

GET /llibrary/_search?q=title:MongoDB


即没有index名也没有type名的搜索

GET /_search?q=title:elasticsearch


-------------------------------

term查询:查询某字段里有某个关键词的文档

GET /library/books/_search
{
“query”: {
“term”: {
“preview”: “elasticsearch”
}
}
}


terms查询:查询某个字段里有多个关键词的文档

minimum_match:最小匹配集:1-说明两个关键词里最少有一个

2-说明文档里这两个关键词都得存在

GET /library/books/_search
{
“query”: {
“terms”: {
“preview”: [“elasticsearch”, “book”],
“minimum_match”: 1
}
}
}


--------------------------------

控制查询返回的数量

from和size

相当于MySQL里的limit

form:从哪个结果开始返回

size:定义返回最大的结果数

GET /library/books/_search?q=title:elasticsearch
GET /library/books/_search
{
“from”: 1,
“size”: 2,
“query”: {
“term”: {
“title”: “elasticsearch”
}
}
}


-------------------------------

返回版本号_version

GET /library/books/_search
{
“version”: true,
“query”: {
“term”: {
“perview”: “elasticsearch”
}
}
}


-------------------------------

match查询

match查询可接受文字,数字日期等数据类型

match跟term的区别是,match查询的时候,elasticsearch会根据你给定的字段提供合适的分析器,

而term查询不会有分析器分析的过程

GET /library/books/_search
{
“query”: {
“match”: {
“preview”: “elasticsearch”
}
}
}


通过match_all查询

查询指定索引下的所有文档

GET /library/books/_search
{
“query”: {
“match_all”: {}
}
}


通过match_phrase查询

短语查询,slop定义的是关键词之间隔多少未知单词

GET /library/books/_search
{
“query”: {
“match_phrase”: {
“preview”: {
“query”: “elasticsearch, distributed”,
“slop”: 1
}
}
}
}


muti_match查询

可以指定多个字段

比如查询title和preview这两个字段里都包含Elasticsearch关键词的文档

GET /library/books/_search
{
“query”: {
“multi_match”: {
“query”: “Elasticsearch”,
“fields”: [“title”, “preview”]
}
}
}


---------------------------------

指定返回的字段

注意只能返连回store为yes的字段

GET /library/books/_search
{
“fields”: [“preview”, “title”]
“query”: {
“match”: {
“preview”: “elasticsearch”
}
}
}


---------------------------------

通过partial_fields控制加载的字段

GET /library/books/_search
{
“partial_fields”: {
“partial”: {
“include”: [“preview”],
“exclude”: [“title”, “price”]
}
},
“query”: {
“match_all”: {}
}
}


---------------------------------

排序

通过sort把结果排序

GET /library/books/_search
{
“query”: {
“match_all”: {}
},
sort: [
{
“price”: {
“order”: “desc”
}
}
]
}


-------------------------------

prefix前缀匹配查询

GET /library/books/_search
{
“query”: {
“prefix”: {
“title”: {
“value”: “r”
}
}
}
}


----------------------------

range查询:范围查询

有from,to,include_lower,inculde_upper,boost这些参数

include_lower:是否包含范围的左边界,默认是true

include_upper:是否包含范围的右边界,默认是true

GET /library/books/_search
{
“query”: {
“range”: {
“publish_date”: {
“from”: “2015-01-01”,
“to”: “2015-06-30”
}
}
}
}
GET /library/books/_search
{
“query”: {
“range”: {
“price”: {
“from”: “10”,
“to”: “20”,
“include_lower”: true,
“include_upper”: false
}
}
}
}


------------------------------

wildcard查询:允许你使用通配符*和?不进行查询

注意:这个查询很影响性能

GET /library/books/_search
{
“query”: {
“wildcard”: {
“preview”: “rab*”
}
}
}


------------------------------

fuzzy模糊查询

value:查询的关键字

boost:设置查询的权值,默认为1.0

min_similarity:设置匹配的最小相似度

默认值为0.5; 对于字符串,取值为0-1; 对于数值,取值可能大于1; 对于日期型,取值为1d,2d,1m这样,1d就代表一天

prefix_length: 指明区分项的共同前缀长度,默认是0

max_expansions:指明查询中的词项可扩展的数目,默认可以无限大

GET /library/books/_search
{
“query”: {
“fuzzy”: {
“preview”: {
“value”: “rabit”,
“min_similarity”: 0.5
}
}
}
}


fuzzy_like_this查询

查询得到与给定内容相似的所有文档

fileds: 字段组,默认是_all

like_text: 设置关键词

ignore_tf: 设置忽略词项的频次,默认是false

max_query_terns: 指明在生成的查询中查询词项的最大数目,默认是25

min_similarity: 指明区分词项最小的相似度,默认是0.5

prefix_length: 指明区分词项共同前缀的长度,默认是0

boost: 设置权值,默认是1.0

analyze: 指明用于分析给定内容的分析器

GET /library/books/_search
{
“query”: {
“fuzzy_like_this”: {
“fields”: [“preview”],
“like_text”: “open source software”,
“min_similarity”: 0.5,
“prefix_length”: 0.2
}
}
}


fuzzy_like_this_field查询

只作用在一个字段里

其他与fuzzy_like_this功能一样

GET /library/books/_search
{
“query”: {
“fuzzy_like_this_field”: {
“preview”: {
“like_text”: “open source software”,
“min_similarity”: 0.5,
“prefix_length”: 0.2
}
}
}
}


more_like_this查询

fields: 定义字段组,默认是_all

like_text: 定义要查询的关键词

precent_terms_to_match: 该参数指明一个文档必须匹配多大比例的词项才被视为相似。默认值是0.3,意思是30%的比例

min_term_freq: 该参数指明在生成的查询中查询词项的最大数目,默认值25

stop_words: 该参数指明将被忽略的单词集合

min_doc_freq: 该参数指明词项应至少在多少个文档中出现才不会被忽略,默认是5

max_doc_freq: 该参数指明出现词项的最大数目,以避免词项被忽略,默认是无限大

min_word_len: 该参数指明单个单词的最大长度,高于该值的单词将被忽略,默认是无限大

max_word_len: 该参数提升每个单词的权重时使用的权值。默认是1

boost: 指明提升一个查询的权值。默认为1.0

analyer: 指定用于分析的分析器

GET /library/books/_search
{
“query”: {
“more_like_this”: {
“fields”: [“preview”],
“like_text”: “Apache open source”,
“min_term_freq”: 1,
“min_doc_freq”: 1
}
}
}


more_like_this_field查询

只作用在一个字段里

其他与more_like_this功能一样

GET /library/books/_search
{
“query”: {
“more_like_this_field” {
“preview”: {
“like_text”: “Apache open source”,
“min_trem_freq”: 1,
“min_doc_freq”: 1
}
}
}
}


-------------------------------

filter过滤查询

SELECT document FROM products WHERE price=20


filtered查询价格是20的商品

GET /store/products/_search
{
“query”: {
“filtered”: {
“query”: {
“match_all”: {}
},
“filter”: {
“term”: {
“price”: 20
}
}
}
}
}


也可以指定多个值

GET /store/products/_search
{
“query”: {
“filtered”: {
“filter”: {
“terms”: {
“price”: [10, 20]
}
}
}
}
}


-----------------------------

bool过滤查询,可以做组合过滤查询

SELECT product FROM products WHERE (price = 20 OR productID = “SD1002136”) AND (price != 30)


类似的,elasticsearch也有and, or, not这样的组合条件的查询方式

{
“bool”: {
“must”: [],
“should”: []’
“must_not”: []
}


must: 条件必须满足,相当于AND

should: 条件可以满足也可以不满足,相当于OR

must_not: 条件不需要满足,相当于NOT

GET /store/products/_search
{
“query”: {
“filtered”: {
“filter”: {
“bool”: {
“should”: [
{“term”: {“price”: 20}},
{“term”: {“productID”: “SD1002136”}}
],
“must_not”: {
“term”: {“price”: 30}
}
}
}
}
}
}


嵌套查询

SELECT document FORM products WHERE productID=”SD1002136” OR (productID=”SD4535233” AND price=30)


GET /store/products/_search
{
“query”: {
“filtered”: {
“filter”: {
“bool”: {
“should”: [
{“term”: {“productID”: “SD1002136”}},
{“bool”: {
“must”: [
{“term”: {“productID”: “SD4535233”}},
{“term”: {“price”: 30}}
]
}}
]
}
}
}
}
}


另外一种and, or, not查询

没有bool,直接使用and, or, not

查询价格既是10元,productID又为SD1002136的结果

GET /store/products/_search
{
“query”: {
“filtered”: {
“filter”: {
“and”:[
{
“term”: {
“productID”: “SD1002136”
}
}
]
},
“query”: {
“match_all”: {}
}
}
}
}


range范围过滤

SELECT document FROM products WHRE price BETWEEN 20 AND 40


gt: > 大于

lt: < 小于

gte: >= 大于等于

lte: <= 小于等于

GET /store/products/_search
{
“query”: {
“filtered”: {
“filter”: {
“range”: {
“price”: {
“gt”: 20,
“lt”: 40
}
}
}
}
}
}


--------------------------------

处理null空值的方法

SELECT tags FROM test WHERE tags IS NOT NULL
SELECT tags FROM test WHERE tags IS NULL


GET /test_index/test/_search
{
“query”: {
“filtered”: {
“filter”: {
“exists”: {field: “tags”}
}
}
}
}
GET /test_index/test/_search
{
“query”: {
“filtered”: {
“filter”: {
“missing”: {“field”: “tags”}
}
}
}
}


--------------------------------

跨索引查询

GET /_search
{
“query”: {
“indices”: {
“indices”: [library],
“query”: {
“term”: {
“title”: “elasticsearch”
}
},
“no_match_query”: {
“term”: {
“price”: 10
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: