<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之旅 廣告
                # Sort 原文鏈接 : [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html) 譯文鏈接 : [http://www.apache.wiki/pages/editpage.action?pageId=48830](http://www.apache.wiki/pages/editpage.action?pageId=4883051)78 貢獻者 : [ping](/display/~wangyangting) 允許在特定字段上添加一個或多個排序。 每個排序也可以顛倒。 排序是在每個字段級別上定義的,具有 _score 的特殊字段名稱按分數排序,_doc 按索引順序排序。 假設以下索引映射: ``` PUT /my_index { "mappings": { "my_type": { "properties": { "post_date": { "type": "date" }, "user": { "type": "keyword" }, "name": { "type": "keyword" }, "age": { "type": "integer" } } } } } ``` ``` GET /my_index/my_type/_search { "sort" : [ { "post_date" : {"order" : "asc"}}, "user", { "name" : "desc" }, { "age" : "desc" }, "_score" ], "query" : { "term" : { "user" : "kimchy" } } } ``` Note:? _doc 除了是最有效的排序順序沒有真正的用例。所以如果你不關心文檔返回的順序,那么你應該按 _doc 排序。 這特別有助于滾動。 ## Sort Values 返回的每個文檔的排序值也作為響應的一部分返回。 ## Sort Order 排序選項可以有以下值: | `asc` | 按升序排序 | | `desc` | 按倒序排序 | 在對 _score 進行排序時,該順序默認為 desc,在對其他事物進行排序時默認為 asc。? Sort mode Option Elasticsearch支持按數組或多值字段排序。 mode 選項控制選擇用于對其所屬文檔進行排序的數組值。 mode 選項可以具有以下值: | `min` | 選擇最低值。 | | `max` | 選擇最高值。 | | `sum` | 使用所有值的和作為排序值。 僅適用于基于數字的數組字段。 | | `avg` | 使用所有值的平均值作為排序值。 僅適用于基于數字的數組字段。 | | `median` | 使用所有值的中值作為排序值。 僅適用于基于數字的數組字段。 | 排序模式示例用法 在下面的示例中,每個文檔字段價格有多個價格。 在這種情況下,結果匹配將按基于每個文檔的平均價格的升序排序。 ``` PUT /my_index/my_type/1?refresh { "product": "chocolate", "price": [20, 4] } POST /_search { "query" : { "term" : { "product" : "chocolate" } }, "sort" : [ {"price" : {"order" : "asc", "mode" : "avg"}} ] } ``` ### Sorting within nested objects Elasticsearch 還支持根據一個或多個嵌套對象內的字段進行排序。 通過嵌套字段支持進行的排序在已經存在的排序選項之上具有以下參數: nested_path 定義要排序的嵌套對象。 實際排序字段必須是此嵌套對象內的直接字段。 當通過嵌套字段排序時,此字段是必需的。 nested_filter 嵌套路徑中的內部對象應與其匹配的過濾器,以便通過排序考慮其字段值。 常見的情況是在嵌套的過濾器或查詢中重復查詢/過濾。 默認情況下,沒有 nested_filter 是激活的。 Nested sorting example 在下面的示例中,offer是一個類型為嵌套的字段。 需要指定nested_path; 否則,elasticsearch不知道需要捕獲哪個嵌套級排序值。 ``` POST /_search { "query" : { "term" : { "product" : "chocolate" } }, "sort" : [ { "offer.price" : { "mode" : "avg", "order" : "asc", "nested_path" : "offer", "nested_filter" : { "term" : { "offer.color" : "blue" } } } } ] } ``` 當通過腳本排序和按地理距離排序時,也支持嵌套排序。 ### Missing Values 缺少的參數指定應如何處理缺少字段的文檔:缺少的值可以設置為 _last,_first 或自定義值(將用于缺少文檔作為排序值)。 例如: ``` GET /_search { "sort" : [ { "price" : {"missing" : "_last"} } ], "query" : { "term" : { "product" : "chocolate" } } } ``` Note: 如果嵌套的內部對象與 nested_filter 不匹配,則使用缺少的值。 ### Ignoring Unmapped Fields 默認地,如果沒有與字段關聯的映射,搜索請求將失敗。 unmapped_type 選項允許忽略沒有映射且沒有由它們排序的字段。 此參數的值用于確定要發出的排序值。下面是一個如何使用它的例子: ``` GET /_search { "sort" : [ { "price" : {"unmapped_type" : "long"} } ], "query" : { "term" : { "product" : "chocolate" } } } ``` 如果查詢的任何索引沒有價格的映射,那么Elasticsearch將處理它,就好像存在類型為 **long** 的映射,其中該索引中的所有文檔都沒有該字段的值。 ### Geo Distance Sorting 允許按 _geo_distance 排序。 下面是一個例子,假設 pin.location 是一個類型為 geo_point 的字段: ``` GET /_search { "sort" : [ { "_geo_distance" : { "pin.location" : [-70, 40], "order" : "asc", "unit" : "km", "mode" : "min", "distance_type" : "sloppy_arc" } } ], "query" : { "term" : { "user" : "kimchy" } } } ``` **distance_type** 如何計算距離。 可以是 sloppy_arc(默認),弧(稍微更精確但顯著更慢)或平面(更快,但不準確在長距離和接近極點)。 **mode** 如果字段有多個地理點,該怎么辦。 默認情況下,按升序排序時考慮最短距離,按降序排序時最長距離。 支持的值為 min,max,median 和 avg。 **unit** 計算排序值時使用的單位。 默認值為 m(米)。 geo distance sorting?不支持可配置的缺失值:當文檔沒有用于距離計算的字段的值時,距離將始終被視為等于 Infinity。 在提供坐標時支持以下格式: #### Lat Lon as Properties ``` GET /_search { "sort" : [ { "_geo_distance" : { "pin.location" : { "lat" : 40, "lon" : -70 }, "order" : "asc", "unit" : "km" } } ], "query" : { "term" : { "user" : "kimchy" } } } ``` #### Lat Lon as String 在 lat,lon 中的格式。 ``` GET /_search { "sort" : [ { "_geo_distance" : { "pin.location" : "40,-70", "order" : "asc", "unit" : "km" } } ], "query" : { "term" : { "user" : "kimchy" } } } ``` #### Geohash ``` GET /_search { "sort" : [ { "_geo_distance" : { "pin.location" : "drm3btev3e86", "order" : "asc", "unit" : "km" } } ], "query" : { "term" : { "user" : "kimchy" } } } ``` #### Lat Lon as Array 格式在 [lon,lat],注意,lon / lat 的順序在這里為了符合 GeoJSON。 ``` GET /_search { "sort" : [ { "_geo_distance" : { "pin.location" : [-70, 40], "order" : "asc", "unit" : "km" } } ], "query" : { "term" : { "user" : "kimchy" } } } ``` ### Multiple reference points 多個地理點可以作為一個包含任何 geo_point 格式的數組傳遞,例如, ``` GET /_search { "sort" : [ { "_geo_distance" : { "pin.location" : [[-70, 40], [-71, 42]], "order" : "asc", "unit" : "km" } } ], "query" : { "term" : { "user" : "kimchy" } } } ``` 等等。 文檔的最終距離將是包含在文檔中的所有點的最小/最大/平均(通過模式定義)到在排序請求中給出的所有點的距離。 ### Script Based Sorting 允許基于自定義腳本排序,這里是一個例子: ``` GET /_search { "query" : { "term" : { "user" : "kimchy" } }, "sort" : { "_script" : { "type" : "number", "script" : { "lang": "painless", "inline": "doc['field_name'].value * params.factor", "params" : { "factor" : 1.1 } }, "order" : "asc" } } } ``` ### Track Scores 在字段上排序時,不會計算分數。 通過將 track_scores 設置為 true,仍將計算和跟蹤分數。 ``` GET /_search { "track_scores": true, "sort" : [ { "post_date" : {"order" : "desc"} }, { "name" : "desc" }, { "age" : "desc" } ], "query" : { "term" : { "user" : "kimchy" } } } ``` ### Memory Considerations 當排序時,相關的排序字段值被加載到存儲器中。 這意味著每個分片,應該有足夠的內存來容納它們。 對于基于字符串的類型,排序的字段不應被分析/標記化。 對于數字類型,如果可能,建議明確設置類型為更窄的類型(如 short,integer 和 float )。
                  <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>

                              哎呀哎呀视频在线观看