Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching Date Field value in ElasticSearch without time

I have one date field in my data as

"type": "date",
"format": "dateOptionalTime"

Now My date Field and Value is -

"INITIAL_EXTRACT_DATE" : "2015-04-02T06:47:57.78+05:30"

While searching I am searching based on only date that is "2015-04-02". but I am getting 0 result.

Can anyone suggest how to search exact date and is any of date.

Now I am trying with this -

For Exact Date -

"term": {
          "IH_PT_DSC": {
             "value": "2015-04-02"
          }
       }

For Is any of date -

"terms": {
         "IH_PT_DSC": [
            "2015-04-02",
            "2015-04-03",
            "2015-04-03"
         ]
      }
like image 236
Mukesh Avatar asked Aug 11 '15 12:08

Mukesh


People also ask

How do you search a specific field in elastic search?

There are two recommended methods to retrieve selected fields from a search query: Use the fields option to extract the values of fields present in the index mapping. Use the _source option if you need to access the original data that was passed at index time.

What is default date format in Elasticsearch?

SSSZ or yyyy-MM-dd .

What is GTE in Elasticsearch?

gte - Greater-than or equal to. lte - Less-than or equal to. gt - Greater-than. lt - Less-than. https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html.

What is term query in Elasticsearch?

Term queryedit. Returns documents that contain an exact term in a provided field. You can use the term query to find documents based on a precise value such as a price, a product ID, or a username.


2 Answers

You can use a range filter for this, by using the same date in gte / lte and a format parameter where you only specify the date part (i.e. leave out the time part)

{
    "constant_score": {
        "filter": {
            "range" : {
                "IH_PT_DSC" : {
                    "gte": "2015-04-02",
                    "lte": "2015-04-02",
                    "format": "yyyy-MM-dd"
                }
            }
        }
    }
}

If you need to specify multiple dates, you can do so easily as well.

{
    "constant_score": {
        "filter": {
            "range" : {
                "IH_PT_DSC" : {
                    "gte": "2015-04-01",
                    "lte": "2015-04-03",
                    "format": "yyyy-MM-dd"
                }
            }
        }
    }
}

Finally, if you need to query disjoint date intervals, simply use a bool/should filter:

{
  "constant_score": {
    "filter": {
      "bool": {
        "should": [
          {
            "range": {                    <--- interval 1
              "IH_PT_DSC": {
                "gte": "2015-04-01",
                "lte": "2015-04-03",
                "format": "yyyy-MM-dd"
              }
            }
          },
          {
            "range": {                    <--- interval 2
              "IH_PT_DSC": {
                "gte": "2015-04-05",
                "lte": "2015-04-08",
                "format": "yyyy-MM-dd"
              }
            }
          },
          {
            "range": {                    <--- interval 3
              "IH_PT_DSC": {
                "gte": "2015-04-10",
                "lte": "2015-04-12",
                "format": "yyyy-MM-dd"
              }
            }
          }
        ]
      }
    }
  }
}
like image 78
Val Avatar answered Dec 23 '22 07:12

Val


You should indeed use a date range query, and a format parameter (but much simpler as explained in the previous answer)

For Exact Date

{
 "range": {
  "IH_PT_DSC": {
   "value": {
    "gte": "2015-04-02||/d",
    "lte": "2015-04-02||/d"
   }
  }
 }
}

For Is any of date - use a boolean filter and "should" the different blocks together

{
 "bool": {
  "should": [{
    "range": {
     "IH_PT_DSC": {
      "value": {
       "gte": "2015-04-02||/d",
       "lte": "2015-04-02||/d"
      }
     }
    }
   }, {
    "range": {
     "IH_PT_DSC": {
      "value": {
       "gte": "2015-06-07||/d",
       "lte": "2015-06-07||/d"
      }
     }
    }
   }
  ]
 }
}
like image 34
Roeland Van Heddegem Avatar answered Dec 23 '22 07:12

Roeland Van Heddegem