elasticsearch 如何通过对主文档的查询返回嵌套文档及其部分文件?,elasticsearch,nested,elasticsearch,Nested" /> elasticsearch 如何通过对主文档的查询返回嵌套文档及其部分文件?,elasticsearch,nested,elasticsearch,Nested" />

elasticsearch 如何通过对主文档的查询返回嵌套文档及其部分文件?

elasticsearch 如何通过对主文档的查询返回嵌套文档及其部分文件?,elasticsearch,nested,elasticsearch,Nested,我有以下关于elasticsearch的索引: PUT /blog { "mappings": { "threadQ":{ "properties": { "title" : { "type" : "string", "analyzer" : "standard" }, "body" : {

我有以下关于elasticsearch的索引:

PUT /blog
{
"mappings": {
    "threadQ":{
        "properties": { 
            "title" : {
                   "type" : "string",
                   "analyzer" : "standard"
            },
            "body" : {
                   "type" : "string",
                   "analyzer" : "standard"
            },
            "posts":{
                "type": "nested",
                "properties": {
                    "comment": {
                        "type": "string",
                        "analyzer": "standard"
                    },
                    "prototype": {
                        "type": "string",
                        "analyzer": "standard"
                    },
                    "customScore":{
                        "type": "long"
                    }
                }
            }
        }
    }
}
}
我添加了一个文档:

PUT /blog/threadQ/1
{
"title": "What is c#?",
"body": "C# is a good programming language, makes it easy to develop!",
"posts": [{
    "comment": "YEP!",
    "prototype": "Hossein Bakhtiari",
    "customScore": 2
},
{
    "comment": "NEVER EVER :O",
    "prototype": "Garpizio En Larri",
    "customScore": 3
}]
}
因此,以下查询有效:

POST /blog/threadQ/_search
{
"query": {
    "bool": {
        "must": [{
            "nested": {
                "query": {
                    "query_string": {
                        "fields": ["posts.comment"],
                        "query": "YEP"
                    }
                },
                "path": "posts"
            }
        }]
    }
}
}

结果就是文档。 现在要进行如下查询:

SELECT threadQ.posts.customScore FROM threadQ WHERE threadQ.posts.comment = "YEP!"   
PUT /my_index
{
"mappings": {
    "my_type": {
        "properties": {
            "Id":{
                "type": "integer",
                "analyzer": "standard"
            },
            "name":{
                "type": "string",
                "analyzer": "english"
            }
        }, 
        "dynamic_templates": [
            { "en": {
                  "match":              "*", 
                  "match_mapping_type": "string",
                  "mapping": {
                      "type":           "string",
                      "analyzer":       "english"
                  }
            }}
        ]
}}}
{
"took": 3,
"timed_out": false,
"_shards": {
   "total": 5,
   "successful": 5,
   "failed": 0
},
"hits": {
   "total": 3,
   "max_score": 14,
   "hits": [
      {
         "_index": "my_index",
         "_type": "my_type",
         "_id": "2",
         "_score": 14,
         "_source": {
            "Id": 2,
            "name": "Second One",
            "iphone": 20,
            "apple": 14
         }
      },
      {
         "_index": "my_index",
         "_type": "my_type",
         "_id": "3",
         "_score": 14,
         "_source": {
            "Id": 3,
            "name": "Third One",
            "apple": 14
         }
      },
      {
         "_index": "my_index",
         "_type": "my_type",
         "_id": "1",
         "_score": 1,
         "_source": {
            "Id": 1,
            "name": "First One",
            "iphone": 2,
            "apple": 1
           }
       }
    ]
 }
}

请告诉我如何实现它。

要返回文档中的特定字段,请使用或参数

这里使用了
\u源代码

curl -XGET http://localhost:9200/blog/threadQ/_search -d '
{
"_source" : "posts.customScore",
"query": {
    "bool": {
        "must": [{
            "nested": {
                "query": {
                    "query_string": {
                        "fields": ["posts.comment"],
                        "query": "YEP"
                    }
                },
                "path": "posts"
            }
        }]
    }
  }
}'
它将返回:

"hits" : {
    "total" : 1,
    "max_score" : 2.252763,
    "hits" : [ {
      "_index" : "myindex",
      "_type" : "threadQ",
      "_id" : "1",
      "_score" : 2.252763,
      "_source":{"posts":[{"customScore":2},{"customScore":3}]}
    } ]
  }
}

最后,这个问题已经被解决了。因此,新的索引结构如下所示:

SELECT threadQ.posts.customScore FROM threadQ WHERE threadQ.posts.comment = "YEP!"   
PUT /my_index
{
"mappings": {
    "my_type": {
        "properties": {
            "Id":{
                "type": "integer",
                "analyzer": "standard"
            },
            "name":{
                "type": "string",
                "analyzer": "english"
            }
        }, 
        "dynamic_templates": [
            { "en": {
                  "match":              "*", 
                  "match_mapping_type": "string",
                  "mapping": {
                      "type":           "string",
                      "analyzer":       "english"
                  }
            }}
        ]
}}}
{
"took": 3,
"timed_out": false,
"_shards": {
   "total": 5,
   "successful": 5,
   "failed": 0
},
"hits": {
   "total": 3,
   "max_score": 14,
   "hits": [
      {
         "_index": "my_index",
         "_type": "my_type",
         "_id": "2",
         "_score": 14,
         "_source": {
            "Id": 2,
            "name": "Second One",
            "iphone": 20,
            "apple": 14
         }
      },
      {
         "_index": "my_index",
         "_type": "my_type",
         "_id": "3",
         "_score": 14,
         "_source": {
            "Id": 3,
            "name": "Third One",
            "apple": 14
         }
      },
      {
         "_index": "my_index",
         "_type": "my_type",
         "_id": "1",
         "_score": 1,
         "_source": {
            "Id": 1,
            "name": "First One",
            "iphone": 2,
            "apple": 1
           }
       }
    ]
 }
}
以及查询:

POST /my_index/my_type/_search
{
 "query": {
"function_score": {
  "query": {"match_all": {}},
  "functions": [
    {
      "script_score": {
        "script": "doc.apple.value * _score"
      }
    }
  ]
}
}
}
结果如下所示:

SELECT threadQ.posts.customScore FROM threadQ WHERE threadQ.posts.comment = "YEP!"   
PUT /my_index
{
"mappings": {
    "my_type": {
        "properties": {
            "Id":{
                "type": "integer",
                "analyzer": "standard"
            },
            "name":{
                "type": "string",
                "analyzer": "english"
            }
        }, 
        "dynamic_templates": [
            { "en": {
                  "match":              "*", 
                  "match_mapping_type": "string",
                  "mapping": {
                      "type":           "string",
                      "analyzer":       "english"
                  }
            }}
        ]
}}}
{
"took": 3,
"timed_out": false,
"_shards": {
   "total": 5,
   "successful": 5,
   "failed": 0
},
"hits": {
   "total": 3,
   "max_score": 14,
   "hits": [
      {
         "_index": "my_index",
         "_type": "my_type",
         "_id": "2",
         "_score": 14,
         "_source": {
            "Id": 2,
            "name": "Second One",
            "iphone": 20,
            "apple": 14
         }
      },
      {
         "_index": "my_index",
         "_type": "my_type",
         "_id": "3",
         "_score": 14,
         "_source": {
            "Id": 3,
            "name": "Third One",
            "apple": 14
         }
      },
      {
         "_index": "my_index",
         "_type": "my_type",
         "_id": "1",
         "_score": 1,
         "_source": {
            "Id": 1,
            "name": "First One",
            "iphone": 2,
            "apple": 1
           }
       }
    ]
 }
}

是您需要的唯一更改,即您希望返回特定字段而不是整个文档吗?还是我遗漏了什么?谢谢Olly,但我只需要检索{“customScore”:2},因为我想根据这个值应用一些函数\分数脚本来对上层结果集进行排序。我认为嵌套文档不可能做到这一点,相反,您需要重新组织数据以使用父/子文档-因此每个
post
都是
blog