curl命令
-XGET一種請求方法
-d 標識以post形式傳入參數 ,寫在請求正文里面
?pretty=true 以格式的形式顯示結果
curl -XGET http://localhost:9200/_cluster/health?pretty --查詢elasticsearch的健康信息
curl -XGET http://localhost:9200/ --查詢實例的相關信息
curl -XGET http://localhost:9200/_cluster/nodes/ --得到集群中節點的相關信息
curl -XPOST http://localhost:9200/_cluster/nodes/_shutdown --關閉整個集群
curl -XPOST http://localhost:9200/_cluster/nodes/aaaa/_shutdown --關閉集群中指定節點
curl -XPOST http://localhost:9200/lishuai --創建名為lishuai的索引
curl -XDELETE http://localhost:9200/lishuai --刪除名為lishuai的索引

curl http://10.10.110.160:9200/benlaitest/_analyze?analyzer=standard -d 我愛你中國
postman執行請求API:
http://10.10.110.160:9200/_cat/indices?v -- Get請求 查看有多少索引
http://10.10.110.160:9200/benlaitest/_analyze?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": "京", "start_offset": 3, "end_offset": 4, "type": "<IDEOGRAPHIC>", "position": 3 }, { "token": "天", "start_offset": 4, "end_offset": 5, "type": "<IDEOGRAPHIC>", "position": 4 }, { "token": "安", "start_offset": 5, "end_offset": 6, "type": "<IDEOGRAPHIC>", "position": 5 }, { "token": "門", "start_offset": 6, "end_offset": 7, "type": "<IDEOGRAPHIC>", "position": 6 } ] }
一 DSL查詢命令 -基本查詢
- term 匹配指定的文檔單元(匹配的是分詞后的詞條,假如川普分詞后為 川和普,那么term匹配川普是無結果的,要用term匹配'川'或者'普'),1 至少匹配1個 2匹配兩個:
{"terms":{"tag":["a","b"],"mininmum":1}}
- match 根據不同的字段選擇合適的分析器,一個很智能的查詢器,可以通過指定他的參數來控制匹配行文 operator 控制關聯的查詢條件 and或者or:
{"query":{match{"title":{"query":"a b }c","operation":"and"}}}
- multi_match 與match查詢類似,不同的是它可以作用在多個字段上:
{"query":{"multi_match":{"query":"a v b","fields":["title","content"]}}}
- query_string 查詢 支持lucene的查詢語法:
{"query":{"query_string":{"query":"titlename:你好^10 +titlename:哈哈","default_field":"titlename"}}} {"query":{"query_string":{"query":"你好 中國","fields":["title","name"],"use_dis_max":true}}}
- range查詢 只針對單個字段
{"query":{"range":{"year":{"from":1700,"to":1900}}}}
bool查詢,復合查詢,可以將無限數目的查詢封裝在一起
{"query":{"bool":{"must":{"term":{"title":"中國"}},"should":{"term":{"name":"帥"}}}}}
其他查詢方式:
- boosting查詢 兩個查詢封裝一起的查詢
- constant_score 恆定分值查詢
- indices 針對多個索引進行查詢
- custom_filter_score custom_boost_factor custom_score
- ids
{ "query": { "ids": { "type": "product", "values": [ "0001-2020774", "0001-2020775" ] } } }
- prefix 前綴查詢
- 模糊查詢
- fuzzy 查詢 相似度查詢,消耗cpu
- fuzzy_like_zhis 基於模糊串
- fuzzy_like_zhis_field
- more_like_this
- mroe_like_this_field
- match_all 匹配所有文檔
- wildcard 查詢 通配符查詢
二 DSL查詢命令 - 范圍查詢和排序
1 index/product/_search 查詢6到11塊的商品

{ "query": { "range": { "price": { "gte": "6", "lte": "11" } } } }
2 查詢0-11塊之間的商品,先按照相關性得分降序排列,然后按照價格升序

{ "query": { "range": { "price": { "gte": "0", "lte": "11" } } }, "sort": [ { "_score": "desc" }, { "price": "asc" } ] }
結果:
三:DSL查詢命令 - 過濾器
1)過濾器會先執行查詢,然后再對查詢文檔進行過濾

