Numeric Range Query

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Numeric Range Query

rabbit_fr
Hi,

I'd like to do combined range query on document like

document {
   start: 10,
   stop: 20
}

Querying using '{ query : { range : {start: { from :null, to:
15 } } }' works fine,
but how to do something like :

'{ query : { range : {start: { from :null, to: 15 } }, stop: { from:
15, to: null} }'

I tried to combine them using boolean query without success.

Any clue ?

Thank you
Reply | Threaded
Open this post in threaded view
|

Re: Numeric Range Query

kimchy
Administrator
Your thoughts are correct, you should use a boolean query with (I am guessing) two must clauses which will return an AND basically of the two clauses, here is an example:

{
    bool : {
        must : {
            range : { ... }
        },
        must: {
            range : { ... }
        }
    }
}

-shay.banon

On Thu, Feb 11, 2010 at 4:54 PM, Rabbit <[hidden email]> wrote:
Hi,

I'd like to do combined range query on document like

document {
  start: 10,
  stop: 20
}

Querying using '{ query : { range : {start: { from :null, to:
15 } } }' works fine,
but how to do something like :

'{ query : { range : {start: { from :null, to: 15 } }, stop: { from:
15, to: null} }'

I tried to combine them using boolean query without success.

Any clue ?

Thank you

Reply | Threaded
Open this post in threaded view
|

Re: Numeric Range Query

rabbit_fr
 curl -X GET "http://localhost:9200/ip/_search?pretty=true" -d
\                                                         (~/workspace/
RestServer/dat)
'{
    bool : {
        must : {
            range : { start: { from :null, to: 15 } }
        },
        must: {
            range : { stop: { from :15, to: null } }
        }
    }
}'
{
  "_shards" : {
    "total" : 5,
    "successful" : 0,
    "failed" : 5
  },
  "hits" : {
    "total" : 0,
    "hits" : [ ]
  }
}%

do not return any result.

match_all returns :

  "hits" : {
    "total" : 2,
    "hits" : [ {
      "_index" : "ip",
      "_type" : "range",
      "_id" : "1", "_source" : {
        start: 1511729664,
        stop: 1511729919,
        location: 210743
}

    }, {
      "_index" : "ip",
      "_type" : "range",
      "_id" : "2", "_source" : {
        start: 10,
        stop: 20,
        location: 210743
}


I must be omitting something obvious :)


On Feb 11, 4:00 pm, Shay Banon <[hidden email]> wrote:

> Your thoughts are correct, you should use a boolean query with (I am
> guessing) two must clauses which will return an AND basically of the two
> clauses, here is an example:
>
> {
>     bool : {
>         must : {
>             range : { ... }
>         },
>         must: {
>             range : { ... }
>         }
>     }
>
> }
>
> -shay.banon
>
> On Thu, Feb 11, 2010 at 4:54 PM, Rabbit <[hidden email]> wrote:
> > Hi,
>
> > I'd like to do combined range query on document like
>
> > document {
> >   start: 10,
> >   stop: 20
> > }
>
> > Querying using '{ query : { range : {start: { from :null, to:
> > 15 } } }' works fine,
> > but how to do something like :
>
> > '{ query : { range : {start: { from :null, to: 15 } }, stop: { from:
> > 15, to: null} }'
>
> > I tried to combine them using boolean query without success.
>
> > Any clue ?
>
> > Thank you
Reply | Threaded
Open this post in threaded view
|

Re: Numeric Range Query

kimchy
Administrator
Yep, the actual Json should include the query tag as explained here:http://www.elasticsearch.com/docs/elasticsearch/json_api/search/. In your case:

{
    query : {
       bool : {
           must : {
               range : { start: { from :null, to: 15 } }
           },
           must: {
               range : { stop: { from :15, to: null } }
           }
       }
    }
}

The reason why its wrapped in query is because there are other parameters that you can pass on the search request JSON.

-shay.banon

