问题
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
回答1:
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
回答2:
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*
.
回答3:
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.
来源:https://stackoverflow.com/questions/12298468/substring-with-spacebar-search-in-ravendb