<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 1. term filter 與keyword 測試數據 ~~~ POST /forum/article/_bulk { "index": { "_id": 1 }} { "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" } { "index": { "_id": 2 }} { "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" } { "index": { "_id": 3 }} { "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" } { "index": { "_id": 4 }} { "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" } ~~~ 1. 匹配text ~~~ GET forum/_search { "query": { "constant_score": { # 固定分值,就不用計算相關度,過濾唄 "filter": { "term": { "articleID": "KDKE-B-9947-#kL5" } }, "boost": 1.2 } } } ~~~ > articleID被分詞成:kdke,b,9947,kl5,而term過濾是精準過濾,KDKE-B-9947-#kL5不會被分詞,還是用KDKE-B-9947-#kL5去查詢過濾,所以這條過濾不能得到document。 結果啥也沒搜到 ~~~ { "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 0, "max_score": null, "hits": [] } } ~~~ 2. 再用articleID.keyword去過濾 keyword參考elasticsearch基礎 type=keyword ~~~ GET forum/_search { "query": { "constant_score": { "filter": { "term": { "articleID.keyword": "KDKE-B-9947-#kL5" } }, "boost": 1.2 # 固定分值,不指定默認為1 } } } ~~~ 結果 ~~~ { "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 1.2, "hits": [ { "_index": "forum", "_type": "article", "_id": "2", "_score": 1.2, "_source": { "articleID": "KDKE-B-9947-#kL5", "userID": 1, "hidden": false, "postDate": "2017-01-02" } } ] } } ~~~ 或者這么寫 ~~~ GET forum/_search { "query": { "bool": { "filter": { "term": { "articleID.keyword": "KDKE-B-9947-#kL5" } } } } } ~~~ ## 2. 組合查詢 ~~~ GET forum/_search { "query": { "bool": { "should": [ {"term": {"postDate":"2017-01-01"}}, {"term": {"articleID":"QQPX-R-3956-#aD8"}} # 文檔被分詞,查詢條件(不被分詞)無法精準匹配到QQPX-R-3956-#aD8的文檔 ] } } } ~~~ ~~~ GET forum/_search { "query": { "bool": { "should": [ {"term": {"postDate":"2017-01-01"}}, {"term": {"articleID.keyword":"QQPX-R-3956-#aD8"}} # text 字段keyword屬性,不分詞,查詢到QQPX-R-3956-#aD8 ] } } } ~~~ ## 3. terms 查詢 1. 查找articleID是QQPX-R-3956-#aD8 或者KDKE-B-9947-#kL5的文檔 ~~~ GET forum/_search { "query": { "terms": { "articleID.keyword": [ "QQPX-R-3956-#aD8", "KDKE-B-9947-#kL5" ] } } } ~~~ ## 4. range 1. range查詢 ~~~ GET forum/_search { "query": { "range": { "view_cnt": { "gte": 50, "lte": 90 } } } } ~~~ 2. range過濾 ~~~ GET forum/_search { "query": { "constant_score": { "filter": { "range": { "view_cnt": { "gte": 50, "lte": 90 } } } } } } ~~~ * 同樣的效果range過濾比range查詢要快 ## 5. 分組聚合 穿件index和映射 ~~~ PUT /tvs { "mappings": { "sales": { "properties": { "price": { "type": "long" }, "color": { "type": "keyword" }, "brand": { "type": "keyword" }, "sold_date": { "type": "date" } } } } } ~~~ 數據 ~~~ POST /tvs/sales/_bulk { "index": {}} { "price" : 1000, "color" : "紅色", "brand" : "長虹", "sold_date" : "2016-10-28" } { "index": {}} { "price" : 2000, "color" : "紅色", "brand" : "長虹", "sold_date" : "2016-11-05" } { "index": {}} { "price" : 3000, "color" : "綠色", "brand" : "小米", "sold_date" : "2016-05-18" } { "index": {}} { "price" : 1500, "color" : "藍色", "brand" : "TCL", "sold_date" : "2016-07-02" } { "index": {}} { "price" : 1200, "color" : "綠色", "brand" : "TCL", "sold_date" : "2016-08-19" } { "index": {}} { "price" : 2000, "color" : "紅色", "brand" : "長虹", "sold_date" : "2016-11-05" } { "index": {}} { "price" : 8000, "color" : "紅色", "brand" : "三星", "sold_date" : "2017-01-01" } { "index": {}} { "price" : 2500, "color" : "藍色", "brand" : "小米", "sold_date" : "2017-02-12" } ~~~ 1. 按照color分組 ~~~ GET /tvs/_search { "size": 0, # 只顯示聚合結果 "aggs": { "group_by_color": { "terms": { "field": "color", "size": 10000 # 控制返回結果數量 } } } } ~~~ 得到 ~~~ "buckets": [ { "key": "紅色", "doc_count": 4 }, { "key": "綠色", "doc_count": 2 }, { "key": "藍色", "doc_count": 2 } ] } } ~~~ 2. 求每種顏色電視銷售價格的平均值 ~~~ GET /tvs/_search { "size": 0, # 只顯示聚合結果 "aggs": { "group_by_color": { "terms": { "field": "color", "size": 1000 }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } ~~~ color》brand》avg ~~~ GET tvs/_search { "size": 0, "aggs": { "group_by_color": { "terms": { "field": "color", "size": 10 }, "aggs": { "group_by_brand": { "terms": { "field": "brand", "size": 10 }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } } } ~~~ 3. 求顏色下的價格最高,最小值,價格總數 ~~~ GET /tvs/_search { "size": 0, "aggs": { "group_by_color": { "terms": { "field": "color", "size": 10000 }, "aggs": { "max_price": { "max": { "field": "price" } }, "min_price":{ "min": { "field": "price" } }, "sum_price":{ "sum": { "field": "price" } } } } } } ~~~ 得到 ~~~ "buckets": [ { "key": "紅色", "doc_count": 4, "max_price": { "value": 8000 }, "min_price": { "value": 1000 }, "sum_price": { "value": 13000 } }, { "key": "綠色", "doc_count": 2, "max_price": { "value": 3000 }, "min_price": { "value": 1200 }, "sum_price": { "value": 4200 } }, { "key": "藍色", "doc_count": 2, "max_price": { "value": 2500 }, "min_price": { "value": 1500 }, "sum_price": { "value": 4000 } ~~~ ## 6. histogram 1. 求以2000為步長,電視的銷售總價 ~~~ GET /tvs/_search { "size": 0, "aggs": { "group_by_price": { "histogram": { "field": "price", "interval": 2000 }, "aggs": { "NAME": { "sum": { "field": "price" } } } } } } ~~~ 得到 ~~~ "group_by_price": { "buckets": [ { "key": 0, "doc_count": 3, "NAME": { "value": 3700 } }, { "key": 2000, "doc_count": 4, "NAME": { "value": 9500 } }, { "key": 4000, "doc_count": 0, "NAME": { "value": 0 } }, { "key": 6000, "doc_count": 0, "NAME": { "value": 0 } }, { "key": 8000, "doc_count": 1, "NAME": { "value": 8000 } } ] ~~~ 2. 求每個月的銷售總價 ## 7. 求季度每個品牌的銷售額 ~~~ GET tvs/_search { "size": 0, "aggs": { "sales": { "date_histogram": { "field": "sold_date", "interval": "quarter", "format": "yyyy-MM-dd", "min_doc_count": 0, "extended_bounds":{ "min":"2016-01-01", "max":"2017-12-12" } }, "aggs": { "group_price_month": { "terms": { "field": "brand" }, "aggs": { "sales_sum": { "sum": { "field": "price" } } } } } } } } ~~~ ~~~ }, { "key_as_string": "2016-07-01", "key": 1467331200000, "doc_count": 2, "group_price_month": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "TCL", "doc_count": 2, "sales_sum": { "value": 2700 } } ] } }, { "key_as_string": "2016-10-01", "key": 1475280000000, "doc_count": 3, "group_price_month": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "長虹", "doc_count": 3, "sales_sum": { "value": 5000 } } ] } }, { ~~~ ## 8. filter與aggression 查找電視價格大于2500的銷售平均價格 首先把價格大于2500的電視過濾出來,在聚合 ~~~ GET /tvs/_search { "size": 0, "query": { "constant_score": { "filter": { "range": { "price": { "gt": 2500 } } } } }, "aggs": { "avg_lgt1200": { "avg": { "field": "price" } } } } ~~~ 得到 ~~~ "hits": [] }, "aggregations": { "avg_lgt1200": { "value": 5500 } } } ~~~ ## 9. 最近一個月某品牌的銷售總價 ~~~ GET /tvs/_search { "size": 0, "query": { "constant_score": { "filter": { "term": { "brand": "長虹" } } } }, "aggs": { "recent_moth":{ "filter": { # 在這里過濾,因為只在查詢的結果中過濾,性能最優 "range": { "sold_date": { "gte": "now-30d" # now-30d 從現在開始,減30天 } } }, "aggs": { "sum_price": { "sum": { "field": "price" } } } } } } ~~~ 得到 ~~~ "aggregations": { "recent_moth": { "doc_count": 3, "sum_price": { "value": 5000 } } } } ~~~ ## 10. 聚合排序 1. 一個桶排序 ~~~ GET /tvs/_search { "size": 0, "aggs": { "groupbycolor": { "terms": { "field": "color", "order": { "avg_price": "asc" # 指定按照avg_price排序 } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } ~~~ 2. 2個桶排序 ~~~ GET /tvs/sales/_search { "size": 0, "aggs": { "group_by_color": { "terms": { "field": "color", "order": { "_count": "asc" # 大桶按照doc count升序 } }, "aggs": { "group_by_brand": { "terms": { "field": "brand", "order": { "avg_price": "desc" # 小桶降序 } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } } } ~~~ ## 11. 網站訪問時間統計 ~~~ PUT /website { "mappings": { "logs":{ "properties": { "latency":{ "type": "long" }, "province":{ "type": "keyword" }, "timestamp":{ "type": "date" } } } } } ~~~ ~~~ POST /website/logs/_bulk { "index": {}} { "latency" : 105, "province" : "江蘇", "timestamp" : "2016-10-28" } { "index": {}} { "latency" : 83, "province" : "江蘇", "timestamp" : "2016-10-29" } { "index": {}} { "latency" : 92, "province" : "江蘇", "timestamp" : "2016-10-29" } { "index": {}} { "latency" : 112, "province" : "江蘇", "timestamp" : "2016-10-28" } { "index": {}} { "latency" : 68, "province" : "江蘇", "timestamp" : "2016-10-28" } { "index": {}} { "latency" : 76, "province" : "江蘇", "timestamp" : "2016-10-29" } { "index": {}} { "latency" : 101, "province" : "新疆", "timestamp" : "2016-10-28" } { "index": {}} { "latency" : 275, "province" : "新疆", "timestamp" : "2016-10-29" } { "index": {}} { "latency" : 166, "province" : "新疆", "timestamp" : "2016-10-29" } { "index": {}} { "latency" : 654, "province" : "新疆", "timestamp" : "2016-10-28" } { "index": {}} { "latency" : 389, "province" : "新疆", "timestamp" : "2016-10-28" } { "index": {}} { "latency" : 302, "province" : "新疆", "timestamp" : "2016-10-29" } ~~~ ~~~ 需求:比如有一個網站,記錄下了每次請求的訪問的耗時,需要統計tp50,tp90,tp99 tp50:50%的請求的耗時最長在多長時間 tp90:90%的請求的耗時最長在多長時間 tp99:99%的請求的耗時最長在多長時間 ~~~ ~~~ GET /website/_search { "aggs": { "latency_percentiles": { "percentiles": { "field": "latency", "percents": [ 50, 99 ] } } } } ~~~ 得到 ~~~ }, "aggregations": { "latency_percentiles": { "values": { "50.0": 108.5, # 50%的請求在108以內 "99.0": 624.8500000000001 # 99%的請求在624以內 } } } } ~~~ 2. 統計各省的訪問情況 * 一個aggs內可以包含一個aggs * 一個aggs可以包含多個不同的metric(指標統計:平均值,最大值,,,),但是記得起名字 ~~~ GET /website/logs/_search { "size": 0, "aggs": { "group_by_province": { "terms": { "field": "province" }, "aggs": { "latency_percentiles": { "percentiles": { "field": "latency", "percents": [ 50, 95, 99 ] } }, "latency_avg": { "avg": { "field": "latency" } } } } } } ~~~ 得到,新疆的網絡不行啊 ~~~ "buckets": [ { "key": "新疆", "doc_count": 6, "latency_avg": { "value": 314.5 }, "latency_percentiles": { "values": { "50.0": 288.5, "95.0": 587.75, "99.0": 640.75 } } }, { "key": "江蘇", "doc_count": 6, "latency_avg": { "value": 89.33333333333333 }, "latency_percentiles": { "values": { "50.0": 87.5, "95.0": 110.25, "99.0": 111.65 } } } ] } } } ~~~ ## 12. percentile_ranks(求某數值所占比例) > SLA:就是你提供的服務的標準 > 我們的網站的提供的訪問延時的SLA,確保所有的請求100%,都必須在200ms以內,大公司內,一般都是要求100%在200ms以內 > 如果超過1s,則需要升級到A級故障,代表網站的訪問性能和用戶體驗急劇下降 > 需求:在200ms以內的,有百分之多少,在1000毫秒以內的有百分之多少,percentile ranks metric > 這個percentile ranks,其實比pencentile還要常用 > 按照品牌分組,計算,電視機,售價在1000占比,2000占比,3000占比 ~~~ GET /website/_search { "size": 0, "aggs": { "groupby_province": { "terms": { "field": "province" }, "aggs": { "percentile_s": { "percentile_ranks": { "field": "latency", "values": [ 100, 300, 500 ] } } } } } } ~~~ 得到 ~~~ "buckets": [ { "key": "新疆", "doc_count": 6, "percentile_s": { "values": { "100.0": 8.076923076923077, "300.0": 53.3625730994152, "500.0": 65.31446540880503 } } }, { "key": "江蘇", "doc_count": 6, "percentile_s": { "values": { "100.0": 46.42857142857142, "300.0": 100, "500.0": 100 } } } ] } } } ~~~ > 新疆: > 響應時間小于100毫秒占46.42% > 響應時間小于300毫秒占53.42% > 響應時間小于500毫秒占65.42% > ## 13. 文檔模型 1. 建立索引,已經文件路徑分詞器 ~~~ PUT /fs { "settings": { "analysis": { "analyzer": { "paths": { "tokenizer": "path_hierarchy" } } } } } ~~~ path_hierarchy:路徑分詞器 2. 創建映射 ~~~ PUT /fs/_mapping/file { "properties": { "name": { "type": "keyword" }, "path": { "type": "keyword", # 不分詞的路徑,用于精準匹配 "fields": { "tree": { "type": "text", "analyzer": "paths" # 分詞子field,用于所有路徑的搜索 } } } } } ~~~ 3. 插入一條數據 ~~~ PUT /fs/file/1 { "name": "README.txt", "path": "/workspace/projects/helloworld", "contents": "這是我的第一個elasticsearch程序" } ~~~ 4. 搜索文件 1. 查找/workspace/projects/helloworld路徑下的elasticsearch文件 ~~~ GET /fs/file/_search { "query": { "bool": { "must": [ { "match": { "contents": "elasticsearch" } }, { "constant_score": { "filter": { "term": { "path": "/workspace/projects/helloworld" } } } } ] } } } ~~~ ~~~ "hits": [ { "_index": "fs", "_type": "file", "_id": "1", "_score": 1.284885, "_source": { "name": "README.txt", "path": "/workspace/projects/helloworld", "contents": "這是我的第一個elasticsearch程序" } } ] } } ~~~ 5. 查找/workspace目錄下的所有elasticsearch文件 ~~~ GET /fs/file/_search { "query": { "bool": { "must": [ { "match": { "contents": "elasticsearch" } }, { "constant_score": { "filter": { "term": { "path.tree": "/workspace" # 用可分詞的field,過濾所有路徑 } } } } ] } } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看