【问题标题】:Elastic Search Extracting Inner Elements弹性搜索提取内部元素
【发布时间】:2015-08-01 10:23:52
【问题描述】:

我在 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]

【问题讨论】:

    标签: json elasticsearch nested


    【解决方案1】:

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

    【讨论】:

    • 谢谢!它以这种方式对我有用,现在我只需要解析输出即可获取最后一个源参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2017-01-26
    相关资源
    最近更新 更多