0

I am trying to fetch the following section of a document i have indexed through elasticsearch:

temporal: {
begin: "2016-11-30T00:00:00",
end: "2016-12-08T13:55:02"
},

The query that i am using on CURL as i am currently just testing the queries on localhost is the following one :

curl -XGET 'localhost:9201/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query": {
        "range" : {
            "timestamp" : {
                "gte": "2015-01-01 00:00:00", 
                "lte": "now"
            }
        }
    }
}
'

mapping

temporal: {
properties: {
begin: {
type: "date"
},
end: {
type: "date"
}
}
}

but the above mentioned query does not returns any successful hits although it should return at least the document mentioned above.

Rehan
  • 371
  • 1
  • 6
  • 29
  • You don't have any `timestamp` field in your document at the top. – Val May 03 '17 at 11:55
  • @val i tried by changing the timestamp field in query to temporal but it still did not generate any result – Rehan May 03 '17 at 11:57
  • 1
    You need to change it to either `temporal.begin` or `temporal.end` depending on which data field you want to check. – Val May 03 '17 at 11:58
  • @Val so i want to check temporal.begin and i edited the query accordingly but the results are still not coming – Rehan May 03 '17 at 12:02
  • Can you show your mapping? `curl -XGET localhost:9200/your_index` ? – Val May 03 '17 at 12:04
  • @Val i added the mapping in question description and i am currently working on 9201 port – Rehan May 03 '17 at 12:07
  • In your query the date pattern is wrong it seems, it should be `2015-01-01T00:00:00` (you're missing the `T`) – Val May 03 '17 at 12:13
  • @Val i've tried that..it's not the issue as query automatically rectifies the format according to documentation – Rehan May 03 '17 at 12:19
  • Works well with my local 5.3 server. What ES version do you have? – Val May 03 '17 at 12:23
  • I know it seems the same but give a shoot to "query_string" with lucene range syntax https://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Range%20Searches – jordivador May 03 '17 at 12:27
  • it started working. i just had to give the complete path of the object within the document. – Rehan May 03 '17 at 12:32

1 Answers1

0

Assuming you want to search as you posted ( Note that I've formatted datetime input) , which is timestamp - mapping would be.

{
  "temporal" : {
    "mappings" : {
      "doc" : {
        "properties" : {
          "temporal" : {
            "properties" : {
              "begin" : {
                "type" : "date",
                "format" : "strict_date_optional_time||epoch_millis"
              },
              "end" : {
                "type" : "date",
                "format" : "strict_date_optional_time||epoch_millis"
              }
            }
          }
        }
      }
    }
  }
}

Indexing / Query document :

curl -XPOST  localhost:9200/temporal/doc/1 -d '
{"temporal": {
"begin": "2016-11-30T00:00:00",
"end": "2016-12-08T13:55:02"
}}';


curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
    {
        "query": {
            "range" : {
                "temporal.begin" : {
                    "gte": "2015-01-01T00:00:00", 
                    "lte": "now"
                }
            }
        }
    }'
    {
      "took" : 6,
      "timed_out" : false,
      "_shards" : {
        "total" : 108,
        "successful" : 108,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [ {
          "_index" : "temporal",
          "_type" : "doc",
          "_id" : "1",
          "_score" : 1.0,
          "_source" : {
            "temporal" : {
              "begin" : "2016-11-30T00:00:00",
              "end" : "2016-12-08T13:55:02"
            }
          }
        } ]
      }
    }
Nirmal
  • 1,276
  • 8
  • 16