# One field free text query on Elasticsearch supporting dates

**URL:** https://discuss.elastic.co/t/one-field-free-text-query-on-elasticsearch-supporting-dates/222946
**Category:** Elasticsearch
**Created:** [March 10, 2020, 1:50pm UTC](https://discuss.elastic.co/t/one-field-free-text-query-on-elasticsearch-supporting-dates/222946 "2020-03-10T13:50:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![styks90](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/styks90/32/59205_2.png) [@styks90](https://discuss.elastic.co/u/styks90)
#### Post date: [March 10, 2020, 1:50pm UTC](https://discuss.elastic.co/t/one-field-free-text-query-on-elasticsearch-supporting-dates/222946/1 "2020-03-10T13:50:22Z")

</div>

I originally asked this question on [StackOverflow](https://stackoverflow.com/questions/60604890/one-field-free-text-query-on-elasticsearch-supporting-dates), but haven't found a satisfactory answer yet:

Suppose I have an index with multiple fields of type `text` and one or more of type `date` (of some format, say `yyyy/MM/dd` ). If I want to enable users to perform queries using only a single input field (so, Google-like), how should I go about supporting the `date` fields? The `text` fields, so far, are mainly queried using the `simple_query_string` and `multi_match` queries.

Now, the `simple_query_string` works fine as long as an entire `date` in the correct format is searched. But if, for instance, I want to filter by year, the missing fields are automatically filled with the smallest accepted value (so e.g. for months it's `01`).

Here's the approaches I've tried so far:

- split the components of a `date` field into year, month etc. This however, as far as I can make out, requires an `update_by_query` , while having this rule at index time would be much nicer...
- use `range` query. The available formatting however does not support the level of freedom the user input should have (so something like `2000 <some-name>` would not be accepted...
- copy the value as date to a different field and accept more formats -\> doesn't work like that...

What my next approaches could be:

- define an analyzer that splits dates by available delimiters and index everything as text and query as text. This should probably work, but elegant it is not...
- change the logstash pipeline to write the date components to different fields. However, it feels like this should be possible to do in Elasticsearch itself...

Anyone had to solve this specific problem before? Thanks for suggestions!

* * *

Update:

What works is doing the `update_by_query` to extract the desired fields, like so

```
POST date_test/_update_by_query 
{
  "script": {
    "source": """
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
      LocalDate ldt = LocalDate.parse(ctx._source.GEBURTSDATUM, formatter);
      ctx._source.GEBURTSJAHR = ldt.getYear();
      ctx._source.GEBURTSMONAT = ldt.getMonthValue();
      ctx._source.GEBURTSTAG = ldt.getDayOfMonth();
    """
  }
}

```

Could this be translated into an index mapping instead of using the `update` api?

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [April 7, 2020, 1:50pm UTC](https://discuss.elastic.co/t/one-field-free-text-query-on-elasticsearch-supporting-dates/222946/2 "2020-04-07T13:50:30Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
