Elasticsearch Connection Query

Source: Internet
Author: User

In a generic relational database, connection operations are supported.

The cost of connecting to ES in this distributed scenario is very expensive.

However, ES also provides similar operations, support the level of arbitrary expansion, to achieve the effect of the connection.

Other content, refer to Elasticsearch official guide to Organize

Connections in ES

Two types of connections are supported in es: nested queries and Has_child, has_parent parent-child queries

Nested queries:

The document contains nested fields, which save the object as an array, so that each nested sub-object can be searched.

Has_child, has_parent parent query:

Parent-child documents are different types stored in the same index, and parent-child relationships are defined before index data. In parent-child queries, parent-child relationships are referenced by type.

nested queries

Nested types need to be implemented to define good mapping:

{    "type1"  : {        "properties"  : {            "obj1"  : {                "type" " nested "             }        }    }}

Once defined, Type1 has the OBJ1 sub-object and can then query the relevant content through nested queries:

{    "nested" : {        "Path":"obj1",        "Score_mode":"avg",        "Query" : {            "BOOL" : {                "must" : [                    {                        "Match": {"Obj1.name":"Blue"}                    },                    {                        "Range": {"Obj1.count": {"GT":5}}                    }                ]            }        }    }}

Note Several of these parameters:

1 path defines nested objects

2 Score_mode defines how the scores of nested objects are computed and how the current query scores are handled, with Avg,sum,max,min and none. None is not to do any processing, the other to see the literal meaning is good understanding.

3 Query/filter is the way of querying, internal definition of the query for nested objects, note that internal queries must use the full path, that is, for the Obj1 name field of the query, to write Obj1.name.

Nested queries are automatically triggered when a subquery is executed, and the results are returned to the current document's query.

Parent-Child Query

Parent-child relationships also need to be defined before mapping, but unlike general mappings, it is defined as follows:

PUT my_index{"Mappings": {    "my_parent": {},    "My_child": {      "_parent": {        "type":"my_parent"}}}}put my_index/my_parent/1 {  "text":"This is a parent document"}put My_index/my_child/2? parent=1 {  "text":"This was a child document"}put My_index/my_child/3? parent=1 {  "text":"This was another child document"}get My_index/my_parent/_search{"Query": {    "Has_child": {       "type":"My_child",      "Query": {        "Match": {          "text":"Child Document"        }      }    }  }}

This means that the parent type of the My_child type is my_parent, which declares a parent-child relationship. When you then index the data, specify the parent-child corresponding relationship.

Has_child Query

This query examines the subdocument and returns the parent Vendang if the child document satisfies the query criteria.

{    "Has_child" : {        "type":"Blog_tag",        "Query" : {            " Term" : {                "Tag":"something"            }        }    }}

The Score_mode field allows you to specify how the scores returned by the subdocument are handled. Similar to nesting, it also has several ways of Avg,sum,max,min and none.

{    "Has_child" : {        "type":"Blog_tag",        "Score_mode":"sum",        "Query" : {            " Term" : {                "Tag":"something"            }        }    }}

In addition, you can specify the minimum and maximum number of child document matches.

{    "Has_child" : {        "type":"Blog_tag",        "Score_mode":"sum",        "Min_children":2,         "Max_children":Ten,         "Query" : {            " Term" : {                "Tag":"something"            }        }    }}

Has_parent Query

Has_parent query is similar to Has_child, it is to check the parent document that matches, and then return the parent document corresponding to the subdocument.

{    "has_parent" : {        "Parent_type":"Blog",        "Query" : {            " Term" : {                "Tag":"something"            }        }    }}

Reference

1 How to define a parent-child relationship: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html

2 Connection query: https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html

3 Nested query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

4 has_child query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html

5 has_parent query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html

Elasticsearch Connection Query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.