Mongoid Tire date search using query_string


(khoan) #1

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 -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 -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.


(Shay Banon) #2

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 -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 -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
Sent from the ElasticSearch Users mailing list archive at Nabble.com.


(khoan) #3

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 -d '{"query":{"query_string":{"query":"updated_at: [20110515 TO 20110519]"}}}'

% curl -XPOST 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" : [ ]
}
}


(khoan) #4

The following formats work:

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

But not:

20110515


(Karel Minarik) #5

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 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...
Sent from the ElasticSearch Users mailing list archive at Nabble.com.


(Shay Banon) #6

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
Sent from the ElasticSearch Users mailing list archive at Nabble.com.


(Karel Minarik) #7

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


(system) #8