3

I'm using such a query:

var query = "*" + QueryParser.Escape(input) + "*";
session.Query<User, UsersByEmailAndName>().Where(x => x.Email.In(query) || x.DisplayName.In(query));

With the support of a simple index:

public UsersByEmailAndName()
{
    Map = users => from user in users
                   select new
                          {
                              user.Email,
                              user.DisplayName,
                          };            
}

Here I've read that:

"By default, RavenDB uses a custom analyzer called LowerCaseKeywordAnalyzer for all content. (...) The default values for each field are FieldStorage.No in Stores and FieldIndexing.Default in Indexes."

The index contains fields:

DisplayName - "jarek waliszko" and Email - "my_email@domain.com"

And finally the thing is:

If the query is something like *_email@* or *ali* the result is fine. But while I use spacebar inside e.g. *ek wa*, nothing is returned. Why and how to fix it ?

Btw: I'm using RavenDB - Build #960

jwaliszko
  • 16,942
  • 22
  • 92
  • 158
  • * term * is rather expensive. Also why aren't you using .Search() to do your searching? – Phil Sep 06 '12 at 12:18
  • I've tried searching `.Search(x => x.DisplayName, string.Format("*{0}*", query), escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards, options: SearchOptions.And)` it behaves the same way - spacebar is not recognized as I expect (btw. I know * is slow, especially when used as prefix). Besides I don't know how to search additionally across email also (without build new index which concatenates fields). – jwaliszko Sep 06 '12 at 12:56

3 Answers3

3

Change the Index option for the fields you want to search on to be Analyzed, instead of Default Also, take a look here: http://ayende.com/blog/152833/orders-search-in-ravendb

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
1

So.., I've came up with an idea how to do it. I don't know if this is the "right way" but it works for me.

query changes to:

var query = string.Format("*{0}*", Regex.Replace(QueryParser.Escape(input), @"\s+", "-"));

index changes to:

public UsersByEmailAndName()
{
    Map = users => from user in users
                   select new
                          {
                              user.Email,
                              DisplayName = user.DisplayName.Replace(" ", "-"),
                          };
}

I've just changed whitespaces into dashes for the user input text and spacebars to dashes in the indexed display name. The query gives expected results right now. Nothing else really changed, I'm still using LowerCaseKeywordAnalyzer as before.

jwaliszko
  • 16,942
  • 22
  • 92
  • 158
1

Lucene’s query parser interprets the space in the search term as a break in the actual query, and doesn’t include it in the search. Any part of the search term that appears after the space is also disregarded.

So you should escape space character by prepending the backslash character before whitespace character. Try to query *jarek\ waliszko*.

jwaliszko
  • 16,942
  • 22
  • 92
  • 158
Lauri Lüüs
  • 189
  • 2
  • 8
  • Thanks (+1), I wish I had such a tip some time ago but still good to know. By the way I think `QueryParser.Escape` should be responsible for doing this (**escape** the spacebar - as indicated by the name of the method). – jwaliszko May 06 '13 at 12:09