觸類旁通Elasticsearch之吊打同行系列:操作篇
點擊上方藍色字體,選擇“設(shè)為星標”

一、索引數(shù)據(jù)
1. 使用映射定義文檔
curl '172.16.1.127:9200/get-together/_doc/_mapping?pretty'索引新文檔時ES可以自動創(chuàng)建映射,例如下面的命令會自動創(chuàng)建my_index索引,在其中索引一個ID為1的文檔,該文檔有name和date兩個字段:
curl -XPUT '172.16.1.127:9200/my_index/_doc/1?pretty' -H 'Content-Type: application/json' -d '{? "name": "Late Night with Elasticsearch",? "date": "2013-10-25T19:00"}'
curl '172.16.1.127:9200/my_index/_doc/_mapping?pretty'{? "my_index" : {? ? "mappings" : {? ? ? "_doc" : {? ? ? ? "properties" : {? ? ? ? ? "date" : {? ? ? ? ? ? "type" : "date"? ? ? ? ? },? ? ? ? ? "name" : {? ? ? ? ? ? "type" : "text",? ? ? ? ? ? "fields" : {? ? ? ? ? ? ? "keyword" : {? ? ? ? ? ? ? ? "type" : "keyword",? ? ? ? ? ? ? ? "ignore_above" : 256? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? }? ? ? ? }? ? ? }? ? }? }}
curl -XPUT '172.16.1.127:9200/my_index?pretty'curl -XPUT '172.16.1.127:9200/my_index/_mapping/_doc?pretty' -H 'Content-Type: application/json' -d '{? "_doc": {? ? "properties": {? ? ? "date": {? ? ? ? "type": "date"? ? ? },? ? ? "name": {? ? ? ? "type": "text",? ? ? ? "fields": {? ? ? ? ? "keyword": {? ? ? ? ? ? "type": "keyword",? ? ? ? ? ? "ignore_above": 256? ? ? ? ? }? ? ? ? }? ? ? }? ? }? }}'
curl -XPUT '172.16.1.127:9200/my_index/_mapping/_doc?pretty' -H 'Content-Type: application/json' -d '{? "_doc": {? ? "properties": {? ? ? "host": {? ? ? ? "type": "text"? ? ? }? ? }? }}'
{? "my_index" : {? ? "mappings" : {? ? ? "_doc" : {? ? ? ? "properties" : {? ? ? ? ? "date" : {? ? ? ? ? ? "type" : "date"? ? ? ? ? },? ? ? ? ? "host" : {? ? ? ? ? ? "type" : "text"? ? ? ? ? },? ? ? ? ? "name" : {? ? ? ? ? ? "type" : "text",? ? ? ? ? ? "fields" : {? ? ? ? ? ? ? "keyword" : {? ? ? ? ? ? ? ? "type" : "keyword",? ? ? ? ? ? ? ? "ignore_above" : 256? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? }? ? ? ? }? ? ? }? ? }? }}
curl -XPUT '172.16.1.127:9200/my_index/_mapping/_doc?pretty' -H 'Content-Type: application/json' -d '{? "_doc": {? ? "properties": {? ? ? "host": {? ? ? ? "type": "long"? ? ? }? ? }? }}'
{? "error" : {? ? "root_cause" : [? ? ? {? ? ? ? "type" : "remote_transport_exception",? ? ? ? "reason" : "[node126][172.16.1.126:9300][indices:admin/mapping/put]"? ? ? }? ? ],? ? "type" : "illegal_argument_exception",? ? "reason" : "mapper [host] of different type, current_type [text], merged_type [long]"? },? "status" : 400}
2. 基本數(shù)據(jù)類型
curl -XPUT '172.16.1.127:9200/my_index/_doc/1?pretty' -H 'Content-Type: application/json' -d '{? "name": "Late Night with Elasticsearch",? "date": "2013-10-25T19:00"}'
curl '172.16.1.127:9200/my_index/_doc/_search?pretty' -H 'Content-Type: application/json' -d '{? "query": {? ? "query_string": {? ? ? "query": "late"? ? }? }}'

curl '172.16.1.127:9200/my_index/_doc/_search?pretty' -H 'Content-Type: application/json' -d '{? "query": {? ? "term": {? ? ? "name.keyword": "late"? ? }? }}'
curl '172.16.1.127:9200/my_index/_doc/_search?pretty' -H 'Content-Type: application/json' -d '{? "query": {? ? "term": {? ? ? "name.keyword": "Late Night with Elasticsearch"? ? }? }}'
使用預(yù)定義的日期格式。參見https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#built-in-date-formats
設(shè)置自己定制的格式。
curl -XPUT '172.16.1.127:9200/my_index/_mapping/_doc?pretty' -H 'Content-Type: application/json' -d '{? "properties": {? ? "next_event": {? ? ? "type": "date",? ? ? "format": "MMM DD YYYY"? ? }? }}'
curl -XPUT '172.16.1.127:9200/my_index/_doc/1?pretty' -H 'Content-Type: application/json' -d '{? "name": "Elasticsearch News",? "first_occurence": "2011-04-03",? "next_event": "Oct 25 2013"}'
curl '172.16.1.127:9200/my_index/_doc/_mapping?pretty'{? "my_index" : {? ? "mappings" : {? ? ? "_doc" : {? ? ? ? "properties" : {? ? ? ? ? "date" : {? ? ? ? ? ? "type" : "date"? ? ? ? ? },? ? ? ? ? "first_occurence" : {? ? ? ? ? ? "type" : "date"? ? ? ? ? },? ? ? ? ? "host" : {? ? ? ? ? ? "type" : "text"? ? ? ? ? },? ? ? ? ? "name" : {? ? ? ? ? ? "type" : "text",? ? ? ? ? ? "fields" : {? ? ? ? ? ? ? "keyword" : {? ? ? ? ? ? ? ? "type" : "keyword",? ? ? ? ? ? ? ? "ignore_above" : 256? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? },? ? ? ? ? "next_event" : {? ? ? ? ? ? "type" : "date",? ? ? ? ? ? "format" : "MMM DD YYYY"? ? ? ? ? }? ? ? ? }? ? ? }? ? }? }}
boolean類型用于存儲文檔中的true/false,例如:
curl -XPUT '172.16.1.127:9200/my_index/_doc/1?pretty' -H 'Content-Type: application/json' -d '{? "name": "Broadcasted Elasticsearch News",? "downloadable": true}'
downloadable字段被自動地映射為boolean,在Lucene的索引中被存儲為T和F。和date一樣,ES解析源文檔中提供的值,將true和false分別轉(zhuǎn)化為T和F。(5)數(shù)組
所有基本類型都支持數(shù)組,無須修改映射,既可以使用單一值,也可以使用數(shù)組:
curl -XPUT '172.16.1.127:9200/blog/posts/1?pretty' -H 'Content-Type: application/json' -d '{? "tags": ["first", "initial"]}'curl -XPUT '172.16.1.127:9200/blog/posts/2?pretty' -H 'Content-Type: application/json' -d '{"tags": "second"}'curl 'localhost:9200/blog/_mapping/posts?pretty'
{? "blog" : {? ? "mappings" : {? ? ? "posts" : {? ? ? ? "properties" : {? ? ? ? ? "tags" : {? ? ? ? ? ? "type" : "text",? ? ? ? ? ? "fields" : {? ? ? ? ? ? ? "keyword" : {? ? ? ? ? ? ? ? "type" : "keyword",? ? ? ? ? ? ? ? "ignore_above" : 256? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? }? ? ? ? }? ? ? }? ? }? }}
3. 多字段
curl -XPUT '172.16.1.127:9200/blog/_mapping/posts?pretty' -H 'Content-Type: application/json' -d '{? "posts": {? ? "properties": {? ? ? "tags": {? ? ? ? "type": "text",? ? ? ? "index": true,? ? ? ? "fields": {? ? ? ? ? "verbatim": {? ? ? ? ? ? "type": "text",? ? ? ? ? ? "index": false? ? ? ? ? }? ? ? ? }? ? ? }? ? }? }}'
curl -XPUT '172.16.1.127:9200/blog/_mapping/posts?pretty' -H 'Content-Type: application/json' -d '{? "posts": {? ? "properties": {? ? ? "tags": {? ? ? ? "type": "text"? ? ? }? ? }? }}'curl 'localhost:9200/blog/_mapping/posts?pretty'
{? "blog" : {? ? "mappings" : {? ? ? "posts" : {? ? ? ? "properties" : {? ? ? ? ? "tags" : {? ? ? ? ? ? "type" : "text",? ? ? ? ? ? "fields" : {? ? ? ? ? ? ? "keyword" : {? ? ? ? ? ? ? ? "type" : "keyword",? ? ? ? ? ? ? ? "ignore_above" : 256? ? ? ? ? ? ? },? ? ? ? ? ? ? "verbatim" : {? ? ? ? ? ? ? ? "type" : "text",? ? ? ? ? ? ? ? "index" : false? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? }? ? ? ? }? ? ? }? ? }? }}
4. 預(yù)定義字段
通常不用部署預(yù)定義的字段。
字段名揭示了相關(guān)字段的功能。
總是以下劃線(_)開頭。
curl '172.16.1.127:9200/get-together/_doc/1?pretty'{? "_index" : "get-together",? "_type" : "_doc",? "_id" : "1",? "_version" : 3,? "found" : true,? "_source" : {? ? "relationship_type" : "group",? ? "name" : "Denver Clojure",? ? "organizer" : [? ? ? "Daniel",? ? ? "Lee"? ? ],? ? "description" : "Group of Clojure enthusiasts from Denver who want to hack on code together and learn more about Clojure",? ? "created_on" : "2012-06-15",? ? "tags" : [? ? ? "clojure",? ? ? "denver",? ? ? "functional programming",? ? ? "jvm",? ? ? "java"? ? ],? ? "members" : [? ? ? "Lee",? ? ? "Daniel",? ? ? "Mike"? ? ],? ? "location_group" : "Denver, Colorado, USA"? }}
curl -XGET '172.16.1.127:9200/get-together/_search?pretty' -H 'Content-Type: application/json' -d '{? "query": {? ? "terms": {? ? ? "_id": [? ? ? ? "1"? ? ? ]? ? }? },? "_source": [? ? "name",? ? "organizer"? ]}'
{? "took" : 6,? "timed_out" : false,? "_shards" : {? ? "total" : 2,? ? "successful" : 2,? ? "skipped" : 0,? ? "failed" : 0? },? "hits" : {? ? "total" : 1,? ? "max_score" : 1.0,? ? "hits" : [? ? ? {? ? ? ? "_index" : "get-together",? ? ? ? "_type" : "_doc",? ? ? ? "_id" : "1",? ? ? ? "_score" : 1.0,? ? ? ? "_source" : {? ? ? ? ? "organizer" : [? ? ? ? ? ? "Daniel",? ? ? ? ? ? "Lee"? ? ? ? ? ],? ? ? ? ? "name" : "Denver Clojure"? ? ? ? }? ? ? }? ? ]? }}
select name, organizer from get-together where id=1;_source字段存儲所有信息,而_all是索引所有的信息。_all字段將所有字段的值連接成一個大字符串,使用空格作為分隔符,然后對其進行分析和索引,但不進行存儲。這意味著可以把它作為搜索條件,但不能返回它。_all字段允許在不知道哪個字段包含值的情況下搜索文檔中的值。? ? ? ??
curl '172.16.1.127:9200/get-together/_search?q=elasticsearch&pretty'curl -X GET '172.16.1.127:9200/get-together/_search?pretty' -H 'Content-Type: application/json' -d'{? "query": {? ? "query_string": {? ? ? "query": "elasticsearch"? ? }? }}'
ES用這三個字段識別單個文檔。ID可以由用戶手動提供:
curl -XPUT '172.16.1.127:9200/manual_id/_doc/1st?pretty' -H 'Content-Type: application/json' -d '{? "name": "Elasticsearch Denver"}'
{? "_index" : "manual_id",? "_type" : "_doc",? "_id" : "1st",? "_version" : 1,? "result" : "created",? "_shards" : {? ? "total" : 2,? ? "successful" : 1,? ? "failed" : 0? },? "_seq_no" : 0,? "_primary_term" : 1}
curl -XPOST '172.16.1.127:9200/logs/_doc/?pretty' -H 'Content-Type: application/json' -d '{"message": "I have an automatic id"}'
{? "_index" : "logs",? "_type" : "_doc",? "_id" : "iEbXOmgBWHJVyzwYQ9ho",? "_version" : 1,? "result" : "created",? "_shards" : {? ? "total" : 2,? ? "successful" : 1,? ? "failed" : 0? },? "_seq_no" : 0,? "_primary_term" : 1}
curl '172.16.1.127:9200/_search?q=_index:get-together&pretty'curl '172.16.1.127:9200/_search?q=_index:blog&pretty'
二、更新數(shù)據(jù)
update get-together set organizer='Roy' where id=2;
從_source字段檢索現(xiàn)有文檔。
進行指定的修改。
刪除舊的文檔,在其原有位置索引新的文檔。
1. 使用更新API
curl -XPOST '172.16.1.127:9200/get-together/_doc/2/_update?pretty' -H 'Content-Type: application/json' -d '{? "doc": {? ? "organizer": "Roy"? }}'
curl -XPOST '172.16.1.127:9200/get-together/_doc/2/_update?pretty' -H 'Content-Type: application/json' -d '{? "doc": {? ? "organizer": "Roy"? },? "upsert": {? ? "name": "Elasticsearch Denver",? ? "organizer": "Roy"? }}'
默認的腳本語言是painless。
由于更新要獲得現(xiàn)有文檔的_source內(nèi)容,修改并重新索引新的文檔,因此腳本會修改_source中的字段。使用ctx._source來引用_source,使用ctx._source[字段名]來引用某個指定的字段。
如果需要變量,推薦在params下作為參數(shù)單獨定義,和腳本本身分開。這是因為腳本需要編譯,一旦編譯完成,就會被緩存。如果使用不同的參數(shù),多次運行同樣的腳本,腳本只需要編譯一次。之后的運行都會從緩存中獲取現(xiàn)有的腳本。相比每次不同的腳本,這樣運行會更快,因為不同的腳本每次都需要編譯。這個思想和Oracle的綁定變量與軟編譯概念異曲同工。
curl -XPUT '172.16.1.127:9200/online-shop/_doc/1?pretty' -H 'Content-Type: application/json' -d '{? "caption": "Learning Elasticsearch",? "price": 15}'curl -XPOST '172.16.1.127:9200/online-shop/_doc/1/_update?pretty' -H 'Content-Type: application/json' -d '{? "script": {? ? "source": "ctx._source.price += params.price_diff",? ? "params": {? ? ? "price_diff": 10? ? }? }}'curl -XGET '172.16.1.127:9200/online-shop/_doc/1?pretty'
{? "_index" : "online-shop",? "_type" : "_doc",? "_id" : "1",? "_version" : 2,? "found" : true,? "_source" : {? ? "caption" : "Learning Elasticsearch",? ? "price" : 25? }}
2. 通過版本實現(xiàn)并發(fā)控制

/***curl -XPOST 'localhost:9200/online-shop/shirts/1/_update' -d '{"script": "Thread.sleep(10000); ctx._source.price = 2"}' &% curl -XPOST 'localhost:9200/online-shop/shirts/1/_update' -d '{"script": "ctx._source.caption = \"Knowing Elasticsearch\""}'***/
curl -XGET "172.16.1.127:9200/online-shop/_doc/1?version=2&pretty"curl -XPOST '172.16.1.127:9200/online-shop/_doc/1/_update?pretty' -H 'Content-Type: application/json' -d '{? "script": "ctx._source.caption = \"Knowing Elasticsearch\""}'curl -XGET "172.16.1.127:9200/online-shop/_doc/1?version=2&pretty"
{? "error" : {? ? "root_cause" : [? ? ? {? ? ? ? "type" : "version_conflict_engine_exception",? ? ? ? "reason" : "[_doc][1]: version conflict, current version [3] is different than the one provided [2]",? ? ? ? "index_uuid" : "b6z8mwmRQ1ambP9g5rv9vQ",? ? ? ? "shard" : "3",? ? ? ? "index" : "online-shop"? ? ? }? ? ],? ? "type" : "version_conflict_engine_exception",? ? "reason" : "[_doc][1]: version conflict, current version [3] is different than the one provided [2]",? ? "index_uuid" : "b6z8mwmRQ1ambP9g5rv9vQ",? ? "shard" : "3",? ? "index" : "online-shop"? },? "status" : 409}

curl -XPOST '172.16.1.127:9200/online-shop/_doc/1/_update?retry_on_conflict=3&pretty' -H 'Content-Type: application/json' -d '{? "script": "ctx._source.price = 2"}'
curl -XPUT "172.16.1.127:9200/online-shop/_doc/1?version=6&pretty" -H 'Content-Type: application/json' -d '{? "caption": "I Know about Elasticsearch Versioning",? "price": 5}'
三、刪除數(shù)據(jù)
1. 刪除文檔
curl -XDELETE '172.16.1.127:9200/online-shop/_doc/1?pretty'curl -X POST "172.16.1.127:9200/my_index/_delete_by_query?pretty" -H 'Content-Type: application/json' -d'{? "query": {?? ? "query_string": {? ? ? "query": "elasticsearch"? ? }? }}'
2. 刪除索引
# 刪除一個索引curl -XDELETE "172.16.1.127:9200/blog?&pretty"# 刪除多個索引curl -XDELETE "172.16.1.127:9200/my_index,manual_id?&pretty"
3. 關(guān)閉索引
# 關(guān)閉索引curl -XPOST '172.16.1.127:9200/logs/_close?pretty'# 打開索引curl -XPOST '172.16.1.127:9200/logs/_open?pretty'

版權(quán)聲明:
文章不錯?點個【在看】吧!??
評論
圖片
表情