On Thu, Feb 11, 2010 at 5:17 PM, Rabbit <[hidden email]> wrote:
 curl -X GET "http://localhost:9200/ip/_search?pretty=true" -d
\                                                         (~/workspace/
RestServer/dat)
'{
   bool : {
       must : {
           range : { start: { from :null, to: 15 } }
       },
       must: {
           range : { stop: { from :15, to: null } }
       }
   }
}'
{
 "_shards" : {
   "total" : 5,
   "successful" : 0,
   "failed" : 5
 },
 "hits" : {
   "total" : 0,
   "hits" : [ ]
 }
}%

do not return any result.

match_all returns :

 "hits" : {
   "total" : 2,
   "hits" : [ {
     "_index" : "ip",
     "_type" : "range",
     "_id" : "1", "_source" : {
       start: 1511729664,
       stop: 1511729919,
       location: 210743
}

   }, {
     "_index" : "ip",
     "_type" : "range",
     "_id" : "2", "_source" : {
       start: 10,
       stop: 20,
       location: 210743
}


I must be omitting something obvious :)


On Feb 11, 4:00 pm, Shay Banon <[hidden email]> wrote:
> Your thoughts are correct, you should use a boolean query with (I am
> guessing) two must clauses which will return an AND basically of the two
> clauses, here is an example:
>
> {
>     bool : {
>         must : {
>             range : { ... }
>         },
>         must: {
>             range : { ... }
>         }
>     }
>
> }
>
> -shay.banon
>
> On Thu, Feb 11, 2010 at 4:54 PM, Rabbit <[hidden email]> wrote:
> > Hi,
>
> > I'd like to do combined range query on document like
>
> > document {
> >   start: 10,
> >   stop: 20
> > }
>
> > Querying using '{ query : { range : {start: { from :null, to:
> > 15 } } }' works fine,
> > but how to do something like :
>
> > '{ query : { range : {start: { from :null, to: 15 } }, stop: { from:
> > 15, to: null} }'
>
> > I tried to combine them using boolean query without success.
>
> > Any clue ?
>
> > Thank you

Reply | Threaded
Open this post in threaded view
|

Re: Numeric Range Query

rabbit_fr
thank you, works fine now

On Feb 11, 4:21 pm, Shay Banon <[hidden email]> wrote:

> Yep, the actual Json should include the query tag as explained here:http://www.elasticsearch.com/docs/elasticsearch/json_api/search/. In your
> case:
>
> {
>     query : {
>        bool : {
>            must : {
>                range : { start: { from :null, to: 15 } }
>            },
>            must: {
>                range : { stop: { from :15, to: null } }
>            }
>        }
>     }
>
> }
>
> The reason why its wrapped in query is because there are other parameters
> that you can pass on the search request JSON.
>
> -shay.banon
>
> On Thu, Feb 11, 2010 at 5:17 PM, Rabbit <[hidden email]> wrote:
> >  curl -X GET "http://localhost:9200/ip/_search?pretty=true" -d
> > \                                                         (~/workspace/
> > RestServer/dat)
> > '{
> >    bool : {
> >        must : {
> >             range : { start: { from :null, to: 15 } }
> >         },
> >        must: {
> >            range : { stop: { from :15, to: null } }
> >        }
> >    }
> > }'
> > {
> >  "_shards" : {
> >    "total" : 5,
> >    "successful" : 0,
> >    "failed" : 5
> >  },
> >  "hits" : {
> >    "total" : 0,
> >    "hits" : [ ]
> >  }
> > }%
>
> > do not return any result.
>
> > match_all returns :
>
> >  "hits" : {
> >    "total" : 2,
> >    "hits" : [ {
> >      "_index" : "ip",
> >      "_type" : "range",
> >      "_id" : "1", "_source" : {
> >        start: 1511729664,
> >        stop: 1511729919,
> >        location: 210743
> > }
>
> >    }, {
> >      "_index" : "ip",
> >      "_type" : "range",
> >      "_id" : "2", "_source" : {
> >        start: 10,
> >        stop: 20,
> >        location: 210743
> > }
>
> > I must be omitting something obvious :)
>
> > On Feb 11, 4:00 pm, Shay Banon <[hidden email]> wrote:
> > > Your thoughts are correct, you should use a boolean query with (I am
> > > guessing) two must clauses which will return an AND basically of the two
> > > clauses, here is an example:
>
> > > {
> > >     bool : {
> > >         must : {
> > >             range : { ... }
> > >         },
> > >         must: {
> > >             range : { ... }
> > >         }
> > >     }
>
> > > }
>
> > > -shay.banon
>
> > > On Thu, Feb 11, 2010 at 4:54 PM, Rabbit <[hidden email]> wrote:
> > > > Hi,
>
> > > > I'd like to do combined range query on document like
>
> > > > document {
> > > >   start: 10,
> > > >   stop: 20
> > > > }
>
> > > > Querying using '{ query : { range : {start: { from :null, to:
> > > > 15 } } }' works fine,
> > > > but how to do something like :
>
> > > > '{ query : { range : {start: { from :null, to: 15 } }, stop: { from:
> > > > 15, to: null} }'
>
> > > > I tried to combine them using boolean query without success.
>
> > > > Any clue ?
>
> > > > Thank you
Reply | Threaded
Open this post in threaded view
|

