Re: [RavenDB] Lucene query: using NOT operator with field

1,339 views
Skip to first unread message

Matt Warren

unread,
Jul 1, 2011, 7:48:53 AM7/1/11
to rav...@googlegroups.com
I think you need to leave out the "AND", try
    "Name:xpto NOT Id:1234"

See http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#NOT

On 1 July 2011 12:20, BFC <bruno....@gmail.com> wrote:

I am trying to do this lucene query

"Name:xpto AND NOT Id:1234"


Basicaly I want documents with Name xpto and the Id (which is the PK) !
= 1234

The query is not working, since it returns the document with id 1234.

Of course I can do the query in another way. However I am curious why
this query is not working.

Am I doing something wrong?

Thanks in advance

BFC

unread,
Jul 1, 2011, 7:20:34 AM7/1/11
to ravendb, aye...@ayende.com

BFC

unread,
Jul 1, 2011, 7:55:44 AM7/1/11
to ravendb, aye...@ayende.com

In fact, my query works if I add AndAlso() clause


var query = session.Advanced.LuceneQuery<object>()
.WhereEquals("@metadata.Raven-Entity-Name",
documentStore.Conventions.GetTypeTagName(_modelType))
.AndAlso()
.Where(string.Format("{0}:{1} AND NOT {2}:{3}",
_fieldName, value.ToString(), _pkFieldName, keyValue));


For example if I do this

var query = session.Advanced.LuceneQuery<object>()
.WhereEquals("@metadata.Raven-Entity-Name",
documentStore.Conventions.GetTypeTagName(_modelType)).WhereEquals("MyField",
"SomeValue")

only the first WhereEqual is being applied.

@ayende: is this the supposed behavior?




On Jul 1, 12:48 pm, Matt Warren <mattd...@gmail.com> wrote:
> I think you need to leave out the "AND", try
>     "Name:xpto NOT Id:1234"
>
> Seehttp://lucene.apache.org/java/2_4_0/queryparsersyntax.html#NOT

BFC

unread,
Jul 1, 2011, 11:47:53 AM7/1/11
to ravendb
The problem was in the query itself.

I have an Id, which is an integer. For example if the query is

@metadata.Raven-Entity-Name:Areas AND Id:1

Raven returns no results. the correct query is

@metadata.Raven-Entity-Name:Areas AND Id:areas/1




On Jul 1, 12:48 pm, Matt Warren <mattd...@gmail.com> wrote:
> I think you need to leave out the "AND", try
>     "Name:xpto NOT Id:1234"
>
> Seehttp://lucene.apache.org/java/2_4_0/queryparsersyntax.html#NOT

Matt Warren

unread,
Jul 1, 2011, 12:43:50 PM7/1/11
to ravendb
> For example if I do this
> var query = session.Advanced.LuceneQuery<object>()
> .WhereEquals("@metadata.Raven-Entity-Name", documentStore.Conventions.GetTypeTagName(_modelType))
> .WhereEquals("MyField" , "SomeValue")
> only the first WhereEqual is being applied.

What does the query look like if you do this? You can call .ToString()
on it to find out.

The query parser should append the queries, but you do need
AndAlso(..) or something like that in the middle otherwise it doesn't
make sense.
Reply all
Reply to author
Forward
0 new messages