你能告诉我如何在RavenDb中执行简单的全文搜索吗?数据库存储文档:Movie {Name = "Pirates of the Carribean"}。我希望在搜索短语“加勒比海盗”或任何其他单词组合中找到此文档。
问问题
7391 次
3 回答
16
Boris, Rob 的答案有正确的索引,但是查询起来有点尴尬。你可以这样做:
session.Query<Movie, Movie_ByName>()
.Search(x=>x.Name, searchTerms)
.ToList()
那将
于 2011-11-27T06:55:28.917 回答
12
您担心的与全文无关 - 默认情况下,Lucene 在 OR 基础上工作,而您想要的是 AND
如果我是你我会做
String[] terms = searchTerm.Split(" "); // Or whatever the string.split method is
和
.Where("Name:(" + String.Join(" AND ", terms) + ")");
您的索引应该类似于
public class Movie_ByName : AbstractIndexCreationTask
{
public override IndexDefinition CreateIndexDefinition()
{
return new IndexDefinitionBuilder<Movie>
{
Map = movies => from movie in movies
select new { movie.Name, market.Id },
Indexes =
{
{x => x.Name, FieldIndexing.Analyzed}
}
}
.ToIndexDefinition(DocumentStore.Conventions);
}
您不需要存储,您不会在任何时候直接从 lucene 请求数据。您甚至可能不需要索引(您可能实际上需要 FieldIndexing.Analyzed,并且可能只在此处使用动态查询就可以逃脱)
不过取决于你。
于 2010-12-01T10:09:00.003 回答
2
以下是我实现“ANDing”术语搜索的方法。
首先,确保您的字段已被索引和分析:
public class MyIndex: AbstractIndexCreationTask<MyDocument>
{
public MyIndex()
{
Map = docs => from d in docs
select new { d.MyTextField };
Index(x => x.MyTextField, FieldIndexing.Analyzed);
}
}
然后从客户端查询:
var query = session.Query<MyDocument, MyIndex>();
query = theSearchText
.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries)
.Aggregate(query, (q, term) =>
q.Search(x => x.MyTextField, term, options: SearchOptions.And));
于 2012-12-22T03:39:48.127 回答