Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

How to specific day of month search in Elasticsearch

what i am doing now is get all data and filter the "day of Month" like 2020-01-05, 2020-02-05 in pandas,

how can i search specific Day of Month in Elasticsearch?

my doc is something like below

{
    "took": 80,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1508,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "test_data",
                "_type": "test",
                "_id": "SlR42nYBlaUrIoJsVOv6",
                "_score": null,
                "_source": {
                    "timestamp": 1609983082524,
                    "timestampstring": "2021-01-07 09:31:22.524",
                    "oneNetDevieId": "624232444",
                    "type": "data"
                },
                "sort": [
                    2609983082524
                ]
            },
            {
                "_index": "test_data",
                "_type": "test",
                "_id": "6FRL2nYBlaUrIoJsAuba",
                "_score": null,
                "_source": {

...

I have try something like

{
  "query": {
    "filtered": {
      "filter": {
        "script": {
          "script": "doc.timestampstring.date.dayOfMonth == 5"
        }
      }
    }
  }
}

or

{
      "query": {
        "filtered": {
          "filter": {
            "script": {
              "script": "doc._source.timestampstring.date.dayOfMonth == 5"
            }
          }
        }
      }
    }

and it shows error "type": "parsing_exception", "reason": "unknown query [filtered]", "line": 5, "col": 17

or do i need to mapping?thanks

Edited update:

i got the new error:
  "reason": {
                    "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "doc['timestamp'].value.getDayOfMonth() == 5",
                        "                      ^---- HERE"
                    ],
                    "script": "doc['timestamp'].value.getDayOfMonth() == 5",
                    "lang": "painless",
                    "position": {
                        "offset": 22,
                        "start": 0,
                        "end": 43
                    },
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "dynamic method [java.lang.Long, getDayOfMonth/0] not found"
                    }
                }
            }
        ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As pointed out by @Val, filtered is replaced by bool query. You need to modify your query as shown below

Adding a working example with index mapping, data, search query and search result

Index Mapping:

{
  "mappings": {
    "properties": {
      "timestampstring": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss.SSS"
      }
    }
  }
}

Index Data:

{
   "timestampstring": "2020-11-06 15:22:58.537"
}
{
   "timestampstring": "2020-11-05 21:21:51.655"
}

Search Query:

{
  "query": {
    "bool" : {
      "filter" : {
       "script" : {
          "script" : {
            "source": "doc['timestampstring'].value.getDayOfMonth() == 5",
            "lang": "painless"
          }
        }
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "65607193",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.0,
        "_source": {
          "timestampstring": "2020-11-05 21:21:51.655"
        }
      }
    ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...