Using Wildcards in Date type


(Abhishek Kumar) #1

{
"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


(David Pilato) #2

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?


(Abhishek Kumar) #3

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


(David Pilato) #4

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


(Abhishek Kumar) #5

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


(David Pilato) #6

It should be must more easy than that.

Let say you have the following dataset:

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:

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

If he wants by date:

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

If he wants both:

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

(Abhishek Kumar) #7

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 :slight_smile:


(David Pilato) #8

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

Thanks david …for your time and concern :slight_smile:

Happy to help.


(Abhishek Kumar) #9

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.


(David Pilato) #10

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)


(system) #11

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