Re: Numeric Range Query

kimchy
Administrator
Great!, would love to hear your experiance with elasticsearch once you
get enough milage on it ;)

-shay.banon

On 2/11/10, Rabbit <[hidden email]> wrote:

> thank you, works fine now
>
> On Feb 11, 4:21 pm, Shay Banon <[hidden email]> wrote:
>> Yep, the actual Json should include the query tag as explained
>> here:http://www.elasticsearch.com/docs/elasticsearch/json_api/search/. In
>> your
>> case:
>>
>> {
>>     query : {
>>        bool : {
>>            must : {
>>                range : { start: { from :null, to: 15 } }
>>            },
>>            must: {
>>                range : { stop: { from :15, to: null } }
>>            }
>>        }
>>     }
>>
>> }
>>
>> The reason why its wrapped in query is because there are other parameters
>> that you can pass on the search request JSON.
>>
>> -shay.banon
>>
>> On Thu, Feb 11, 2010 at 5:17 PM, Rabbit <[hidden email]> wrote:
>> >  curl -X GET "http://localhost:9200/ip/_search?pretty=true" -d
>> > \                                                         (~/workspace/
>> > RestServer/dat)
>> > '{
>> >    bool : {
>> >        must : {
>> >             range : { start: { from :null, to: 15 } }
>> >         },
>> >        must: {
>> >            range : { stop: { from :15, to: null } }
>> >        }
>> >    }
>> > }'
>> > {
>> >  "_shards" : {
>> >    "total" : 5,
>> >    "successful" : 0,
>> >    "failed" : 5
>> >  },
>> >  "hits" : {
>> >    "total" : 0,
>> >    "hits" : [ ]
>> >  }
>> > }%
>>
>> > do not return any result.
>>
>> > match_all returns :
>>
>> >  "hits" : {
>> >    "total" : 2,
>> >    "hits" : [ {
>> >      "_index" : "ip",
>> >      "_type" : "range",
>> >      "_id" : "1", "_source" : {
>> >        start: 1511729664,
>> >        stop: 1511729919,
>> >        location: 210743
>> > }
>>
>> >    }, {
>> >      "_index" : "ip",
>> >      "_type" : "range",
>> >      "_id" : "2", "_source" : {
>> >        start: 10,
>> >        stop: 20,
>> >        location: 210743
>> > }
>>
>> > I must be omitting something obvious :)
>>
>> > On Feb 11, 4:00 pm, Shay Banon <[hidden email]> wrote:
>> > > Your thoughts are correct, you should use a boolean query with (I am
>> > > guessing) two must clauses which will return an AND basically of the
>> > > two
>> > > clauses, here is an example:
>>
>> > > {
>> > >     bool : {
>> > >         must : {
>> > >             range : { ... }
>> > >         },
>> > >         must: {
>> > >             range : { ... }
>> > >         }
>> > >     }
>>
>> > > }
>>
>> > > -shay.banon
>>
>> > > On Thu, Feb 11, 2010 at 4:54 PM, Rabbit <[hidden email]>
>> > > wrote:
>> > > > Hi,
>>
>> > > > I'd like to do combined range query on document like
>>
>> > > > document {
>> > > >   start: 10,
>> > > >   stop: 20
>> > > > }
>>
>> > > > Querying using '{ query : { range : {start: { from :null, to:
>> > > > 15 } } }' works fine,
>> > > > but how to do something like :
>>
>> > > > '{ query : { range : {start: { from :null, to: 15 } }, stop: { from:
>> > > > 15, to: null} }'
>>
>> > > > I tried to combine them using boolean query without success.
>>
>> > > > Any clue ?
>>
>> > > > Thank you
>

