面向文檔html
在應用程序中對象不多隻是一個簡單的鍵和值的列表。一般,它們擁有更復雜的數據結構,可能包括日期、地理信息、其餘對象或者數組等。node
也許有一天你想把這些對象存儲在數據庫中。使用關係型數據庫的行和列存儲,這至關因而把一個表現力豐富的對象擠壓到一個很是大的電子表格中:你必須將這個對象扁平化來適應表結構--一般一個字段>對應一列--並且又不得不在每次查詢時從新構造對象。git
Elasticsearch 是 面向文檔 的,意味着它存儲整個對象或 文檔_。Elasticsearch 不只存儲文檔,並且 _索引 每一個文檔的內容使之能夠被檢索。在 Elasticsearch 中,你 對文檔進行索引、檢索、排序和過濾--而不是對行列數據。這是一種徹底不一樣的思考數據的方式,也是 Elasticsearch 能支持複雜全文檢索的緣由。github
JSON編輯web
Elasticsearch 使用 JavaScript Object Notation 或者 JSON 做爲文檔的序列化格式。JSON 序列化被大多數編程語言所支持,而且已經成爲 NoSQL 領域的標準格式。 它簡單、簡潔、易於閱讀。
正則表達式
請參考 https://www.elastic.co/guide/cn/elasticsearch/guide/current/_indexing_employee_documents.html 中的操做 將 HTTP 命令由 PUT 改成 GET 能夠用來檢索文檔,一樣的,可使用 DELETE 命令來刪除文檔,以及使用 HEAD 指令來檢查文檔是否存在。若是想更新已存在的文檔,只需再次 PUT
GET _search { "query": { "match_all": {} } } GET /_count?pretty { "query": { "match_all": {} } } #添加數據 PUT /megacorp/employee/1 { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "musicxx" ] } GET /megacorp/_doc/1 GET /megacorp/_search PUT /megacorp/_doc/1 { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing and swimming", "interests": [ "sports", "music" ], "homw":"shanghai" } PUT /megacorp/employee/1 { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing and swimming", "interests": [ "sports", "music" ], "homw":"shanghai" } PUT /megacorp/employee/2 { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests": [ "music" ] } PUT /megacorp/employee/3 { "first_name" : "Douglas", "last_name" : "Fir", "age" : 35, "about": "I like to build cabinets", "interests": [ "forestry" ] } PUT /megacorp/employee/4 { "first_name" : "wendeng", "last_name" : "xing", "age" : 18, "about": "I like to play basketball", "interests": [ "musics" ], "hometomn":"anhui province" } # 局部更新 POST 方式 + _update 標示 POST /megacorp/_doc/4/_update { "doc": { "tags":["testing"], "views":0, "title":"ok" } } GET /megacorp/_doc/4 # 腳本更新 POST /megacorp/_doc/4/_update { "script" : "ctx._source.tags+=new_tag", "params" : { "new_tag" : "search" } } # upsert 初始化值 POST /megacorp/_doc/4/_update { "script" : "ctx._source.views+=1", "upsert": { "views": 1 } } # retry_on_conflict 遇到衝突重試 POST /megacorp/_doc/100/_update?retry_on_conflict=3 { "script" : "ctx._source.views+=1", "upsert": { "views": 0 } } #mget 批量多文檔檢索 POST /_mget { "docs" : [ { "_index" : "website", "_type" : "blog", "_id" : 2 }, { "_index" : "website", "_type" : "pageviews", "_id" : 1, "_source": "views" } ] } POST /megacorp/_doc/_mget { "ids":[123] } POST /megacorp/_doc/_mget { "ids":[1,2,3,212] } POST /_bulk { "delete": { "_index": "website", "_type": "blog", "_id": "123" }} { "create": { "_index": "website", "_type": "blog", "_id": "123" }} { "title": "My first blog post" } { "index": { "_index": "website", "_type": "blog" }} { "title": "My second blog post" } { "update": { "_index": "website", "_type": "blog", "_id": "123", "retry_on_conflict" : 3} } { "doc" : {"title" : "My updated blog post"} } GET /_search #分頁查詢. from 表示下標 ,size 表示數量 GET /_search?size=2 GET /_search?size=2&from=2 GET /_search?size=2&from=4 #獲取全部數據 _search GET /megacorp/employee/_search #刪除 ID=2的數據 DELETE /megacorp/employee/200 #判斷ID=2的數據是否存在 HEAD /megacorp/employee/2 #過濾查詢_search?q=XX, last_name 條件 GET /megacorp/employee/_search?q=last_name:Smith #過濾查詢_search?q=XX, interests 條件 GET /megacorp/employee/_search?q=interests:music #過濾查詢包裝形式 GET /megacorp/employee/_search { "query":{ "match": { "interests": "music" } } } #過濾查詢 #單個匹配 match GET /megacorp/employee/_search { "query": { "match": { "interests": "music" } } } #短語匹配 match_phrase GET /megacorp/employee/_search { "query": { "match_phrase": { "about": "rock albums" } }, "highlight": { "fields" : { "about" : {} } } } #過濾查詢帶有範圍的 GET /megacorp/employee/_search { "query" : { "bool": { "must": { "match" : { "last_name" : "smith" } }, "filter": { "range" : { "age" : { "gte": 10, "lte": 30 } } } } } } #全文搜索,查詢結果是相關性的 GET /megacorp/employee/_search { "query" : { "match" : { "about" : "rock albums" } } } #短語搜索,經過 match_phrase GET /megacorp/employee/_search { "query": { "match_phrase": { "about": "rock albums" } } } #查詢結果高亮顯示 highlight,排序 order GET megacorp/employee/_search { "query": { "match_phrase": { "about": "rock" } }, "highlight": { "fields": { "about":{ "fragment_size": 20, "number_of_fragments": 5 } }, "order": "score" } } #映射(mapping)機制用於進行字段類型確認,將每一個字段匹配爲一種肯定的數據類型(string, number, booleans, date等)。 #分析(analysis)機制用於進行全文文本(Full Text)的分詞,以創建供搜索用的反向索引。 #分析 GET /megacorp/employee/_search { "aggs" : { "avg_age" : { "avg" : { "field" : "age" } } } } GET /megacorp/employee/_search { "aggs": { "terms": { "field": "interests" } } } POST /tt/gb/ { "tweet": "Elasticsearch is very flexible", "user": { "id": "@johnsmith", "gender": "male", "age": 26, "name": { "full": "John Smith", "first": "John", "last": "Smith" } } } GET /tt/gb/_search { "from": 0, "size": 10 } GET /index_2014*/type1,type2/_search {} #集羣健康 GET /_cluster/health GET /_cluster/state PUT /blogs { "settings": { "number_of_shards": 13, "number_of_replicas" : 2 } } PUT /blogs/_settings { "number_of_replicas" : 2 } PUT /xing { "settings": { "number_of_shards": 3, "number_of_replicas":2 } } GET /xing POST /megacorp/schools PUT /movies/movie/1 { "title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972, "genres": ["Crime", "Drama"] } PUT /movies/_doc/1 { "title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972, "genres": ["Crime", "Drama2"] } PUT /movies/_doc/10 { "title": "The Godfather10", "director": "Francis Ford Coppola", "year": 1990, "genres": ["Crime10", "Drama2"] } GET movies/_search GET /movies/movie/_search { "query": { "match": { "title": "lover" } } } PUT /movies/movie/2 { "title":"my lover", "desc":"This is very good", "time":"1999" } GET /movies/movie/2 GET /movies/movie/_search { "query": { "query_string": { "default_field": "desc", "query": "1999" } } } PUT company/_doc/1 { "title":"lieni", "address":"shanghaixuhui", "number":101 } PUT company/_doc/2 { "title":"dipont", "address":"shanghaihongkou", "number":200, "tele":"123456" } PUT company/_doc/6 { "title":"qingke", "address":"shanghaixuhui", "number":300, "tele":"12321", "people":1000 } POST company/_doc/ { "title":"qiniu", "address":"shanghaixuhui", "number":800, "tele":"123521", "people":6000 } GET company/_doc/1?_source=title,address GET company/_doc/1?_source GET company/_doc/1/_source GET company/_search # 判斷是否存在 HEAD HEAD company/_doc/1 PUT /schools GET /schools DELETE /schools PUT /schools/_bulk { "index":{ "_index":"schools", "_type":"school", "_id":"1" } } PUT /schools/_bulk { "index":{ "_index":"schools", "_type":"school", "_id":"1" } } POST /xing,wen,deng/_search POST /_all/_search POST /index1,index2,index3/_search GET _cat/nodes?v GET _cat/shards POST users/_doc { "user":"Mike", "post_data":"2019-07-01", "message":"trying out Kibana" } PUT users/_doc/crhBrWsBPyIZBrqJA4UR?op_type=create { "user":"Tom", "post_data":"2019-07-02", "message":"trying out Kibana2" } #查詢 GET users/_doc/crhBrWsBPyIZBrqJA4UR #更新並覆蓋 PUT users/_doc/crhBrWsBPyIZBrqJA4UR { "user":"jack" } #批量操做 POST _bulk {"index":{"_index":"test","_id":"1"}} { "field1":"value1"} {"delete":{"_index":"test","_id":"2"}} {"create":{"_index":"test2","_id":"3"}} {"field":"value3"} {"update":{"_id":"1","_index":"test"}} {"doc":{"field2":"value2"}} GET _mget { "docs":[ {"_index":"test","_id":"1"}, {"_index":"test2","_id":"3"} ] } #分詞 GET _analyze { "analyzer": "standard", "text": "2 running Qucik brown-foxes leap over lazy dogs in the summer evening." } #語言分詞 GET _analyze { "analyzer": "english", "text": "2 running Qucik brown-foxes leap over lazy dogs in the summer evening." } POST _analyze { "analyzer": "standard", "text":"她說的確實在理" } POST _analyze { "analyzer": "english", "text":"她說的確實在理" } POST _analyze { "analyzer": "icu_analyzer", "text":"她說的確實在理" } POST _analyze { "analyzer": "ik_smart", "text":"她說的確實在理" } POST _analyze { "analyzer": "ik_max_word", "text":"她說的確實在理" } #添加數據 POST customer/_doc/1?pretty { "city": "北京", "useragent": "Mobile Safari", "sys_version": "Linux armv8l", "province": "北京", "event_id": "", "log_time": 1559191912, "session": "343730" } GET customer/_doc/1?pretty POST customer/_doc/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512 GET /_count?pretty { "query": { "match_all": {} } } #結構化查詢 GET /_search { "query": { "match_all": {} } } #過濾查詢 GET /megacorp/employee/_search { "post_filter": { "range":{ "age":{ "gte":20, "lt":30 } } } } #排序 GET /megacorp/employee/_search { "post_filter": { "exists": { "field": "age" } }, "sort": [ { "age": { "order": "asc" } }, { "_id": { "order": "asc" } } ] } GET megacorp/employee/_search?sort=date:desc&sort=_score&q=search GET posts/product/1 GET demo-2019.07.19/demo/5 GET cc_problem/_doc/1 GET cc/_mapping GET cc/_settings DELETE cc #全部 GET cc/_search { "query": { "match_all": {} } } GET cc/_search { "query": { "match": { "knowledgePoint": "5" } } } #排序 GET cc/_search { "query": { "match": { "code": "AMC8" } }, "sort": [ { "createTime": { "order": "asc" } } ] } #分頁 GET cc/_search { "query": { "match_all": {} }, "sort": [ { "createTime": { "order": "desc" } } ], "from": 0, "size": 2 } GET cc/_search { "sort": { "_script": { "script": "Math.random()", "type": "number", "order": "asc" } } } #指定結果字段 GET cc/_search { "query": { "match_all": {} }, "_source": ["id","code","knowledgePoint"] } #過濾查詢 GET cc/_search { "query": { "bool": { "must": [ {"match": { "code": "amc8" }} ], "filter": { "range": { "questionLevelId": { "gte": 2, "lte": 10 } } } } } } GET cc/_search { "query": { "match": { "FIELD": "AMC8" } } } GET cc/_search { "query": { "match_phrase": { "code": "1998" } } } GET cc/_search { "query": { "term": { "code.keyword": "NCSL3-2016-1" } } } GET cc/_search { "query": { "term": { "code.keyword": { "value": "AMC8" } } } } DELETE cc # html 剝離 POST _analyze { "tokenizer": "keyword", "char_filter": ["html_strip"], "text": "<b>hello world</b>" } POST _analyze { "tokenizer": "standard", "char_filter": [ { "type":"mapping", "mappings":["- => _"] } ], "text": "123-234,I-test ! test-990 650-12" } POST _analyze { "tokenizer": "standard", "char_filter": [ { "type":"mapping", "mappings":[":) => happy"] } ], "text": "I fell :)" } # 正則表達式 POST _analyze { "tokenizer": "standard", "char_filter": [ { "type":"pattern_replace", "pattern":"http://(.*)", "replacement":"$1" } ], "text": "http://www.baidu.co" } #路徑 POST _analyze { "tokenizer": "path_hierarchy", "text": "/user/soft/es/bin/es" } GET _analyze { "tokenizer": "whitespace", "filter": ["stop"], "text": ["The rain in Spain fail"] } GET cc/_mapping GET cc GET cc/_search { "query": { "match_all": {} } } DELETE cc GET cc PUT cc { "mappings" : { "cc_question" : { "properties" : { "answer" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "category" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "code" : { "type" : "completion", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "content" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "createTime" : { "type" : "long" }, "deleted" : { "type" : "boolean" }, "id" : { "type" : "long" }, "knowledgePoint" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "properties" : { "type" : "nested", "properties" : { "examinationPaperCode" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "questionNumber" : { "type" : "long" } } }, "questionLevelId" : { "type" : "long" }, "questionSourceId" : { "type" : "long" }, "subjectId" : { "type" : "long" }, "type" : { "type" : "long" }, "updateTime" : { "type" : "long" } } } } } DELETE products PUT products { "settings": { "number_of_shards": 1 } } POST products/_doc/_bulk { "index": { "_id": 1 }} { "productID" : "XHDK-A-1293-#fJ3","desc":"iPhone" } { "index": { "_id": 2 }} { "productID" : "KDKE-B-9947-#kL5","desc":"iPad" } { "index": { "_id": 3 }} { "productID" : "JODL-X-1937-#pV7","desc":"MBP" } GET products GET products/_search { "query": { "match_all": {} } } GET products/_search { "query": { "term": { "productID.keyword": { "value": "XHDK-A-1293-#fJ3" } } } } # constant_score 跳過算分 GET products/_search { "query": { "constant_score": { "filter": {"term": { "productID.keyword": "XHDK-A-1293-#fJ3" }} } } } # 返回結果過濾 GET cc/_search { "query": { "match": { "content": { "query": "'desc':'B'", "operator": "and" } } }, "_source": { "excludes": ["id","subjectId","content"], "includes": ["knowledgePoint"] } } DELETE cc GET _search { "query": { "match_all": {} } } GET cc GET cc/cc_question/mapping GET cc/_search { "query": { "match_all": {} } } GET cc/_search { "query": { "bool": { "must": [ {"match": { "knowledgePoint": "1" }},{ "match": { "questionLevelId": "1" } },{ "match": { "questionSourceId": "1" } } ] } }, "from": 0, "size": 20 } GET cc/_search { "query": { "match": { "properties.questionNumber": 6 } } } GET cc/_search { "query": { "match": { "knowledgePoint": "2" } }, "sort": { "_script": { "script": "Math.random()", "type": "number", "order": "asc" } }, "from": 0, "size": 2 } GET cc/_search { "query": { "match": { "questionSourceId": "3" } } } GET cc/_search { "query": { "match": { "id": "256" } } } GET cc/_search { "query": { "match": { "subjectId": "1" } } } GET cc/_search { "query": { "bool": { "filter": { "term": { "knowledgePoint": "1" } } } } } # must 必須知足(影響算分的),must_not 必須不知足,should 必須知足其中一條,filter 必須知足(不影響算分) GET cc/_search { "size": 3, "query": { "bool": { "must": [ {"term": { "subjectId": { "value": "1" } }}, {"term": { "type": { "value": "1" } }} ], "must_not": [ {"term": { "questionSourceId": { "value": "23" } }} ], "should": [ {"term": { "category": { "value": "2" } }}, { "term": { "category": { "value": "1" } } } ] } } } GET cc/_search { "query": { "match": { "questionSourceId": "3" } }, "aggs": { "all": { "terms": { "field": "questionSourceId" } } } } GET cc/_search { "query": { "match": { "subjectId": "1" } }, "aggs": { "all": { "terms": { "field": "questionLevelId" } } } } GET cc/_search { "aggs": { "all_interests": { "terms": { "field": "questionLevelId" }, "aggs": { "NAME": { "terms": {"field":"questionSourceId"} } } } } } GET cc/_search { "aggs": { "questionSourceIds": { "terms": { "field": "questionSourceId" }, "aggs": { "questionLevelIds": { "terms": {"field":"questionLevelId"} } } } } , "from": 0, "size": 0 } GET cc/_search { "aggs": { "questionSourceIds": { "terms": { "field": "questionSourceId" }, "aggs": { "questionLevelIds": { "terms": {"field":"questionLevelId"} } } } } , "from": 0, "size": 0 } GET cc/_search { "size": 0, "aggs": { "NAME": { "terms": { "field": "questionSourceId", "size": 10 }, "aggs": { "ava": { "avg": { "field": "questionLevelId" } } } } } } #ik_max_word #ik_smart #hanlp: hanlp默認分詞 #hanlp_standard: 標準分詞 #hanlp_index: 索引分詞 #hanlp_nlp: NLP分詞 #hanlp_n_short: N-最短路分詞 #hanlp_dijkstra: 最短路分詞 #hanlp_crf: CRF分詞(在hanlp 1.6.6已開始廢棄) #hanlp_speed: 極速詞典分詞 POST _analyze { "analyzer": "hanlp_standard", "text": ["劍橋分析公司多位高管對臥底記者說,他們確保了唐納德·特朗普在總統大選中獲勝"] } GET _cluster/health GET _cluster/health?level=indices DELETE cc GET _cluster/state/unassigned_shards GET /_cat/shards/cc POST cc/_flush/synced GET cc PUT cc/_settings { "number_of_replicas": 1 } #用於自動補全的字段 PUT music { "mappings": { "_doc" : { "properties" : { "suggest" : { "type" : "completion" }, "title" : { "type": "keyword" } } } } } PUT music/_doc/1?refresh { "suggest" : { "input": [ "Nevermind", "Nirvana" ], "weight" : 34 } } PUT music/_doc/1?refresh { "suggest" : [ { "input": "Nevermind", "weight" : 10 }, { "input": "Nirvana", "weight" : 3 } ]} PUT music/_doc/2?refresh { "suggest" : { "input": [ "Nevermind", "Nirvana" ], "weight" : 20 } } POST music/_search?pretty { "suggest": { "song-suggest" : { "prefix" : "nir", "completion" : { "field" : "suggest" } } } } POST music/_search?pretty { "suggest": { "song-suggest" : { "prefix" : "nir", "completion" : { "field" : "suggest", "skip_duplicates": true } } }} # 短語 PUT music/_doc/3?refresh { "suggest" : { "input": [ "lucene solr", "lucene so cool","lucene elasticsearch" ], "weight" : 20 } } PUT music/_doc/4?refresh { "suggest" : { "input": ["lucene solr cool","lucene elasticsearch" ], "weight" : 10 } } GET music POST music/_search?pretty { "suggest": { "song-suggest" : { "prefix" : "lucene s", "completion" : { "field" : "suggest" , "skip_duplicates": true } } } } POST cc/_search?pretty { "suggest": { "song-suggest" : { "prefix" : "lucene s", "completion" : { "field" : "content" , "skip_duplicates": true } } } } GET cc/_search { "suggest": { "YOUR_SUGGESTION": { "text": "AMC", "completion": { "field": "codes", "size": 20 } } }, "_source": "" } GET cc/_search { "query": { "query_string": { "query": "2" } }, "_source": ["id","code","knowledgePoint"] } DELETE cc GET cc/_search { "query": { "match_all": {} } }
整合以下: