1. 程式人生 > >Elasticsearch 學習之Search API inner hits

Elasticsearch 學習之Search API inner hits

inner hits:

    curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
    {
    "mappings": {
        "_doc": {
        "properties": {
            "comments": {
            "type": "nested"
            }
        }
        }
    }
    }
    '
    curl -X PUT "localhost:9200/test/_doc/1?refresh" -H 'Content-Type: application/json' -d'
    {
    "title": "Test title",
    "comments": [
        {
        "author": "kimchy",
        "number": 1
        },
        {
        "author": "nik9000",
        "number": 2
        }
    ]
    }
    '
  • inner hit 查詢匹配在nested中描述。

    curl -X POST "localhost:9200/test/_search" -H 'Content-Type: application/json' -d'
    {
    "query": {
        "nested": {
        "path": "comments",
        "query": {
            "match": {"comments.number" : 2}
        },
        "inner_hits": {} 
        }
    }
    }
    '
    
  • Nested inner hits and _source:在nested欄位裡面查詢_source可能消耗很多時間,特別是size大於預設值的狀況下,可以通過設定_source:false,查詢fields欄位的值減少這種消耗

    curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
    {
    "mappings": {
     "_doc": {
        "properties": {
            "comments": {
      "type": "nested"
            }
        }
        }
    }
    }
    '
    curl -X PUT "localhost:9200/test/_doc/1?refresh" -H 'Content-Type: application/json' -d'
    {
    "title": "Test title",
    "comments": [
        {
        "author": "kimchy",
        "text": "comment text"
        },
        {
        "author": "nik9000",
        "text": "words words words"
        }
    ]
    }
    '
    curl -X POST "localhost:9200/test/_search" -H 'Content-Type: application/json' -d'
    {
    "query": {
        "nested": {
        "path": "comments",
        "query": {
            "match": {"comments.text" : "words"}
        },
        "inner_hits": {
            "_source" : false,
            "docvalue_fields" : ["comments.text.keyword"]
        }
        }
    }
    }
    '
    
  • Hierarchical levels of nested object fields and inner hits.(巢狀裡面包含巢狀的狀況)

    curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
    {
    "mappings": {
        "_doc": {
        "properties": {
            "comments": {
            "type": "nested",
            "properties": {
                "votes": {
                "type": "nested"
                }
            }
            }
        }
        }
    }
    }
    '
    curl -X PUT "localhost:9200/test/_doc/1?refresh" -H 'Content-Type: application/json' -d'
    {
    "title": "Test title",
    "comments": [
        {
        "author": "kimchy",
        "text": "comment text",
        "votes": []
        },
        {
        "author": "nik9000",
        "text": "words words words",
        "votes": [
            {"value": 1 , "voter": "kimchy"},
            {"value": -1, "voter": "other"}
        ]
        }
    ]
    }
    '
    curl -X POST "localhost:9200/test/_search" -H 'Content-Type: application/json' -d'
    {
    "query": {
        "nested": {
        "path": "comments.votes",
            "query": {
            "match": {
                "comments.votes.voter": "kimchy"
            }
            },
            "inner_hits" : {
                "_source":false,
                "docvalue_field":["comments.votes.voter.keyword"]}
        }
    }
    }
    '
    
  • Parent/child inner hits(通過路由機制實現父子關係)

    curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
    {
    "mappings": {
        "_doc": {
        "properties": {
            "my_join_field": {
            "type": "join",
            "relations": {
                "my_parent": "my_child"
            }
            }
        }
        }
    }
    }
    '
    curl -X PUT "localhost:9200/test/_doc/1?refresh" -H 'Content-Type: application/json' -d'
    {
    "number": 1,
    "my_join_field": "my_parent"
    }
    '
    curl -X PUT "localhost:9200/test/_doc/2?routing=1&refresh" -H 'Content-Type: application/json' -d'
    {
    "number": 1,
    "my_join_field": {
        "name": "my_child",
        "parent": "1"
    }
    }
    '
    curl -X POST "localhost:9200/test/_search" -H 'Content-Type: application/json' -d'
    {
    "query": {
        "has_child": {
        "type": "my_child",
        "query": {
            "match": {
            "number": 1
            }
        },
        "inner_hits": {}    
        }
    }
    }
    '