PUT /{index} { "settings": {}, "mappings": { "properties": { } } }
建立索引示例:html
PUT /my_index { "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "my_type":{ "properties": { "my_field":{ "type": "text" } } } } }
PUT /{index}/_settings { "setttings": {} } PUT /my_index/_settings { "settings": { "number_of_replicas": 1 } }
DELETE /{index}
示例java
DELETE /my_index DELETE /index_one,index_two DELETE /index_* DELETE /_all
刪除索引API也能夠經過使用逗號分隔列表應用於多個索引,或者經過使用_all或*做爲索引應用於全部索引(當心!)。
要禁用容許經過通配符刪除索引,或者將 elasticsearch.yml 配置中的_all設置action.destructive_requires_name設置爲true。也能夠經過羣集更新設置api更改此設置。正則表達式
Elasticsearch附帶了各類內置分析器,無需進一步配置便可在任何索引中使用:算法
standard analyzer:
所述standard分析器將文本分爲在字邊界條件,由Unicode的文本分割算法所定義的。它刪除了大多數標點符號,小寫術語,並支持刪除停用詞。
Simple analyzer:
該simple分析儀將文本分爲方面每當遇到一個字符是否是字母。而後所有變爲小寫
whitespace analyzer:
whitespace只要遇到任何空格字符 ,分析器就會將文本劃分爲術語。它不會進行小寫轉換。
stop analyzer:
該stop分析器是像simple,並且還支持去除中止詞。
keyword analyzer:
所述keyword分析器是一個「空操做」分析器接受任何文本它被賦予並輸出徹底相同的文本做爲一個單一的術語,也就是不會分詞,進行精確匹配。
pattern analyzer:
所述pattern分析器使用一個正則表達式對文本進行拆分。它支持小寫轉換和停用字。
language analyzer:
Elasticsearch提供了許多特定於語言的分析器,如english或 french。
fingerprint analyzer:
所述fingerprint分析器是一種專業的指紋分析器,它能夠建立一個指紋,用於重複檢測。
put /my_index { "settings": { "analysis": { "analyzer": { "es_std":{ "type":"standard", "stopwords":"_english_" } } } } }
# standard分詞 GET /my_index/_analyze { "analyzer": "standard", "text": "a dog is in the house" }
{ "tokens": [ { "token": "a", "start_offset": 0, "end_offset": 1, "type": "<ALPHANUM>", "position": 0 }, { "token": "dog", "start_offset": 2, "end_offset": 5, "type": "<ALPHANUM>", "position": 1 }, { "token": "is", "start_offset": 6, "end_offset": 8, "type": "<ALPHANUM>", "position": 2 }, { "token": "in", "start_offset": 9, "end_offset": 11, "type": "<ALPHANUM>", "position": 3 }, { "token": "the", "start_offset": 12, "end_offset": 15, "type": "<ALPHANUM>", "position": 4 }, { "token": "house", "start_offset": 16, "end_offset": 21, "type": "<ALPHANUM>", "position": 5 } ] }
使用原來的es_sted中的english分詞數據庫
# english分詞 GET /my_index/_analyze { "analyzer": "es_std", "text": "a dog is in the house" }
{ "tokens": [ { "token": "dog", "start_offset": 2, "end_offset": 5, "type": "<ALPHANUM>", "position": 1 }, { "token": "house", "start_offset": 16, "end_offset": 21, "type": "<ALPHANUM>", "position": 5 } ] }
PUT /my_index { "settings": { "analysis": { "char_filter": { "&_to_and":{ "type":"mapping", "mappings":["&=>and"] } }, "filter": { "my_stopwords":{ "type":"stop", "stopwords":["the","a"] } }, "analyzer": { "my_analyzer":{ "type": "custom", "char_filter":["html_strip","&_to_and"], "tokenizer":"standard", "filter":["lowercase", "my_stopwords"] } } } } }
測試分詞json
GET /my_index/_analyze { "text": "tom&jerry are a friend in the house, <a>, HAHA!!", "analyzer": "my_analyzer" }
{ "tokens": [ { "token": "tomandjerry", "start_offset": 0, "end_offset": 9, "type": "<ALPHANUM>", "position": 0 }, { "token": "are", "start_offset": 10, "end_offset": 13, "type": "<ALPHANUM>", "position": 1 }, { "token": "friend", "start_offset": 16, "end_offset": 22, "type": "<ALPHANUM>", "position": 3 }, { "token": "in", "start_offset": 23, "end_offset": 25, "type": "<ALPHANUM>", "position": 4 }, { "token": "house", "start_offset": 30, "end_offset": 35, "type": "<ALPHANUM>", "position": 6 }, { "token": "haha", "start_offset": 42, "end_offset": 46, "type": "<ALPHANUM>", "position": 7 } ] }
設置使用分詞自定義api
PUT /my_index/_mapping/my_type { "properties": { "content": { "type": "text", "analyzer": "my_analyzer" } } }
PUT /my_index { "mappings": { "my_type": { "properties": {} } } }
properties多線程
PUT /my_index/_mapping/my_type { "properties": { "title": { "type": "text" } } }
好處併發
(1)查詢的時候,直接能夠拿到完整的document,不須要先拿document id,再發送一次請求拿document
(2)partial update基於_source實現
(3)reindex時,直接基於_source實現,不須要從數據庫(或者其餘外部存儲)查詢數據再修改
(4)能夠基於_source定製返回field
(5)debug query更容易,由於能夠直接看到_sourceapp
若是不須要上述好處,能夠禁用_source
PUT /my_index/_mapping/my_type2 { "_source": {"enabled": false} }
PUT /my_index/_mapping/my_type3 { "_all": {"enabled": false} }
也能夠在field級別設置include_in_all field,設置是否要將field的值包含在_all field中
PUT /my_index/_mapping/my_type4 { "properties": { "my_field": { "type": "text", "include_in_all": false } } }
舉例:
PUT my_index { "mappings": { "my_type":{ "dynamic": "strict", "properties": { "title":{ "type": "text" }, "address":{ "type": "object", "dynamic":"true" } } } } }
PUT /my_index/my_type/1 { "title": "my article", "content": "this is my article", "address": { "province": "guangdong", "city": "guangzhou" } } { "error": { "root_cause": [ { "type": "strict_dynamic_mapping_exception", "reason": "mapping set to strict, dynamic introduction of [content] within [my_type] is not allowed" } ], "type": "strict_dynamic_mapping_exception", "reason": "mapping set to strict, dynamic introduction of [content] within [my_type] is not allowed" }, "status": 400 }
PUT /my_index/my_type/1 { "title": "my article", "address": { "province": "guangdong", "city": "guangzhou" } } GET /my_index/_mapping/my_type { "my_index": { "mappings": { "my_type": { "dynamic": "strict", "properties": { "address": { "dynamic": "true", "properties": { "city": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "province": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "title": { "type": "text" } } } } } }
PUT /my_index/_mapping/my_type { "date_detection": false }
PUT my_index { "mappings": { "my_type":{ "dynamic_templates": [ { "en":{ "match":"*_en", "match_mapping_type": "string", "mapping": { "type":"string", "analyzer":"english" } } } ] } } }
初始化數據
PUT /my_index/my_type/1 { "title": "this is my first article" } PUT /my_index/my_type/2 { "title_en": "this is my first article" }
無模板匹配
GET /my_index/my_type/_search { "query": { "match": { "title":"is" } } }
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2824934, "hits": [ { "_index": "my_index", "_type": "my_type", "_id": "1", "_score": 0.2824934, "_source": { "title": "this is my first article" } } ] } }
有模板匹配
GET /my_index/my_type/_search { "query": { "match": { "title_en":"is" } } }
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 0, "max_score": null, "hits": [] } }
此時title沒有匹配到任何的dynamic模板,默認就是standard分詞器,不會過濾停用詞,is會進入倒排索引,用is來搜索就能夠搜索到。而title_en匹配到了dynamic模板,就是english分詞器,會過濾停用詞,is這種停用詞就會被過濾掉,用is來搜索就搜索不到了。
DELETE /my_index PUT /my_index/my_type/1 { "title": "2017-01-01" } PUT /my_index/my_type/2 { "title": "2017-01-02" } PUT /my_index/my_type/3 { "title": "2017-01-03" }
GET /my_index/my_type/_search { "query": { "match_all": {} } } { "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "my_index", "_type": "my_type", "_id": "2", "_score": 1, "_source": { "title": "2017-01-02" } }, { "_index": "my_index", "_type": "my_type", "_id": "1", "_score": 1, "_source": { "title": "2017-01-01" } }, { "_index": "my_index", "_type": "my_type", "_id": "3", "_score": 1, "_source": { "title": "2017-01-03" } } ] } }
PUT /my_index/my_type/4 { "title": "my first article" }
{ "error": { "root_cause": [ { "type": "mapper_parsing_exception", "reason": "failed to parse [title]" } ], "type": "mapper_parsing_exception", "reason": "failed to parse [title]", "caused_by": { "type": "illegal_argument_exception", "reason": "Invalid format: \"my first article\"" } }, "status": 400 }
PUT /my_index/_mapping/my_type { "properties": { "title": { "type": "text" } } }
{ "error": { "root_cause": [ { "type": "illegal_argument_exception", "reason": "mapper [title] of different type, current_type [date], merged_type [text]" } ], "type": "illegal_argument_exception", "reason": "mapper [title] of different type, current_type [date], merged_type [text]" }, "status": 400 }
PUT /my_index/_alias/goods_index
PUT my_index_new { "mappings": { "my_type":{ "properties": { "title":{ "type": "text" } } } } }
GET my_index/_search?scroll=1m { "query": { "match_all": {} }, "sort": [ "_doc" ], "size": 1 }
{ "_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAARhWFjFMZHFMRnF4UVFxNHhnMk1waElfZ3cAAAAAAAEYWBYxTGRxTEZxeFFRcTR4ZzJNcGhJX2d3AAAAAAABGFoWMUxkcUxGcXhRUXE0eGcyTXBoSV9ndwAAAAAAARhXFjFMZHFMRnF4UVFxNHhnMk1waElfZ3cAAAAAAAEYWRYxTGRxTEZxeFFRcTR4ZzJNcGhJX2d3", "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 3, "max_score": null, "hits": [ { "_index": "my_index", "_type": "my_type", "_id": "2", "_score": null, "_source": { "title": "2017-01-02" }, "sort": [ 0 ] } ] } }
POST _bulk {"index":{"_index": "my_index_new", "_type": "my_type", "_id": "2"}} {"title":"2017-01-02"}
POST _aliases { "actions": [ { "remove": { "index": "my_index", "alias": "goods_index" } }, { "add": { "index": "my_index_new", "alias": "goods_index" } } ] }
GET /goods_index/my_type/_search
格式:
POST /_aliases { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } }, { "add" : { "index" : "test2", "alias" : "alias1" } } ] }