英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论