Elasticsearch深度嵌套字段的查询

huangapple go评论54阅读模式
英文:

Elasticsearch query for deeply nested field

问题

我正在尝试查找两个日期之间的所有记录,但无法找到正确的查询。

映射如下所示:

GET my-books-index-1/_mapping
{
  "my-books-index-1": {
    "mappings": {
      "properties": {
        "book": {
          "properties": {
            "bookInfo": {
              "properties": {
                "publisherInfo": {
                  "type": "nested",
                  "properties": {
                    "publication": {
                      "properties": {
                        "publishedOn": {
                          "type": "date"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

以下是上述映射的示例记录:

"_source": {
  "book": {
    "name": "Harry Potter",
    "bookInfo": {
      "author": "J.K. Rowling",
      "publisherInfo": [
        {
          "price": "100",
          "publication": {
            "publishedOn": 1685268404000 // [Sunday, May 28, 2023 10:06:44 AM]
          }
        }
      ]
    }
  }
}

我正在尝试查找所有在5月25日至5月31日之间发布的书籍。

感谢任何帮助。谢谢。

英文:

I am trying to find all records between two dates, but can't figure out the proper query.

The mapping looks like this

GET my-books-index-1/_mapping
{
  "my-books-index-1": {
    "mappings": {
      "properties": {
        "book": {
          "properties": {
            "bookInfo": {
              "properties": {
                "publisherInfo": {
                  "type": "nested",
                  "properties": {
                    "publication": {
                      "properties": {
                        "publishedOn": {
                          "type": "date"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Following is a sample record for the above mapping

"_source": {
  "book": {
    "name": "Harry Potter",
    "bookInfo": {
      "author": "J.K. Rowling",
      "publisherInfo": [
        {
          "price": "100",
          "publication": {
            "publishedOn": 1685268404000 // [Sunday, May 28, 2023 10:06:44 AM]
          }
        }
      ]
    }
  }
}

[NOTE]: Some additional properties are removed from the mapping sample to keep it short and precise.

I am trying to find all books published between 25th May to 31st May.

Any help is appreciated. Thanks.

答案1

得分: 1

你可以在嵌套路径中使用 range 查询。

PUT test_my-books-index-1
{
    "mappings": {
      "properties": {
        "book": {
          "properties": {
            "bookInfo": {
              "properties": {
                "publisherInfo": {
                  "type": "nested",
                  "properties": {
                    "publication": {
                      "properties": {
                        "publishedOn": {
                          "type": "date"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
}

POST test_my-books-index-1/_bulk?refresh
{"index":{"_id":"1"}}
{"book":{"name":"Harry Potter","bookInfo":{"author":"J.K. Rowling","publisherInfo":[{"price":"100","publication":{"publishedOn":1685268404000}}]}}}

动态日期大于10天前

GET test_my-books-index-1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "book.bookInfo.publisherInfo",
            "query": {
              "range": {
                "book.bookInfo.publisherInfo.publication.publishedOn": {
                  "gte": "now-10d",
                  "lte": "now"
                }
              }
            }
          }
        }
      ]
    }
  }
}

要搜索特定日期

GET test_my-books-index-1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "book.bookInfo.publisherInfo",
            "query": {
              "range": {
                "book.bookInfo.publisherInfo.publication.publishedOn": {
                  "gte": "25/05/2023",
                  "lte": "31/05/2023",
                  "format": "dd/MM/yyyy||yyyy"
                }
              }
            }
          }
        }
      ]
    }
  }
}

另一个示例见此处:https://stackoverflow.com/questions/50524770/elasticsearch-nested-range-query

英文:

You can use range query inside of nested path.

PUT test_my-books-index-1
{
    "mappings": {
      "properties": {
        "book": {
          "properties": {
            "bookInfo": {
              "properties": {
                "publisherInfo": {
                  "type": "nested",
                  "properties": {
                    "publication": {
                      "properties": {
                        "publishedOn": {
                          "type": "date"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
}

POST test_my-books-index-1/_bulk?refresh
{"index":{"_id":"1"}}
{"book":{"name":"Harry Potter","bookInfo":{"author":"J.K. Rowling","publisherInfo":[{"price":"100","publication":{"publishedOn":1685268404000}}]}}}

dynamic date bigger than 10 days ago

GET test_my-books-index-1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "book.bookInfo.publisherInfo",
            "query": {
              "range": {
                "book.bookInfo.publisherInfo.publication.publishedOn": {
                  "gte": "now-10d",
                  "lte": "now"
                }
              }
            }
          }
        }
      ]
    }
  }
}

to search with exact date

GET test_my-books-index-1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "book.bookInfo.publisherInfo",
            "query": {
              "range": {
                "book.bookInfo.publisherInfo.publication.publishedOn": {
                  "gte": "25/05/2023",
                  "lte": "31/05/2023",
                  "format": "dd/MM/yyyy||yyyy"
                }
              }
            }
          }
        }
      ]
    }
  }
}

another example here: https://stackoverflow.com/questions/50524770/elasticsearch-nested-range-query

huangapple
  • 本文由 发表于 2023年6月1日 19:17:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定