# Date math in a query string is affected by analyzer

**URL:** <https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366>\
**Category:** Elasticsearch\
**Created:** [February 21, 2019, 9:57am UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366 "2019-02-21T09:57:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yraber](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yraber](https://discuss.elastic.co/u/yraber)\
**Post date:** [February 21, 2019, 9:57am UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366/1 "2019-02-21T09:57:46Z")

</div>

It seems that a query\_string with date math gets modified by the analyzer, and despite having read all the doc and searched everywhere, I can't find a way around it.

Here is a very basic example :

```
PUT twitter/_doc/1
{
    "user" : "kimchy",
    "post_date" : "2019-02-01T00:00:00",
    "message" : "trying out Elasticsearch"
}

```

Then

```
GET twitter/_search
{
   "query" : {
	    "query_string" : {
	       "analyzer" : "standard",
	       "query" : "post_date:[now-12M TO *]"
	    }   	
   }
}

```

Gives no results, my tests seems to indicate that the query is converted from "now-12M" (months) to "now-12m" (minutes).

Is there a way around it ?

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 21, 2019, 10:10am UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366/2 "2019-02-21T10:10:25Z")

</div>

Which version are you using?

I don't know if this has ever worked...

The only way I found for now is to use:

```auto
GET twitter/_search
{
   "query" : {
	    "query_string" : {
	       "query" : "post_date:[now-12M TO *]"
	    }   	
   }
}

```

---

<div class="post-metadata">

**Author:** ![yraber](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yraber](https://discuss.elastic.co/u/yraber)\
**Post date:** [February 21, 2019, 1:15pm UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366/3 "2019-02-21T13:15:08Z")

</div>

I'm using version 6.6.1 on CentOS7.

I really need the analyzer, so this is not really an option for me. The workaround that I have is to calculate the date in my app and concatenate it in the query, as the date string is not affected.

But I was wondering if this is a normal behavior.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 21, 2019, 1:36pm UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366/4 "2019-02-21T13:36:05Z")

</div>

Why do you need to define the analyzer?  
Don't your text fields already define it?

---

<div class="post-metadata">

**Author:** ![yraber](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yraber](https://discuss.elastic.co/u/yraber)\
**Post date:** [February 21, 2019, 2:08pm UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366/5 "2019-02-21T14:08:48Z")

</div>

The query string comes from a user exposed input field, and it's used for multi-field search.

I defined a custom analyzer for it to behave like I wanted :

```
    meta_search => {
      type => 'custom',
      tokenizer => 'whitespace',          
      filter => [qw/asciifolding lowercase elision/],          
      char_filter => [qw/strip_article/],
    },

```

Is there a better/other way to do this ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 21, 2019, 2:21pm UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366/6 "2019-02-21T14:21:40Z")

</div>

Are you applying this analyzer to text fields in the mapping?

---

<div class="post-metadata">

**Author:** ![yraber](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yraber](https://discuss.elastic.co/u/yraber)\
**Post date:** [February 22, 2019, 7:54am UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366/7 "2019-02-22T07:54:37Z")

</div>

No I only apply this to the search query. I have defined some custom `analyzers` on my fields, but no `search_analyzer`.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 22, 2019, 8:51am UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366/8 "2019-02-22T08:51:12Z")

</div>

Do that then and that should solve your problem IMO.

I'm not saying it's not a bug but this is at least a workaround.  
Could you also try the `simple_query_string` query?

---

<div class="post-metadata">

**Author:** ![yraber](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yraber](https://discuss.elastic.co/u/yraber)\
**Post date:** [February 22, 2019, 9:19am UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366/9 "2019-02-22T09:19:58Z")

</div>

I'll try that and see what works best, and I'll post my findings if it can be of any interest for someone with a similar problem.

Thank you for your help.

---

<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:** [March 22, 2019, 9:20am UTC](https://discuss.elastic.co/t/date-math-in-a-query-string-is-affected-by-analyzer/169366/10 "2019-03-22T09:20:04Z")

</div>

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