瀏覽代碼

docs: add sort workaround

Martijn van Groningen 9 年之前
父節點
當前提交
7c9af98a3c
共有 2 個文件被更改,包括 62 次插入0 次删除
  1. 31 0
      docs/reference/query-dsl/has-child-query.asciidoc
  2. 31 0
      docs/reference/query-dsl/has-parent-query.asciidoc

+ 31 - 0
docs/reference/query-dsl/has-child-query.asciidoc

@@ -93,3 +93,34 @@ and will not match any documents for this query. This can be useful when
 querying multiple indexes which might have different mappings. When set to
 `false` (the default value) the query will throw an exception if the `type`
 is not mapped.
+
+[float]
+==== Sorting
+
+Parent documents can't be sorted by fields in matching child documents via the
+regular sort options. If you need to sort parent document by field in the child
+documents then you can should use the `function_score` query and then just sort
+by `_score`.
+
+Sorting blogs by child documents' `click_count` field:
+
+[source,js]
+--------------------------------------------------
+GET /_search
+{
+    "query": {
+        "has_child" : {
+            "type" : "blog_tag",
+            "score_mode" : "max",
+            "query" : {
+                "function_score" : {
+                    "script_score": {
+                        "script": "_score * doc['click_count'].value"
+                    }
+                }
+            }
+        }
+    }
+}
+--------------------------------------------------
+// CONSOLE

+ 31 - 0
docs/reference/query-dsl/has-parent-query.asciidoc

@@ -63,3 +63,34 @@ and will not match any documents for this query. This can be useful when
 querying multiple indexes which might have different mappings. When set to
 `false` (the default value) the query will throw an exception if the `type`
 is not mapped.
+
+[float]
+==== Sorting
+
+Child documents can't be sorted by fields in matching parent documents via the
+regular sort options. If you need to sort child documents by field in the parent
+documents then you can should use the `function_score` query and then just sort
+by `_score`.
+
+Sorting tags by parent document' `view_count` field:
+
+[source,js]
+--------------------------------------------------
+GET /_search
+{
+    "query": {
+        "has_parent" : {
+            "parent_type" : "blog",
+            "score" : true,
+            "query" : {
+                "function_score" : {
+                    "script_score": {
+                        "script": "_score * doc['view_count'].value"
+                    }
+                }
+            }
+        }
+    }
+}
+--------------------------------------------------
+// CONSOLE