[RavenDB] Query fails when querying again a numerical (long) field

45 views
Skip to first unread message

Matt Warren

unread,
May 17, 2010, 12:59:05 PM5/17/10
to ravendb
I have the following document (only relevant fields shown);

{
* ViewCount: 0
* CreationDate: "/Date(1267396921443+0000)/"
* OwnerUserId: 188414
* PostTypeId: 2 //this is an integer not a string
.......
}

And I create the following index:
docs.Posts.Select(doc => new {PostTypeId = doc.PostTypeId})

then hit this Url
http://localhost:8080/indexes/PostIndex?Query=PostTypeId:2
I get no results returned.

However if I change the index query to:
docs.Posts.Select(doc => new {PostTypeId =
doc.PostTypeId.ToString()})

then the query http://localhost:8080/indexes/PostIndex?Query=PostTypeId:2,
returns the expected results, i.e. the "post" documents, where
PostTypeId = 2.

Is this related to the Lucene indexing and the fact that it's text
based?

If so what's the best way round it? To always make sure my indexes
use .ToString() on a non-text field. Or is it a bug (if so I'm happy
to try and fix it if someone could point me in the right direction).

Ayende Rahien

unread,
May 17, 2010, 1:06:21 PM5/17/10
to ravendb
Matt, yes, it is.
You need to use the following logic to be able to query on numbers:
string.Format("0x{0:X16}", number)

We need this is to allow range queries.

Alternatively, you can specify in your index definition that this field in NotAnalyzed, which will allow to query on it directly

Matt Warren

unread,
May 17, 2010, 3:23:57 PM5/17/10
to ravendb
Is there any difference between the 2 solutions? Is it more/less
efficient to change the index so that the field is "NotAnalyzed"?

For anyone else looking at this 1 thing caught me out:

If you field is a Int32 then you need this query
(string.Format("0x{0:X8}", number))

http://localhost:8080/indexes/PostIndex?Query=PostTypeId:0x00000002

If it's Int64 (string.Format("0x{0:X16}", number))

http://localhost:8080/indexes/PostIndex?Query=PostTypeId:0x0000000000000002

On May 17, 6:06 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Matt, yes, it is.
> You need to use the following logic to be able to query on numbers:
> string.Format("0x{0:X16}", number)
>
> We need this is to allow range queries.
>
> Alternatively, you can specify in your index definition that this field in
> NotAnalyzed, which will allow to query on it directly
>
> On Mon, May 17, 2010 at 5:59 PM, Matt Warren <mattd...@googlemail.com>wrote:
>
> > I have the following document (only relevant fields shown);
>
> > {
> >    * ViewCount: 0
> >    * CreationDate: "/Date(1267396921443+0000)/"
> >    * OwnerUserId: 188414
> >    * PostTypeId: 2 //this is an integer not a string
> >    .......
> > }
>
> > And I create the following index:
> >    docs.Posts.Select(doc => new {PostTypeId = doc.PostTypeId})
>
> > then hit this Url
> >    http://localhost:8080/indexes/PostIndex?Query=PostTypeId:2
> > I get no results returned.
>
> > However if I change the index query to:
> >    docs.Posts.Select(doc => new {PostTypeId =
> > doc.PostTypeId.ToString()})
>
> > then the queryhttp://localhost:8080/indexes/PostIndex?Query=PostTypeId:2,

Ayende Rahien

unread,
May 17, 2010, 3:43:19 PM5/17/10
to ravendb
Matt,
It depends on whatever you want to be able to do thins like:

PostsCount:[100 TO 1000]

Or:

PostTypeId:2

I think, given that range queries are probably rarer, we should make those queries more complex, rather than the default

Matt Warren

unread,
May 17, 2010, 4:38:57 PM5/17/10
to ravendb
Okay, that makes sense, thanks for the help

On May 17, 8:43 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Matt,
> It depends on whatever you want to be able to do thins like:
>
> PostsCount:[100 TO 1000]
>
> Or:
>
> PostTypeId:2
>
> I think, given that range queries are probably rarer, we should make those
> queries more complex, rather than the default
>
> On Mon, May 17, 2010 at 8:23 PM, Matt Warren <mattd...@googlemail.com>wrote:
>
> > Is there any difference between the 2 solutions? Is it more/less
> > efficient to change the index so that the field is "NotAnalyzed"?
>
> > For anyone else looking at this 1 thing caught me out:
>
> > If you field is a Int32 then you need this query
> > (string.Format("0x{0:X8}", number))
>
> >http://localhost:8080/indexes/PostIndex?Query=PostTypeId:0x00000002
>
> > If it's Int64 (string.Format("0x{0:X16}", number))
>
> >http://localhost:8080/indexes/PostIndex?Query=PostTypeId:0x0000000000...
Reply all
Reply to author
Forward
0 new messages