如何通过主文档查询返回嵌套文档及其一些文件?

时间:2015-01-07 11:44:16

标签: elasticsearch nested

我在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!"   

请告诉我如何实施它。

2 个答案:

答案 0 :(得分:0)

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

此处使用_source

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}]}
    } ]
  }
}

答案 1 :(得分:0)

最后,问题已由 dynamic templates 解决。所以新的索引结构是这样的:

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"
                  }
            }}
        ]
}}}

查询:

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

结果如下:

{
"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
           }
       }
    ]
 }
}