# Using Wildcards in Date type

**URL:** <https://discuss.elastic.co/t/using-wildcards-in-date-type/95504>\
**Category:** Elasticsearch\
**Created:** [August 2, 2017, 10:20am UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504 "2017-08-02T10:20:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhikrbng](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@abhikrbng](https://discuss.elastic.co/u/abhikrbng)\
**Post date:** [August 2, 2017, 10:20am UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/1 "2017-08-02T10:20:44Z")

</div>

{  
"query": {  
"wildcard": {  
" **myStringTypeFieldName**": "\*\*"  
}  
}  
}

The above query doesn't work , if " **myStringTypeFieldName**" is replaced with a " **Date-Type**" Field  
I am getting IllegalArgument exception.  
Because , wildcard works only with string types

Is there any way in Elasticsearch to achieve the same , if the searched field type is **Date**

PS:  
I am trying to implement an application, which executes the the above query with date-field in ES ,  
sometimes with the "date-value" | sometimes with "empty-value"

I will be very pleased if someone can enlighten me on 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:** [August 2, 2017, 10:45am UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/2 "2017-08-02T10:45:42Z")

</div>

First of all, **don't use wildcard query**.

Then, here you seem to be looking for a document which has any value in `myStringTypeFieldName`, right?

May be this can help: [https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-exists-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-exists-query.html)?

---

<div class="post-metadata">

**Author:** ![abhikrbng](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@abhikrbng](https://discuss.elastic.co/u/abhikrbng)\
**Post date:** [August 2, 2017, 10:56am UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/3 "2017-08-02T10:56:47Z")

</div>

yes , the rest api calling the same query...when the date-filed is either empty OR having exact-value

---

<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:** [August 2, 2017, 11:28am UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/4 "2017-08-02T11:28:04Z")

</div>

I don't understand sorry. Can you illustrate what you said with an example?

---

<div class="post-metadata">

**Author:** ![abhikrbng](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@abhikrbng](https://discuss.elastic.co/u/abhikrbng)\
**Post date:** [August 2, 2017, 12:02pm UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/5 "2017-08-02T12:02:34Z")

</div>

Imagine an e-commerce website

You selected 'Mobile' from the category and you get the paginated result

Then on sidebar-panel you have multiple filter-checkbox

Now suppose

> One of the checkbox value is "2017" :  
> On checking it will call a rest-api-url which will query ES to return all the mobiles launched in 2017

> Another checkbox value is "Android" :  
> On clicking which it will call the same rest-api-url, which will query ES to return all the Android mobiles

**CASE I** : you checked "2017"  
: A query in ES is called to get all the mobiles launched in 2017 , here in ES query

> date-field : which is a Date type will be having **exact value** which is 2017 here  
> brand-field : which is a String type is empty

**Case II** : you unchecked the "2017" and checked "Android"  
: **Same query in ES is called** to get all the Android Mobiles **but** here in ES query

> date-field-type : which is a Date type will be " **empty**" , which will match mobiles in every year  
> brand-field : which is a string type with value "Android"

In my scenario , Case II is not working and giving me illegal-argument exception on date-field

---

<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:** [August 2, 2017, 12:10pm UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/6 "2017-08-02T12:10:07Z")

</div>

It should be must more easy than that.

Let say you have the following dataset:

```auto
DELETE test
PUT test
{
  "mappings": {
    "doc": {
      "properties": {
        "brand": {
          "type": "keyword"
        },
        "date": {
          "type": "date"
        }
      }
    }
  }
}
PUT test/doc/1
{
	"brand": "myBrand",
	"date": "2017"
}

```

If the user wants to filter by brand:

```auto
GET test/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "brand": "myBrand"
          }
        }
      ]
    }
  }
}

```

If he wants by date:

```auto
GET test/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "date": {
              "gte": "2017",
              "lt": "2018"
            }
          }
        }
      ]
    }
  }
}

```

If he wants both:

```auto
GET test/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "brand": "myBrand"
          }
        },
        {
          "range": {
            "date": {
              "gte": "2017",
              "lt": "2018"
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![abhikrbng](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@abhikrbng](https://discuss.elastic.co/u/abhikrbng)\
**Post date:** [August 2, 2017, 12:27pm UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/7 "2017-08-02T12:27:04Z")

</div>

Yes , I also tried the same  
Its working, when i am writing separate queries.

But i am trying to dynamically insert values in "brand" and "date" range , So that i need to write only one query for all the cases/filters possible  
Cause i have many filters on my application.

I guess i need to go with separate queries .

Thanks david ...for your time and concern 🙂

---

<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:** [August 2, 2017, 1:00pm UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/8 "2017-08-02T13:00:54Z")

</div>

> [@abhikrbng](#):
>
> I guess i need to go with separate queries .

Definitely. Don't try to mix apples and oranges. That won't work.

> Thanks david …for your time and concern 🙂

Happy to help.

---

<div class="post-metadata">

**Author:** ![abhikrbng](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@abhikrbng](https://discuss.elastic.co/u/abhikrbng)\
**Post date:** [August 2, 2017, 1:11pm UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/9 "2017-08-02T13:11:39Z")

</div>

But its really **strange**!!!

In a document , I have a date field , say "myDateAttributeName" : "2017-07-30T18:30:00.000Z"

when using  
{  
"query": {  
"wildcard": {  
"\_all" " \* 2017 \* "  
}  
}  
}

ES gets me the correct record,  
that means,internally ES looking 2017 in the "myDateAttributeName" field

but when replacing " **\_all**" with "myDateAttributeName"  
Its not working.

---

<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:** [August 2, 2017, 2:37pm UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/10 "2017-08-02T14:37:02Z")

</div>

Not strange. `_all` is a String field.

BTW you should disable it. It's pretty much useless. `copy_to` is a better option.

(It will be deprecated in 6.0)

---

<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:** [August 30, 2017, 2:37pm UTC](https://discuss.elastic.co/t/using-wildcards-in-date-type/95504/11 "2017-08-30T14:37:04Z")

</div>

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