<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] # 簡介 關系型數據庫查詢有where語句,可以找到我們想要的數據 filter和這個類似 filter查詢能拿到我們想要的數據,查到的數據會緩存在內存中cache緩存 建立測試數據 ~~~ POST /store/products/_bulk {"index":{"_id":1}} {"price":10,"productID":"SD1002136"} {"index":{"_id":2}} {"price":20,"productID":"SD2678421"} {"index":{"_id":3}} {"price":30,"productID":"SD8897573"} {"index":{"_id":4}} {"price":30,"productID":"SD4535233"} ~~~ 查看測試數據 ~~~ GET /store/products/_mget { "ids": ["1","2","3","4"] } ~~~ 查看library的mapping信息 ~~~ GET /store/_mapping ~~~ # 簡單過濾查詢 ## 最簡單filter查詢 ~~~ select document from products where price = 20 ~~~ filtered查詢價格是20的商品 ~~~ GET /store/products/_search { "query": { "bool": { "filter": { "term": { "price": "20" } } } } } ~~~ 也可以指定多個值 ~~~ GET /store/products/_search { "query": { "bool": { "filter": { "terms": { "price": ["10", "20"] } } } } } ~~~ ~~~ select product from products where productID = "SD4535233" ~~~ ~~~ GET /store/products/_search { "query": { "filtered": { "filter": { "term": { "productID":"SD4535233" } } } } } ~~~ 這個沒有命中任何結果 我們看下mapping,發現是有的,是被分析的 查看分析器解析的結果 ~~~ GET /_analyze?text=SD4535233 ~~~ 發現分析的結果是sd4535233 我們刪除他,重新建立映射 ~~~ DELETE /store PUT /store { "mappings": { "products":{ "properties": { "productID": { "type": "string", "index": "not_analyzed" } } } } } ~~~ 然后他不被分析,他就不是小寫,因為分析的時候,會把他小寫,或者分段 ## bool過濾查詢,可以做組合過濾查詢 ~~~ select product from products where (price = 20 or productID= "SD1002136") and (price != 30); # 查詢價格等于20的或者productID為SD1002136的商品,排除價格30元的 ~~~ 類似的,elasticsearch也有and,or,not這樣的組合條件的查詢方式 格式如下 ~~~ { "bool": { "must":[], "should":[], "must_not":[], } } ~~~ must條件必須滿足,相當于and should:條件可以滿足也可以不滿足,相當于or must_not:條件不需要,相當于not ~~~ GET /store/products/_search { "query": { "bool": { "filter": { "bool": { "should": [ {"term": {"price": 20}}, {"term": {"productID": "SD1002136"}} ], "must_not": { "term": {"price": 30} } } } } } } ~~~ ## 嵌套查詢 ~~~ select document from products where productID="SD1002136" OR (productID = "SD4535233" and price = 30); ~~~ ~~~ GET /store/products/_search { "query": { "bool": { "filter": { "bool": { "should": [ {"term": {"productID": "SD1002136"}}, { "bool": { "must": [ {"term": {"productID": "SD4535233"}}, {"term": {"price": "30"}} ] }} ] } } } } } ~~~ 另外一種,and,or,not查詢 沒有bool,直接使用and, or, not 查詢價格即是10元,productID又為SD1002136的結果 ~~~ GET /store/products/_search { "query": { "bool": { "must": [ { "match": { "price": 10 } }, { "match": { "productID": "SD1002136" } } ] } } } ~~~ or 查詢價格是10元或productID是SD4535233的一些商品 ~~~ GET /store/products/_search { "query": { "bool": { "should": [ { "match": { "price": 10 } }, { "match": { "productID": "SD4535233" } } ] } } } ~~~ not 查詢productID不是SD1002136的商品 ~~~ GET /store/products/_search { "query": { "bool": { "must_not": [ { "match": { "productID": "SD1002136" } } ] } } } ~~~ # range范圍過濾 ~~~ select document from products where price between 20 and 40 gt : > 大于 lt : < 小于 gte : >= 大于等于 lte : <= 小于等于 ~~~ ~~~ GET /store/products/_search { "query": { "filtered": { "filter": { "range": { "price": { "gte": 20, "lte": 40 } } } } } } ~~~ # 過濾空和非空 建立測試數據 ~~~ POST /test_index/test/_bulk {"index": {"_id":"1"}} {"tags":["search"]} {"index": {"_id":"2"}} {"tags":["open_source"]} {"index": {"_id":"3"}} {"other_fields":"some data"} {"index": {"_id":"4"}} {"tags":null} {"index": {"_id":"5"}} {"tags":["search",null]} ~~~ 處理null空值的方法 ~~~ select tags from test where tags is not null select tags from test where tags is null ~~~ ~~~ GET /test_index/test/_search { "query": { "filtered": { "filter": { "exists": {"field": "tags"} } } } } ~~~ ~~~ GET /test_index/test/_search { "query": { "filtered": { "filter": { "missing": {"field": "tags"} } } } } ~~~ # cache緩存 elasticsearch在執行帶有filter查詢時,會打開索引的每個segment文件(Luncene式底層文件),然后去判斷里面的文檔是否符合filter要求 注意:舊的segment文件不會變,新來的數據會產生新的segment 匹配的結果會用一個大型的BigSet數組來存儲,這個數組的值只有0和1 匹配:1 不匹配:0 BigSet值是存在內存里的,而不是硬盤里,所以速度快 開啟方式:在filter查詢語句后面加`"_cache":true` 注意: Scriptfilters,Geo-filters,Date ranges這樣的過濾方式開啟cache無意義 exists,missing,range,term和terms查詢是默認開啟cache的 filter cache緩存執行原理圖 ![](https://img.kancloud.cn/ad/d1/add1a4061c0d7dfc6263056b5f7c4c7f_748x259.png) 匹配的結果會放在Bigset這個數組,匹配上就記為1,沒匹配上就為0 ![](https://img.kancloud.cn/19/00/1900906f8b63ca17420e593a38acbacf_379x299.png)
                  <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>

                              哎呀哎呀视频在线观看