弹性搜索提取内部元素

时间:2015-05-20 09:08:01

标签: json elasticsearch nested

我正在使用Scala和REST进行弹性搜索,并具有以下数据结构:(作为JSON输入文件)

{
    "bookTitle" : "textbook",
    "bookAuthors" : [
        {
            "authorId" : "01",
            "authorName" : "author1"
        },
        {
            "authorId" : "02",
            "authorName" : "author2"
        },
    ]
}

此集合使用的数据映射:

{
    "properties" : {
        "book": {
            "properties": {
                "bookTitle": {
                    "type": "string"
                }, 
                "bookAuthors": {
                    "type": "nested",
                    "properties": {
                        "authorId ": {
                            "type":"string"
                        },
                        "authorName" : {
                            "type": "string"
                        } 
                    }
                }
            }
        }
    }
}

我希望能够通过作者ID进行查询,并且只能获得匹配的单个作者。到目前为止,我已经设法通过authorId进行查询,但是我继续获取整个书籍文档,同时显示两位作者;我还尝试仅选择要显示的bookAuthors特定的字段,但结果是相同的。

现状: 获取authorId为01 =>的作者姓名返回[author1,author2]

必填查询: 获取authorId为01 =>的作者姓名return [author1]

1 个答案:

答案 0 :(得分:3)

在elasticsearch 1.5.2中,您可以使用inner hits

实现此目的

例如:

put mybooks
{
   "mappings": {
      "book": {
         "properties": {
            "bookTitle": {
               "type": "string"
            },
            "bookAuthors": {
               "type": "nested",
               "properties": {
                  "authorId ": {
                     "type": "string"
                  },
                  "authorName": {
                     "type": "string"
                  }
               }
            }
         }
      }
   }
}

2)索引文件

put mybooks/book/1
{
   "bookTitle": "book1",
   "bookAuthors": [
      {
         "authorId": "01",
         "authorName": "author1"
      },
      {
         "authorId": "02",
         "authorName": "author2"
      }
   ]
}

put mybooks/book/2
{
    "bookTitle" : "book2",
    "bookAuthors" : [
        {
            "authorId" : "03",
            "authorName" : "author1"
        },
        {
            "authorId" : "02",
            "authorName" : "author2"
        }
    ]
}

3)查询

post mybooks/_search
{
   "_source": [
      "bookTitle"
   ],
   "query": {
      "nested": {
         "path": "bookAuthors",
         "query": {
            "match": {
               "bookAuthors.authorId": "02"
            }
         },
         "inner_hits": {
             "_source" :["authorName"]
         }
      }
   }
}

4)结果

"hits": [
         {
            "_index": "mybooks",
            "_type": "book",
            "_id": "1",
            "_score": 1.4054651,
            "_source": {
               "bookTitle": "book1"
            },
            "inner_hits": {
               "bookAuthors": {
                  "hits": {
                     "total": 1,
                     "max_score": 1.4054651,
                     "hits": [
                        {
                           "_index": "mybooks",
                           "_type": "book",
                           "_id": "1",
                           "_nested": {
                              "field": "bookAuthors",
                              "offset": 1
                           },
                           "_score": 1.4054651,
                           "_source": {
                              "authorName": "author2"
                           }
                        }
                     ]
                  }
               }
            }
         },
         {
            "_index": "mybooks",
            "_type": "book",
            "_id": "2",
            "_score": 1.4054651,
            "_source": {
               "bookTitle": "book2"
            },
            "inner_hits": {
               "bookAuthors": {
                  "hits": {
                     "total": 1,
                     "max_score": 1.4054651,
                     "hits": [
                        {
                           "_index": "mybooks",
                           "_type": "book",
                           "_id": "2",
                           "_nested": {
                              "field": "bookAuthors",
                              "offset": 1
                           },
                           "_score": 1.4054651,
                           "_source": {
                              "authorName": "author2"
                           }
                        }
                     ]
                  }
               }
            }
         }
      ]