---------------------------
单模式下索引的建立,更新以及删除操做
---------------------------数组
---------------------------
初始化索引
建立索引以前能够对索引作初始化操做
好比指定shards数量以及replicas的数量app
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操做elasticsearch
---------------------------
建立一个索引ide
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-EDnU9duEv19TRm8ui
---------------------------
经过_source获取指定的字段
GET /library/books/1?_source=title
GET /library/books/1?_source=title,price
GET /library/books/1?_sourcethis
---------------------------
更新同一个ID下的文档,能够经过覆盖的方式更新code
PUT /library/books/1 { “title”: “Elasticsearch: The Definitive Guide”, “name”: { “first”: “Zachary”, “last”: “Tong” }, “publish_data”: “2015-02-06”, “price”: “59.99” }
-----------------------------
经过_update API的方式单独更新你想要更新的字段orm
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 } } } } }