# Mongoid Tire date search using query\_string

**URL:** <https://discuss.elastic.co/t/mongoid-tire-date-search-using-query-string/4421>\
**Category:** Elasticsearch\
**Created:** [May 17, 2011, 9:29am UTC](https://discuss.elastic.co/t/mongoid-tire-date-search-using-query-string/4421 "2011-05-17T09:29:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![khoan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khoan/32/3187_2.png) [@khoan](https://discuss.elastic.co/u/khoan)\
**Post date:** [May 17, 2011, 9:29am UTC](https://discuss.elastic.co/t/mongoid-tire-date-search-using-query-string/4421/1 "2011-05-17T09:29:41Z")

</div>

Hi,

I'm indexing a Mongoid record via Tire gem. I'm having trouble searching for dates. Excerpt below:

% curl -XPOST [http://localhost:9200/events/\_search?pretty=true](http://localhost:9200/events/_search?pretty=true) -d '{"query":{"query\_string":{"query":"started\_at: \*"}}}'

=\> ... hits ...  
{  
"\_index" : "events",  
"\_type" : "event",  
"\_id" : "4dd22cd5f96f36e6b900027a",  
"\_score" : 1.0, "\_source" : {"created\_at":"2011-05-17T08:07:49Z","started\_at":"2011-05-22T16:40:00+00:00","updated\_at":"2011-05-17T08:07:49Z","url":null,"id":"4dd22cd5f96f36e6b900027a"},  
"sort" : [1.0, 1306082400000]  
} ...

But when I try to do a "like" search:

% curl -XPOST [http://localhost:9200/events/\_search?pretty=true](http://localhost:9200/events/_search?pretty=true) -d '{"query":{"query\_string":{"query":"started\_at: 2011\*"}}}'

=\> {  
"took" : 24,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 0,  
"max\_score" : null,  
"hits" : []  
}

Any pointer is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 17, 2011, 10:09pm UTC](https://discuss.elastic.co/t/mongoid-tire-date-search-using-query-string/4421/2 "2011-05-17T22:09:51Z")

</div>

If ti has been detected as a date type, then you need to do a range query against dates, not a wildcard query (internally, they are not stored as text, but as milliseconds since the epoch).  
On Tuesday, May 17, 2011 at 12:29 PM, khoan wrote:

> Hi,
> 
> I'm indexing a Mongoid record via Tire gem. I'm having trouble searching for  
> dates. Excerpt below:
> 
> % curl -XPOST [http://localhost:9200/events/\_search?pretty=true](http://localhost:9200/events/_search?pretty=true) -d  
> '{"query":{"query\_string":{"query":"started\_at: \*"}}}'
> 
> =\> ... hits ...  
> {  
> "\_index" : "events",  
> "\_type" : "event",  
> "\_id" : "4dd22cd5f96f36e6b900027a",  
> "\_score" : 1.0, "\_source" :  
> {"created\_at":"2011-05-17T08:07:49Z","started\_at":"2011-05-22T16:40:00+00:00","updated\_at":"2011-05-17T08:07:49Z","url":null,"id":"4dd22cd5f96f36e6b900027a"},  
> "sort" : [1.0, 1306082400000]  
> } ...
> 
> But when I try to do a "like" search:
> 
> % curl -XPOST [http://localhost:9200/events/\_search?pretty=true](http://localhost:9200/events/_search?pretty=true) -d  
> '{"query":{"query\_string":{"query":"started\_at: 2011\*"}}}'
> 
> =\> {  
> "took" : 24,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 0,  
> "max\_score" : null,  
> "hits" :   
> }
> 
> Any pointer is greatly appreciated.
> 
> --  
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Mongoid-Tire-date-search-using-query-string-tp2951786p2951786.html](http://elasticsearch-users.115913.n3.nabble.com/Mongoid-Tire-date-search-using-query-string-tp2951786p2951786.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![khoan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khoan/32/3187_2.png) [@khoan](https://discuss.elastic.co/u/khoan)\
**Post date:** [May 18, 2011, 12:07am UTC](https://discuss.elastic.co/t/mongoid-tire-date-search-using-query-string/4421/3 "2011-05-18T00:07:10Z")

</div>

Thanks for the reply. I've tried date range query too on updated\_at and started\_at fields.

Is there a way we can "reflect on"/inspect/peek at the internal data type and see what's being done behind the scene? Can we print out the type of a field?

% curl -XPOST [http://localhost:9200/events/\_search?pretty=true](http://localhost:9200/events/_search?pretty=true) -d '{"query":{"query\_string":{"query":"updated\_at: [20110515 TO 20110519]"}}}'

% curl -XPOST [http://localhost:9200/events/\_search?pretty=true](http://localhost:9200/events/_search?pretty=true) -d '{"query":{"query\_string":{"query":"started\_at: [20110521 TO 20110523]"}}}'

=\> {  
"took" : 63,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 0,  
"max\_score" : null,  
"hits" : []  
}  
}

---

<div class="post-metadata">

**Author:** ![khoan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khoan/32/3187_2.png) [@khoan](https://discuss.elastic.co/u/khoan)\
**Post date:** [May 18, 2011, 6:06am UTC](https://discuss.elastic.co/t/mongoid-tire-date-search-using-query-string/4421/4 "2011-05-18T06:06:29Z")

</div>

The following formats work:

2011  
2011-05-15  
2011-05-15T00:00:00  
2011-05-15T00:00:00Z

But not:

20110515

---

<div class="post-metadata">

**Author:** ![karmi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karmi/32/44951_2.png) [@karmi](https://discuss.elastic.co/u/karmi)\
**Post date:** [May 19, 2011, 9:17am UTC](https://discuss.elastic.co/t/mongoid-tire-date-search-using-query-string/4421/5 "2011-05-19T09:17:25Z")

</div>

Hi,

yes, as Shay says, dates behave differently. You still _can_ use \* as  
wildcard, though. Also, double check the mapping for the field in  
question. Full example below:

```
require 'rubygems'
require 'tire'

Tire.index 'events-test-dates' do
  delete
  create :mappings => {
    :event => {
      :properties => {
        :started_at => { :type => 'date' }
      }
    }
  }

  store :event, { :id => 1, :started_at => "2010-01-01" }
  store :event, { :id => 2, :started_at => "2011-01-01" }
  store :event, { :id => 3, :started_at => "2011-01-02" }
  store :event, { :id => 4, :started_at => "2011-02-01" }
  store :event, { :id => 5, :started_at => "2011-03-01" }

  refresh
end

s = Tire.search 'events-test-dates' do
  query { string 'started_at:[2011 TO *]' }
  sort { started_at }
end

puts s.to_curl
puts s.results.map(&:started_at).inspect

```

On May 17, 11:29 am, khoan [huukhoangu...@hotmail.com](mailto:huukhoangu...@hotmail.com) wrote:

> Hi,
> 
> I'm indexing a Mongoid record via Tire gem. I'm having trouble searching for  
> dates. Excerpt below:
> 
> % curl -XPOSThttp://localhost:9200/events/\_search?pretty=true-d  
> '{"query":{"query\_string":{"query":"started\_at: \*"}}}'
> 
> =\> ... hits ...  
> {  
> "\_index" : "events",  
> "\_type" : "event",  
> "\_id" : "4dd22cd5f96f36e6b900027a",  
> "\_score" : 1.0, "\_source" :  
> {"created\_at":"2011-05-17T08:07:49Z","started\_at":"2011-05-22T16:40:00+00:0 0","updated\_at":"2011-05-17T08:07:49Z","url":null,"id":"4dd22cd5f96f36e6b90 0027a"},  
> "sort" : [1.0, 1306082400000]  
> } ...
> 
> But when I try to do a "like" search:
> 
> % curl -XPOSThttp://localhost:9200/events/\_search?pretty=true-d  
> '{"query":{"query\_string":{"query":"started\_at: 2011\*"}}}'
> 
> =\> {  
> "took" : 24,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 0,  
> "max\_score" : null,  
> "hits" :   
> }
> 
> Any pointer is greatly appreciated.
> 
> --  
> View this message in context:[http://elasticsearch-users.115913.n3.nabble.com/Mongoid-Tire-date-sea](http://elasticsearch-users.115913.n3.nabble.com/Mongoid-Tire-date-sea)...  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 20, 2011, 10:00pm UTC](https://discuss.elastic.co/t/mongoid-tire-date-search-using-query-string/4421/6 "2011-05-20T22:00:56Z")

</div>

Thats because the default date format is the ISO one. You can configure several date formats in the mapping for the date type.  
On Wednesday, May 18, 2011 at 9:06 AM, khoan wrote:

> The following formats work:
> 
> 2011  
> 2011-05-15  
> 2011-05-15T00:00:00  
> 2011-05-15T00:00:00Z
> 
> But not:
> 
> 20110515
> 
> --  
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Mongoid-Tire-date-search-using-query-string-tp2951786p2955937.html](http://elasticsearch-users.115913.n3.nabble.com/Mongoid-Tire-date-search-using-query-string-tp2951786p2955937.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![karmi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karmi/32/44951_2.png) [@karmi](https://discuss.elastic.co/u/karmi)\
**Post date:** [May 22, 2011, 8:16am UTC](https://discuss.elastic.co/t/mongoid-tire-date-search-using-query-string/4421/7 "2011-05-22T08:16:38Z")

</div>

> On Wednesday, May 18, 2011 at 9:06 AM, khoan wrote:
> 
> > The following formats work:
> 
> > 2011  
> > 2011-05-15  
> > 2011-05-15T00:00:00  
> > 2011-05-15T00:00:00Z
> 
> > But not:
> 
> > 20110515

On May 21, 12:00 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Thats because the default date format is the ISO one. You can configure several date formats in the mapping for the date type.

Exactly. @khoan, check out documentation at \<http://  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/date-format.html)\>.  
`20110515` would not work in any Lucene-compatible query I am aware  
of.

> On Wednesday, May 18, 2011 at 9:06 AM, khoan wrote:
> 
> > Is there a way we can "reflect on"/inspect/peek at the internal data type

As said, you should double-check you define the proper mapping and  
then you can just retrieve the mapping for the index/type. The  
analyzer will throw errors, when you try to index/store dates in wrong  
format.

Karel

---

<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:** [July 6, 2017, 4:05am UTC](https://discuss.elastic.co/t/mongoid-tire-date-search-using-query-string/4421/8 "2017-07-06T04:05:36Z")

</div>


