如下分爲 索引文檔(insert) 和 查詢文檔(select)segmentfault
1 一個index只有一個typeapp
索引文檔時,使用 _doc來代替typeui
PUT /megacorp/_doc/3 { "first_name" : "Douglas", "last_name" : "Fir", "age" : 35, "about": "I like to build cabinets", "interests": [ "forestry" ] }
查詢某一條文檔this
GET /megacorp/_doc/3
查詢姓smith的spa
GET /megacorp/_search?q=last_name:Smith
2 查詢姓smith的,並大於30歲的 DSL 1使用 a and b 2查詢a,過濾b.net
POST /megacorp/_search { "query": { "bool": { "must": [ { "match": { "last_name": "Smith" } }, { "range": { "age": { "gt": 30 } } } ] } } }
POST /megacorp/_search { "query": { "bool": { "must": [ { "match": { "last_name": "Smith" } } ], "filter": { "range": { "age": { "gt": 30 } } } } } }
3短語搜索, 包含關鍵字的所有分詞rest
https://blog.csdn.net/sinat_29581293/article/details/81486761code
GET /megacorp/_search { "query" : { "match_phrase": { "about" : "rock climbing" } } }
4查看關鍵字分詞 standard標準分詞漢字分爲每一個字,英文分爲每一個單詞 ,ik分詞 有 ik_smart 和ik_max_wordblog
GET /megacorp/_analyze { "text": ["康師傅","rock climbing"], "analyzer": "standard" }
{ "tokens" : [ { "token" : "康", "start_offset" : 0, "end_offset" : 1, "type" : "<IDEOGRAPHIC>", "position" : 0 }, { "token" : "師", "start_offset" : 1, "end_offset" : 2, "type" : "<IDEOGRAPHIC>", "position" : 1 }, { "token" : "傅", "start_offset" : 2, "end_offset" : 3, "type" : "<IDEOGRAPHIC>", "position" : 2 }, { "token" : "rock", "start_offset" : 4, "end_offset" : 8, "type" : "<ALPHANUM>", "position" : 103 }, { "token" : "climbing", "start_offset" : 9, "end_offset" : 17, "type" : "<ALPHANUM>", "position" : 104 } ] }
5查看某個字段在索引文檔時分詞結果索引
GET /test/_analyze { "field": "t_name", "text": ["康師傅","rock climbing"], }
6 查看文檔字段 ,t_name字段在索引文檔時使用ik_max_word分詞,查詢文檔時使用ik_smart分詞
https://segmentfault.com/a/1190000012553894?utm_source=tag-newest
http://localhost:9200/test/_mapping
t_name: { type: "text", similarity: "BM25", fields: { keyword: { type: "keyword", ignore_above: 256 } }, analyzer: "ik_max_word", search_analyzer: "ik_smart" }, t_pyname: { type: "text", fields: { keyword: { type: "keyword", ignore_above: 256 } } },
7高亮關鍵字
GET /megacorp/_search { "query" : { "match_phrase": { "about" : "rock climbing" } }, "highlight": { "fields": { "about": {} } } }
8es的group_by,聚合 aggregations,進行分析統計
GET /megacorp/_search { "aggs": { "all_inter": { "terms": { "field": "interests.keyword" } } } }
9 聚合時報錯,具體緣由是聚合須要大量的內存,聚合前,須要將相應的字段開啓聚合,或者按上面的方式 使用 .keyword
Fielddata is disabled on text fields by default. Set fielddata=true on [interests] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead
PUT megacorp/_mapping { "properties": { "interests": { "type": "text", "fielddata": true } } }
10聚合時間長,聚合慢, 使用"execution_hint": "map"
https://blog.csdn.net/laoyang360/article/details/79253294
GET /megacorp/_search
{ "query": { "match": { "last_name": "smith" } }, "aggs": { "all_inter": { "terms": { "field": "interests",
"execution_hint": "map"
} } } }
11查詢文檔,一個字段多個關鍵字(同一個字段查詢多個搜索詞) interests字段包含music的或者包含sports的,or
GET /megacorp/_search { "query": { "terms": { "interests": [ "music", "sports" ] } } }
12查詢文檔,同一個字段包含多個關鍵字 interests字段包含music的和包含sports的,and
GET /megacorp/_search { "query": { "bool": { "must": [ { "term": { "interests": { "value": "music" } } } , { "term": { "interests": { "value": "sports" } } } ] } } }
12查詢文檔,一個關鍵字多個字段(同一個搜索詞查詢多個字段)
http://www.javashuo.com/article/p-hzrevgrk-gc.html
GET /megacorp/_search { "query": { "multi_match": { "query": "Smith", "fields": ["last_name","first_name"] } } }
13聚合分級彙總,聚合後的每一組數據進行統計,aggs後再aggs
GET /megacorp/_search { "size":0, "aggs": { "all_inter": { "terms": { "field": "interests", "execution_hint": "map" }, "aggs": { "avg_age": { "avg": { "field": "age" } } } } } }
14 多字段查詢, 如一個關鍵字查詢同音字,同義字,形近字,等
https://blog.csdn.net/questiontoomuch/article/details/48493991
同音字能夠增長一個字段,如 t_pyname 是t_name的pinyin
同義字增長一個字段, t_shinglesname
- 使用一個詞幹提取器來將jumps,jumping和jumped索引成它們的詞根:jump。而後當用戶搜索的是jumped時,咱們仍然可以匹配含有jumping的文檔。
- 包含同義詞,好比jump,leap和hop。
- 移除變音符號或者聲調符號:好比,ésta,está和esta都會以esta被索引。