gpt4 book ai didi

elasticsearch - Elasticsearch:查询对象中包含的嵌套对象

转载 作者:行者123 更新时间:2023-12-03 01:03:05 24 4
gpt4 key购买 nike

我正在努力构建一个查询,以便可以在文档的子对象上进行嵌套搜索。

说我有以下索引/映射:

curl -XPOST "http://localhost:9200/author/" -d '
{
"mappings": {
"item": {
"properties": {
"books": {
"type": "object",
"properties": {
"data": {
"type": "nested"
}
}
}
}
}
}
}
'

并在索引中包含以下2个文件:
{
"id": 1,
"name": "Robert Louis Stevenson",
"books": {
"count": 2,
"data": [
{
"id": 1,
"label": "Treasure Island"
},
{
"id": 3,
"label": "Dr Jekyll and Mr Hyde"
}
]
}
}


{
"id": 2,
"name": "Philip K. Dick",
"books": {
"count": 1,
"data": [
{
"id": 4,
"label": "Do Android Dream of Electric Sheep"
}
]
}
}

我有一个Book ID数组,比如 [1,4];我将如何编写一个查询,该查询只对作者姓名进行关键字搜索,并且仅当他们写了阵列中的一本书时才返回它们?

我没有设法得到一个不会引起某种查询的查询 parse_exception,但是作为起点,这是我查询的当前迭代-也许很明显我出了错?
{
"query": {
"bool": {
"must": {
"match": {
"label": "Louis"
}
}
},
"nested": {
"path": "books.data",
"query": {
"bool": {
"must": {
"terms": {
"books.data.id": [
1,
4
]
}
}
}
}
}
},
"from": 0,
"size": 8
}

在上述情况下,我希望退回罗伯特·路易斯·史蒂文森先生的文件,因为他的名字包含 Louis,并且他写了书ID 1

对于它的值(value),我得到的当前错误如下所示:
{
"error": {
"root_cause": [
{
"type": "parse_exception",
"reason": "failed to parse search source. expected field name but got [START_OBJECT]"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "author",
"node": "sCk3su4YSnqhvdTGjOztlw",
"reason": {
"type": "parse_exception",
"reason": "failed to parse search source. expected field name but got [START_OBJECT]"
}
}
]
},
"status": 400
}

这让我觉得我的“嵌套”对象全错了,但是文档表明我是对的!

最佳答案

您几乎完全正确,nested查询必须简单地位于bool内部,就像下面的查询一样。另外,还需要在match字段上进行name查询,因​​为这是作者姓名的存储位置:

{
"query": {
"bool": {
"must": [
{
"match": {
"name": "Louis"
}
},
{
"nested": {
"path": "books.data",
"query": {
"bool": {
"must": {
"terms": {
"books.data.id": [
1,
4
]
}
}
}
}
}
}
]
}
},
"from": 0,
"size": 8
}

关于elasticsearch - Elasticsearch:查询对象中包含的嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34662128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com