--
Sent from my mobile device
Reply | Threaded
Open this post in threaded view
|

Re: Numeric Range Query

Simon Willison
In reply to this post by kimchy
On Feb 11, 3:00 pm, Shay Banon <[hidden email]> wrote:

> Your thoughts are correct, you should use a boolean query with (I am
> guessing) two must clauses which will return an AND basically of the two
> clauses, here is an example:
>
> {
>     bool : {
>         must : {
>             range : { ... }
>         },
>         must: {
>             range : { ... }
>         }
>     }
> }

I'm a bit worried about that example - it isn't valid JSON according
to the JSON spec (which requires double quotes around all strings),
but more importantly JSON objects shouldn't have two keys with the
same names. If you feed the above in to many (most?) JSON libraries
you'll lose data. Here's what the Python JSON library does with it,
for example:

In [45]: s = """{
   ....:   "bool" : {
   ....:     "must" : {
   ....:       "range" : 1
   ....:     },
   ....:     "must": {
   ....:       "range" : 2
   ....:     }
   ....:   }
   ....: }"""

In [46]: json.loads(s)
Out[46]: {u'bool': {u'must': {u'range': 2}}}

One fix would be to use the following syntax instead:

{
    "bool" : {
        "must" : [
            {
                "range" : { ... }
            },
            {
                "range": { ... }
            }
        ]
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: Numeric Range Query

kimchy
Administrator
You are absolutely right, it format supported should be supported as well.Though I think the current format is a valid JSON, I think there is the underlying assumption that field names should be unique. So, I created the following issue: http://github.com/elasticsearch/elasticsearch/issues/#issue/6 and it should be fixed by now in master :)

-shay.banon

On Fri, Feb 12, 2010 at 2:34 PM, Simon Willison <[hidden email]> wrote:
On Feb 11, 3:00 pm, Shay Banon <[hidden email]> wrote:
> Your thoughts are correct, you should use a boolean query with (I am
> guessing) two must clauses which will return an AND basically of the two
> clauses, here is an example:
>
> {
>     bool : {
>         must : {
>             range : { ... }
>         },
>         must: {
>             range : { ... }
>         }
>     }
> }

I'm a bit worried about that example - it isn't valid JSON according
to the JSON spec (which requires double quotes around all strings),
but more importantly JSON objects shouldn't have two keys with the
same names. If you feed the above in to many (most?) JSON libraries
you'll lose data. Here's what the Python JSON library does with it,
for example:

In [45]: s = """{
  ....:   "bool" : {
  ....:     "must" : {
  ....:       "range" : 1
  ....:     },
  ....:     "must": {
  ....:       "range" : 2
  ....:     }
  ....:   }
  ....: }"""

In [46]: json.loads(s)
Out[46]: {u'bool': {u'must': {u'range': 2}}}

One fix would be to use the following syntax instead:

{
   "bool" : {
       "must" : [
           {
               "range" : { ... }
           },
           {
               "range": { ... }
           }
       ]
   }
}