在我们自己的RavenQueryableExtensions
课程中,我们有以下方法:
public static IRavenQueryable<T> SearchMultiple<T>(this IRavenQueryable<T> self,
Expression<Func<T, object>> fieldSelector, string queries,
decimal boost = 1, SearchOptions options = SearchOptions.Or)
{
if (String.IsNullOrEmpty(queries)) throw new ArgumentNullException("queries");
// More than two spaces or tabs are replaced with a single space
var newQueries = Regex.Replace(queries, @"\s{2,}", " ");
// not important for this question:
//newQueries = SyncShared.ReplacePostcode(newQueries);
// Splits the search-string into separate search-terms
var searchValues = newQueries.Split(' ');
return self.SearchMultiple(fieldSelector, searchValues, boost, options);
}
public static IRavenQueryable<T> SearchMultiple<T>(this IRavenQueryable<T> self,
Expression<Func<T, object>> fieldSelector, IEnumerable<string> queries,
decimal boost = 1, SearchOptions options = SearchOptions.Or)
{
if (queries == null) throw new ArgumentNullException("queries");
return queries.Aggregate(self, (current, query) => current.Search(fieldSelector, query + "* ", boost, options, EscapeQueryOptions.AllowPostfixWildcard));
}
使用searchValues-array中的所有松散搜索词创建搜索查询。但是,它似乎无法识别&
或.
等特殊字符。例如:
{Query:(A*) AND Query:(\&*) AND Query:(A*)}
有谁知道如何更改搜索方法,以便正确格式化这些特殊字符?
另外,我不知道它是否与我们的问题相关,但我们也使用AsciiFoldingAnalyzer
类(见下文)。这个课程允许我们在输入“e”或“u”时搜索“é”或“ü”等字符的公司。
using System.Collections.Generic;
using System.IO;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Util;
namespace NatWa.MidOffice.RavenDb.ServerGoodies
{
public class AsciiFoldingAnalyser : StandardAnalyzer
{
public AsciiFoldingAnalyser(Version matchVersion)
: base(matchVersion)
{
}
public AsciiFoldingAnalyser(Version matchVersion, ISet<string> stopWords)
: base(matchVersion, stopWords)
{
}
public AsciiFoldingAnalyser(Version matchVersion, FileInfo stopwords)
: base(matchVersion, stopwords)
{
}
public AsciiFoldingAnalyser(Version matchVersion, TextReader stopwords)
: base(matchVersion, stopwords)
{
}
public override TokenStream TokenStream(string fieldName, TextReader reader)
{
return new LowerCaseFilter(new ASCIIFoldingFilter(base.TokenStream(fieldName, reader)));
}
}
}
我们在我们的Mappings中使用它,如下:
public class UserLijst : AbstractIndexCreationTask<UserState, UserLijstResult>
{
public UserLijst()
{
Map = states => from state in states
select new UserLijstResult
{
Id = (UserId)state.AggregateId,
Naam = state.Naam,
Query = new object[]
{
state.Naam
}
};
Reduce = results => from result in results
group result by new { result.Id } into g
select new UserLijstResult
{
Id = g.Key.Id,
Naam = g.First().Naam,
Query = g.First().Query
};
Index("Query", FieldIndexing.Analyzed);
Analyze(result => result.Query, typeof(AsciiFoldingAnalyser).AssemblyQualifiedName);
}
}
答案 0 :(得分:2)
好的,事实证明这很容易。我们在分析器中使用了基本Tokenizer,它过滤掉长度为1的所有特殊字符和字符。当我们更换
时public override TokenStream TokenStream(string fieldName, TextReader reader)
{
return new LowerCaseFilter(new ASCIIFoldingFilter(base.TokenStream(fieldName, reader)));
}
在我们的AsciiFoldingAnalyser中:
public override TokenStream TokenStream(string fieldName, TextReader reader)
{
return new LowerCaseFilter(new ASCIIFoldingFilter(new WhitespaceTokenizer(reader)));
}
它有效。我们可以搜索特殊字符。
我们现在为“A&amp; A”这样的搜索获得了很多结果,因为它找到所有出现的字符“a”和“&amp;”在所有索引字段中,所以我们可能需要更改一些东西来缩小这一点,但至少我在问这个问题时得到了我想要的东西。