{ "query": { "field": { "title": "中國" }, "filter": { "term": { "year": 1949 } } } }
但是先執行過濾,然后對過濾結果進行查詢。效率更高:

{ "filter": { "range": { "year": { "from": 1700, "to": 1900 } } } } { "query": { "filtered": { "field": { "title": "中國" }, "filter": { "term": { "year": 1949 } } } } }
2)exists過濾器 只過濾有指定字段的文檔 相反的 missing
3)script 腳本過濾 ,過濾得到100年以前的文檔:

{ "filter": { "script": { "script": "now - doc['year'].value>100", "params": { "now": 2016 } } } }
四:DSL查詢命令 - 聚合
AGGS
1)針對性別進行分組:

{ "size": 0, "aggs": { "agg_sex": { "terms": { "field": "gender" } } } }
結果:

{ "took": 24, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 6, "max_score": 0, "hits": [ ] }, "aggregations": { "agg_sex": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 1, "key_as_string": "true", "doc_count": 4 }, { "key": 0, "key_as_string": "false", "doc_count": 2 } ] } } }
2)得到最大價格:

{ "size": 0, "aggs": { "agg_price": { "max": { "field": "price" } } } }
結果:

{ "took": 16, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 6, "max_score": 0, "hits": [ ] }, "aggregations": { "agg_price": { "value": 20.36 } } }
另外還有: min,max,sum,avg range,postfilter解決了僅僅過濾搜索結果,但是並不影響聚合結果
五:DSL查詢命令 - 統計
faceting
1)query統計:
{"query":{"match_all":{}},"facets":{"my_query_facet":{"query":{"term":{"tags":"person"}}}}}
2)filter統計:my_filter_facet
3)terms統計:返回指定字段中使用最多的詞項
4)range統計 :ranges_facet_result 統計指定范圍的文檔數。選取不同的字段進行數據聚合計算,使用 key_field和key_value 前者指定應該對哪個字段取值檢查是否屬於指定范圍,后者指明
應該對那些字段進行聚合計算
5)statistical統計:是的我們可以對一個數值型字段計算統計,得到個數綜合平方和均值最小值最大值
{"query":{"match_all":{}},"facets":{"statistical_test":{"statistical":{"field":"price"}}}}
6)terms_stats 統計,過濾統計結果:facet_filter
六:mapping嵌套

