Issue with string equality

62 views
Skip to first unread message

Ben

unread,
May 3, 2012, 6:57:04 PM5/3/12
to ravendb
Hi,

Given the following document:

{
"FullName": "Ben Foster",
}

The following occurs:

var user1 = session.Query<User>().FirstOrDefault(u =>
u.FullName == "Ben Foster"); // returns user
var user2 = session.Query<User>().FirstOrDefault(u =>
u.FullName.Equals("Ben Foster")); // returns null
var user3 = session.Query<User>().FirstOrDefault(u =>
u.FullName.Equals("Ben Foster", StringComparison.Ordinal)); // returns
null
var user4 = session.Query<User>().FirstOrDefault(u =>
u.FullName.Equals("Ben Foster",
StringComparison.InvariantCultureIgnoreCase)); // returns user

Is this something to do with the Linq provider as I thought the ==
operator, Equals(string, string) and Equals(string,
StringComparison.Ordinal) effectively did the same thing?

Oren Eini (Ayende Rahien)

unread,
May 4, 2012, 9:49:28 AM5/4/12
to rav...@googlegroups.com
Ben,
we don't support Equals(string,  StringComparison) 

Chris Marisic

unread,
May 4, 2012, 10:12:56 AM5/4/12
to rav...@googlegroups.com
It's redudant to support Equals(string,  StringComparison)  because all of those comparisons could be facilitated by using an appropriate Lucene analyzer for the field, correct?

Oren Eini (Ayende Rahien)

unread,
May 4, 2012, 10:19:01 AM5/4/12
to rav...@googlegroups.com
Yes

Ben

unread,
May 4, 2012, 3:11:42 PM5/4/12
to ravendb
So by default we are always case insensitive? I'm assuming I need to
create an index for this then?

On May 4, 3:19 pm, "Oren Eini (Ayende Rahien)" <aye...@ayende.com>
wrote:
> Yes
>
>
>
>
>
>
>
> On Fri, May 4, 2012 at 5:12 PM, Chris Marisic <ch...@marisic.com> wrote:
> > It's redudant to support Equals(string,  StringComparison)  because all
> > of those comparisons could be facilitated by using an appropriate Lucene
> > analyzer for the field, correct?
>
> > On Friday, May 4, 2012 9:49:28 AM UTC-4, Oren Eini wrote:
>
> >> Ben,
> >> we don't support Equals(string,  StringComparison)
>
> >> On Fri, May 4, 2012 at 1:57 AM, Ben <b...@planetcloud.co.uk> wrote:
>
> >>> Hi,
>
> >>> Given the following document:
>
> >>> {
> >>>  "FullName": "Ben Foster",
> >>> }
>
> >>> The following occurs:
>
> >>>            var user1 = session.Query<User>().**FirstOrDefault(u =>
> >>> u.FullName == "Ben Foster"); // returns user
> >>>            var user2 = session.Query<User>().**FirstOrDefault(u =>
> >>> u.FullName.Equals("Ben Foster")); // returns null
> >>>            var user3 = session.Query<User>().**FirstOrDefault(u =>
> >>> u.FullName.Equals("Ben Foster", StringComparison.Ordinal)); // returns
> >>> null
> >>>            var user4 = session.Query<User>().**FirstOrDefault(u =>
> >>> u.FullName.Equals("Ben Foster",
> >>> StringComparison.**InvariantCultureIgnoreCase)); // returns user

Matt Warren

unread,
May 4, 2012, 4:47:10 PM5/4/12
to rav...@googlegroups.com
Yeah, by default RavenDB uses a custom analyser called "LowerCaseKeywordAnalyzer". There's a bit more info here http://ravendb.net/docs/client-api/querying/static-indexes/configuring-index-options.

You can change this by specifying a different analyser in the index, so yes you'll have to create your own index. See the link about for more info.

You write the index something like this:

new IndexDefinitionBuilder<BlogPost, BlogPost>
    {
        Map =  users =>
            from doc in users select new { doc.Tags, doc.Content },
        Analyzers =
            {
                {x => x.Tags, "SimpleAnalyzer"},
                {x => x.Content, "SnowballAnalyzer"}
            },
    });
Reply all
Reply to author
Forward
0 new messages