mapping:{ "mappings": { "test": { "properties": { "productname": { "type": "string" }, "category": { "type": "nested", "properties": { "category1Sysno": { "type": "string", "index": "not_analyzed" }, "category2Sysno": { "type": "string", "index": "not_analyzed" }, "category3Sysno": { "type": "string", "index": "not_analyzed" }, "category1Souce": { "type": "int", "index": "not_analyzed" }, "category2Souce": { "type": "int", "index": "not_analyzed" }, "category3Souce": { "type": "int", "index": "not_analyzed" } } } } } } } //DSL 查詢
{ "query": { "nested": { "path": "category", "query": { "bool": { "must": [ { "term": { "category.category1Sysno": "1" } } ] } } } } } //注意:在嵌套和父子中,不能根據子文檔field進行排序;例如此例中根據category.category1Souce對搜索結果進行排序; //使用對象數組的方式可以排序,排序語句:
"sort": [ { "category.category1Souce": "asc" }
七:嵌套聚合
- 查詢嵌套文檔中onlinecategory.category2Sysno為11的且先按照onlinecategory.category3Souce升序在按照price降序排列
- 增加 nested_path 和 nested_filter 重復查詢條件的原因是:排序發生在查詢執行之后 查詢條件限定了只查詢category2Sysno為11的文檔,如果排序子句中不加入此條件,排序就是基於所有文檔的onlinecategory.category3Souce來排序的,而不僅僅是"onlinecategory.category2Sysno": 11的文檔【但是此處是不需要加的因為加不加都是正確的,只做演示,基於范圍的篩選加了才有意義】
- 可以聚合嵌套的文檔,同時聚合本身也可以嵌套很多層
聚合結果排序(聚合后,按照每個桶的_count數量排序)

{ "query": { "nested": { "path": "onlinecategory", "query": { "term": { "onlinecategory.category2Sysno": "11" } } } }, "sort": [ { "onlinecategory.category3Souce": { "order": "desc", "mode": "max", "nested_path": "onlinecategory", "nested_filter": { "term": { "onlinecategory.category2Sysno": 11 } } } }, { "price": { "order": "asc" } } ], "aggs": { "size_onlinecategory": { "nested": { "path": "onlinecategory" }, "aggs": { "size_category3Sysno": { "terms": { "field": "onlinecategory.category3Sysno", "order": { "_count": "desc" } } }, "size_category2Sysno": { "terms": { "field": "onlinecategory.category2Sysno" } } } } } }
嵌套索引創建: 定義mapping:

[ElasticsearchType(Name = "product")] public class NestProduct { [String(Store = true, Index = FieldIndexOption.NotAnalyzed)] public string Id { get; set; } [String(Store = true, Index = FieldIndexOption.Analyzed)] public string Name { get; set; } [String(Store = true, Index = FieldIndexOption.NotAnalyzed)] public string Stock { get; set; } [String(Store = true, Index = FieldIndexOption.NotAnalyzed)] public string Price { get; set; } [Nested(Name ="onlinecategory")] //Path ="onlinecategory" 也可以 public List<NestCategory> Categorys { get; set; } } [ElasticsearchType(Name = "category")] public class NestCategory { [String(Store = true, Index = FieldIndexOption.Analyzed)] public string Category1Name { get; set; } [String(Store = true, Index = FieldIndexOption.NotAnalyzed)] public string Category1Sysno { get; set; } [String(Store = true, Index = FieldIndexOption.Analyzed)] public string Category2Name { get; set; } [String(Store = true, Index = FieldIndexOption.NotAnalyzed)] public string Category2Sysno { get; set; } [String(Store = true, Index = FieldIndexOption.Analyzed)] public string Category3Name { get; set; } [String(Store = true, Index = FieldIndexOption.NotAnalyzed)] public string Category3Sysno { get; set; } }
創建索引:
var decriptior = new CreateIndexDescriptor("nestproduct").Mappings(map => map.Map<NestProduct>(m => m.AutoMap())); var response = ElasticSearchCommon.GetInstance().GetElasticClient().CreateIndex(decriptior);
嵌套的查詢,排序和聚合:

/// <summary> /// 嵌套查詢 /// </summary> /// <returns></returns> public List<NestProduct> NestedSearch() { //搜索包含車厘子的且中類為12 小類為121的商品 //嵌套查詢條件 List<QueryContainer> qc = new List<QueryContainer>(); qc.Add(new TermQuery { Field = "onlinecategory.category2Sysno", Value = "11" }); //qc.Add(new TermQuery { Field = "onlinecategory.category3Sysno", Value = "121" }); NestedQuery nestq = new NestedQuery { Path= "onlinecategory", Query=new BoolQuery() { Must= qc} }; //非嵌套查詢條件 MatchPhraseQuery matchPhraseQuery = new MatchPhraseQuery { Field = "name", Query = "車厘子" }; //組合上面的查詢條件 List<QueryContainer> qcall = new List<QueryContainer>(); qcall.Add(nestq); qcall.Add(matchPhraseQuery); BoolQuery bq = new BoolQuery() { Must = qcall }; //使用嵌套字段排序 //1 默認情況下,根文檔的分數是這些嵌套文檔分數的平均值。可以通過設置 score_mode 參數來控制這個得分策略, //相關策略有 avg (平均值), max (最大值), sum (加和) 和 none (直接返回 1.0 常數值分數)。 //2 考慮將查詢條件作為 NestedFilter 子句 https://www.elastic.co/guide/cn/elasticsearch/guide/current/nested-sorting.html List<ISort> sortlist = new List<ISort>() ; sortlist.Add(new SortField() { Field = "onlinecategory.category3Souce", NestedPath= "onlinecategory",Mode= SortMode.Min, NestedFilter= bq, Order = SortOrder.Ascending }); sortlist.Add(new SortField() { Field = "price", Order = SortOrder.Ascending }); //聚合 //Dictionary<string, AggregationContainer> container = new Dictionary<string, AggregationContainer>(); //container.Add //聚合嵌套 NestedAggregation nestedAgg = new NestedAggregation("agg_onlinecategory") { Path = "onlinecategory", Aggregations = new TermsAggregation("agg_oc3sysno") { Field = "onlinecategory.category3Sysno", Order = new List<TermsOrder>() { new TermsOrder() { Key = "_count", Order = SortOrder.Descending } } } }; //執行搜索 SearchRequest<NestProduct> searchRequest = new SearchRequest<NestProduct>("nestproduct", "product") { From = 0, Size = 100, Query = bq, //Sort= sortlist Aggregations= nestedAgg }; var client = ElasticSearchCommon.GetInstance().GetElasticClient(); var response = client.Search<NestProduct>(searchRequest); //結果處理 List<NestProduct> prolist = new List<NestProduct>(); foreach (var i in response.Hits) { prolist.Add(i.Source); } //獲取聚合信息 List<NestCategory> nestcat = new List<NestCategory>(); //獲取指定的桶對象 var buck = response.Aggs.Aggregations["agg_onlinecategory"] as SingleBucketAggregate; var termbuck = buck.Aggregations["agg_oc3sysno"] as BucketAggregate; foreach (KeyedBucket b in termbuck.Items) { nestcat.Add(new NestCategory() { Category3Sysno = b.Key }); } return prolist; }
八: mapping父子

//創建父子類型mapping,在子類型上指定父類型是誰
http://192.168.60.61:29200/qtorder
{ "mappings": { "order": { "properties": { "sysno": { "type": "string", "index": "not_analyzed" }, "uid": { "type": "string", "index": "not_analyzed" }, "createTime": { "type": "date", "index": "not_analyzed" } } }, "orderItem": { "_parent": { //此處只需要指定父類型是誰即可
"type": "order" }, "properties": { "productname": { "type": "string", "index": "analyzed" }, "productid": { "type": "string", "index": "not_analyzed" }, "ordersysno": { "type": "string", "index": "not_analyzed" } } } } }
插入嵌套關系的數據,例如一個訂單里面有3個商品,那么對象關系是一個訂單對象里包含了一個商品列表對象,列表里有3個商品數據;而我們需要創建一個訂單類型索引數據為父文檔,三個產品類型索引數據為子文檔

public class objmanger { //sysno為訂單號,指定默認id為訂單號(唯一的)
public static List<order> CreateOrder() { List<order> orders = new List<order>(); orders.Add(new order { id="1001", createTime="2016-07-01", sysno="1001", uid="u1" }); orders.Add(new order { id = "1002", createTime = "2016-07-02", sysno = "1002", uid = "u2" }); return orders; } //ordersysno為訂單號,對應order對象里的sysno和id
public static List<orderItem> CreateOrderItem() { List<orderItem> items = new List<orderItem>(); items.Add(new orderItem { ordersysno = "1001", productid = "p0011", productname = "產品11" }); items.Add(new orderItem { ordersysno = "1001", productid = "p0012", productname = "產品12" }); items.Add(new orderItem { ordersysno = "1001", productid = "p0013", productname = "產品13" }); items.Add(new orderItem { ordersysno = "1002", productid = "p0014", productname = "產品14" }); items.Add(new orderItem { ordersysno = "1002", productid = "p0015", productname = "產品15" }); items.Add(new orderItem { ordersysno = "1002", productid = "p0016", productname = "產品16" }); return items; } }
插入數據

public static void createparent() { var client = ElasticSearchCommon.GetInstance().GetElasticClient(); var des = new BulkDescriptor().Index("qtorder"); var orders = objmanger.CreateOrder(); foreach (var i in orders) { des.Index<order>(o => o.Document(i)); } var response = client.Bulk(des); } public static void createchild() { var client = ElasticSearchCommon.GetInstance().GetElasticClient(); var des = new BulkDescriptor().Index("qtorder"); var items = objmanger.CreateOrderItem(); foreach (var i in items) { des.Type("orderItem").Index<orderItem>(o => o.Document(i).Parent(i.ordersysno)); } var response2 = client.Bulk(des); //foreach(var i in items) //{ // var request = new IndexRequest<orderItem>("qtorder", "orderItem", i.productid); // request.Document = i; // request.Parent=i.orderid; // var respon= client.Index(request); //}
}
查詢

查詢子文檔商品名稱包含 “產品” 的父文檔訂單信息(只返回父文檔信息): post http://192.168.60.61:29200/qtorder/order/_search
{ "query":{ "has_child": { "type": "orderItem", "query": { "match": { "productname": "產品" } } } } } 返回結果: { "took": 8, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "qtorder", "_type": "order", "_id": "1001", "_score": 1, "_source": { "id": "1001", "ordersysno": "1001", "uid": "u1", "createTime": "2016-07-01" } }, { "_index": "qtorder", "_type": "order", "_id": "1002", "_score": 1, "_source": { "id": "1002", "ordersysno": "1002", "uid": "u2", "createTime": "2016-07-02" } } ] } }
查詢用戶u1買過的所有商品,(根據父文檔條件查詢子文檔信息,只返回子文檔信息),注意,下面orderid就是上面的ordersysno,忘改:
nest代碼

/// <summary>
/// 嵌套查詢 /// 使用嵌套在一些情況下不用聚合就能得到結果 /// </summary>
public static void query() { var client = ElasticSearchCommon.GetInstance().GetElasticClient(); //TypeName [] ts = new TypeName[] { "order"};
var result = client.Search<order>(s => s .Index("qtorder") .Type("order") //.Query(q => q.Term("uid", "u1")) //.Query(q => q.HasChild<orderItem>(c => c.Type("orderItem").Query(cq => cq.Term("productid", "p0011"))))//查詢買過商品p0011的 訂單
.Query(q => q.HasChild<orderItem>(c => c.Type("orderItem").Query(cq => cq.Match(o => o.Name("productname").Query("蘋果")))))//查詢子文檔商品名稱包含蘋果的父文檔信息
); IEnumerable<order> ss = result.Documents; }

//多個文檔類型查詢
public static void query() { var client = ElasticSearchCommon.GetInstance().GetElasticClient(); TypeName[] ts = new TypeName[] { "order", "giftOrder" }; var result = client.Search<order>(s => s .Index("qtorder") .Type(ts) //.Query(q => q.HasChild<orderItem>(c => c.Type("orderItem").Query(cq => cq.Term("productid", "p0011"))))//查詢買過商品p0011的 訂單
.Query(q => q.HasChild<orderItem>(c => c .Type("orderItem").Query(cq => cq.Match(o => o.Name("productname").Query("產品"))) .Type("giftOrderItem").Query(cq => cq.Match(o => o.Name("productname").Query("產品"))) ))//查詢子文檔商品名稱包含蘋果的父文檔信息
); IEnumerable<order> ss = result.Documents; }
es 支持文檔嵌套和父子文檔,文檔嵌套式內容嵌套,一個大文檔里面包含多個小文檔;父子文檔是指為兩個獨立的文檔類型建立父子關聯關系;
父子文檔查詢時也支持多個類型查詢
文檔id:同一文檔類型下,文檔id不能重復,同一索引不同類型下的兩個文檔id可以是相同的

//查詢訂單內容包含“禮金”,且用戶是2340458的訂單號,父子條件同時查詢
http://192.168.60.61:29200/order20160801/giftOrder/_search
{ "query": { "bool": { "must": [ { "term": { "uid": "2340458" } }, { "has_child": { "type": "giftOrderProduct", "query": { "term": { "productName": "禮金" } } } } ] } } }

//查詢根據多個子類型信息,已經父類型信息,查詢多個父類型。查詢用戶23130購買過包含“電子“的商品的訂單(包含普通訂單和禮品卡訂單)
http://192.168.60.61:29200/order20160801/giftOrder,order/_search
{ "from":0, "size":100, "query": { "bool":{"should":[ {"bool":{"must":[{"term": {"uid": "23130"}}, { "has_child": {"type": "giftOrderProduct","query": {"term": {"productName": "電子"}}}}]}}, {"bool":{"must":[{"term": {"uid": "23130"}}, { "has_child": {"type": "orderProduct","query": {"term": {"productName": "電子"}}}}]}} ]} } }