在RavenDB中查询带有Where子句的术语
有了RavenDB,就可以得到索引的项。此功能已记录在案 简短问题:是否有可能通过索引的其他字段过滤这些术语 长问题: 我们正在创建一个知识库应用程序,客户可以在其中存储主题 我们建立了一个用于全文搜索的索引,并对所有主题建立索引。在索引中,我们有两个可查询的属性:CompanyName、Query在RavenDB中查询带有Where子句的术语,ravendb,Ravendb,有了RavenDB,就可以得到索引的项。此功能已记录在案 简短问题:是否有可能通过索引的其他字段过滤这些术语 长问题: 我们正在创建一个知识库应用程序,客户可以在其中存储主题 我们建立了一个用于全文搜索的索引,并对所有主题建立索引。在索引中,我们有两个可查询的属性:CompanyName、Query public class SearchQueryResult { public string Query { get; set; } public string CompanyNa
public class SearchQueryResult
{
public string Query { get; set; }
public string CompanyName { get; set; }
}
public class FullTextIndex : AbstractIndexCreationTask<Topic, SearchQueryResult>
{
public FullTextIndex()
{
Map = topics => from topic in topics
from content in topic.Content
select new
{
topic.CompanyName,
Query = new object[]
{
content.Title,
content.Description,
content.Article
}
};
Analyzers.Add(x => x.Query, typeof(OurCustomHtmlStandardAnalyzer).AssemblyQualifiedName);
Indexes.Add(x => x.Query, FieldIndexing.Analyzed);
Stores.Add(x => x.Query, FieldStorage.Yes);
}
}
公共类SearchQueryResult
{
公共字符串查询{get;set;}
公共字符串CompanyName{get;set;}
}
公共类FullTextIndex:AbstractIndexCreationTask
{
公共全文索引()
{
Map=topics=>来自主题中的主题
来自topic.content中的内容
选择新的
{
topic.CompanyName,
查询=新对象[]
{
内容,标题,,
内容,说明,,
内容.文章
}
};
Analyzers.Add(x=>x.Query,typeof(OurCustomHtmlStandardAnalyzer).AssemblyQualifiedName);
添加(x=>x.Query,fieldindex.analysis);
添加(x=>x.Query,FieldStorage.Yes);
}
}
对于全文查询,我可以将结果限制到相应的公司用户
var results = _documentSession.Query<SearchQueryResult, FullTextIndex>()
.Where(x => x.Query.StartsWith(input))
.Where(x => x.CompanyName == "MyCompany")
.OfType<Topic>()
.ToList();
var results=\u documentSession.Query()
.Where(x=>x.Query.StartsWith(输入))
.其中(x=>x.CompanyName==“MyCompany”)
第()类
.ToList();
我们想在搜索输入字段中添加一个自动补全,为此,我们需要获取索引的术语(查询字段)
问题是,索引确实包含所有公司的术语,而我们只需要当前公司的术语
在RavenDB Studio中,可以从索引中获取存储的、经过分析的术语:
-->如何在客户端代码中通过查询获取这些值
感谢您的建议您可以查询如下索引:
var results = _documentSession.Query<SearchQueryResult, FullTextIndex>()
.Where(x => x.CompanyName == "MyCompany")
.Select(x=>x.Query)
.ToList();
没有其他方法可以获取索引项。为了获得术语,客户机api提供的唯一函数都有文档记录 因此,如果必须通过某些属性值过滤术语,则必须将where子句移动到索引定义中 如果要筛选的值预先已知,则可以使用从
AbstractIndexCreationTask
public class FullTextIndex : AbstractIndexCreationTask<Topic, SearchQueryResult>
{
public FullTextIndex()
{
Map = topics => from topic in topics
where topic.CompanyName == "TheFirm"
from content in topic.Content
select new
{
topic.CompanyName,
Query = new object[]
{
content.Title,
content.Description,
content.Article
}
};
Analyzers.Add(x => x.Query, typeof(OurCustomHtmlStandardAnalyzer).AssemblyQualifiedName);
Indexes.Add(x => x.Query, FieldIndexing.Analyzed);
Stores.Add(x => x.Query, FieldStorage.Yes);
}
}
公共类FullTextIndex:AbstractIndexCreationTask
{
公共全文索引()
{
Map=topics=>来自主题中的主题
其中topic.CompanyName==“TheFirm”
来自topic.content中的内容
选择新的
{
topic.CompanyName,
查询=新对象[]
{
内容,标题,,
内容,说明,,
内容.文章
}
};
Analyzers.Add(x=>x.Query,typeof(OurCustomHtmlStandardAnalyzer).AssemblyQualifiedName);
添加(x=>x.Query,fieldindex.analysis);
添加(x=>x.Query,FieldStorage.Yes);
}
}
请注意映射部分中的where子句
如果您事先不知道筛选值(例如,如果您有多个公司),则可以为每个公司构建专用索引。因此,您将得到一些静态索引,如FullTextIndexForXXXX、FullTextIndexForyyyyy等
然后,这些索引包含过滤,您可以使用RavenDB客户端api提供的GetTerms函数来获取术语
必须动态生成索引定义。我正在使用IndexFactory创建索引。 (必须在应用程序启动时调用)
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Raven.Abstractions.index;
使用Raven.Client;
命名空间MyNamespace.Data.Index.FullTextIndex
{
公共静态类FullTextIndexFactory
{
公共静态void CreateFullTextIndexes(IDocumentStore documentStore)
{
尝试
{
var公司=GetCompanys(documentStore);
foreach(公司中的var公司)
{
var index Name=“FullTextIndexFor”+公司名称;
var indexDefinition=CreateIndexDefinition(documentStore,公司);
var existingIndexDefinition=documentStore.DatabaseCommands.GetIndex(indexName);
如果(existingIndexDefinition==null)
{
CreateIndex(documentStore、indexDefinition、indexName);
继续;
}
if(existingIndexDefinition.Equals(indexDefinition))
继续;
更新索引(documentStore、indexDefinition、indexName);
}
}
捕获(异常)
{
//做一些例外的事情
}
}
公共静态void CreateIndex(IDocumentStore documentStore、IndexDefinition、IndexDefinition、string indexName)
{
documentStore.DatabaseCommands.PutIndex(indexName,indexDefinition);
}
公共静态void UpdateIndex(IDocumentStore documentStore、IndexDefinition、IndexDefinition、string indexName)
{
documentStore.DatabaseCommands.DeleteIndex(indexName);
CreateIndex(documentStore、indexDefinition、indexName);
}
公共静态IndexDefinition CreateIndexDefinition(IDocumentStore documentStore,公司)
{
var indexDefinition=新的indexDefinition();
var mapBuilder=新的StringBuilder();
mapBuilder.AppendLine(“docs.Topics.SelectMany(topic=>topic)。
public class FullTextIndex : AbstractIndexCreationTask<Topic, SearchQueryResult>
{
public FullTextIndex()
{
Map = topics => from topic in topics
where topic.CompanyName == "TheFirm"
from content in topic.Content
select new
{
topic.CompanyName,
Query = new object[]
{
content.Title,
content.Description,
content.Article
}
};
Analyzers.Add(x => x.Query, typeof(OurCustomHtmlStandardAnalyzer).AssemblyQualifiedName);
Indexes.Add(x => x.Query, FieldIndexing.Analyzed);
Stores.Add(x => x.Query, FieldStorage.Yes);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Abstractions.Indexing;
using Raven.Client;
namespace MyNamespace.Data.Index.FullTextIndex
{
public static class FullTextIndexFactory
{
public static void CreateFullTextIndexes(IDocumentStore documentStore)
{
try
{
var companies = GetCompanies(documentStore);
foreach (var company in companies)
{
var indexName = "FullTextIndexFor" + company.Name;
var indexDefinition = CreateIndexDefinition(documentStore, company);
var existingIndexDefinition = documentStore.DatabaseCommands.GetIndex(indexName);
if (existingIndexDefinition == null)
{
CreateIndex(documentStore, indexDefinition, indexName);
continue;
}
if (existingIndexDefinition.Equals(indexDefinition))
continue;
UpdateIndex(documentStore, indexDefinition, indexName);
}
}
catch (Exception exception)
{
// do something with the exception
}
}
public static void CreateIndex(IDocumentStore documentStore, IndexDefinition indexDefinition, string indexName)
{
documentStore.DatabaseCommands.PutIndex(indexName, indexDefinition);
}
public static void UpdateIndex(IDocumentStore documentStore, IndexDefinition indexDefinition, string indexName)
{
documentStore.DatabaseCommands.DeleteIndex(indexName);
CreateIndex(documentStore, indexDefinition, indexName);
}
public static IndexDefinition CreateIndexDefinition(IDocumentStore documentStore, Company company)
{
var indexDefinition = new IndexDefinition();
var mapBuilder = new StringBuilder();
mapBuilder.AppendLine("docs.Topics.SelectMany(topic => topic.Content, (topic, content) => new");
mapBuilder.AppendLine("{");
mapBuilder.AppendLine(" topic = topic,");
mapBuilder.AppendLine(" content = content");
mapBuilder.AppendLine("})");
mapBuilder.AppendFormat(".Where(this0 => this0.topic.CompanyName == \"{0}\")", company.Name);
mapBuilder.AppendLine(".Select(this0 => new ");
mapBuilder.AppendLine("{");
mapBuilder.AppendLine(" CompanyName = this0.topic.CompanyName,");
mapBuilder.AppendLine(" Query = new object[]");
mapBuilder.AppendLine(" {");
mapBuilder.AppendLine(" this0.content.Value.Title,");
mapBuilder.AppendLine(" this0.content.Value.Description,");
mapBuilder.AppendLine(" this0.content.Value.Article");
mapBuilder.AppendLine(" },");
mapBuilder.AppendLine(" PublishStartDate = this0.topic.PublishStartDate,");
mapBuilder.AppendLine(" PublishEndDate = this0.topic.PublishEndDate,");
mapBuilder.AppendLine("})");
var map = mapBuilder.ToString();
indexDefinition.Maps.Add(map);
indexDefinition.Name = "FullTextIndexFor" + company.Name;
var analyzerAssemblyQualifiedName = typeof(CustomFullTextAnalyzer).AssemblyQualifiedName;
indexDefinition.Analyzers.Add("Query", analyzerAssemblyQualifiedName);
indexDefinition.Stores.Add("Query", FieldStorage.Yes);
return indexDefinition;
}
private static IEnumerable<Company> GetCompanies(IDocumentStore documentStore)
{
using (var session = documentStore.OpenSession())
{
return session.Query<Company>().ToList();
}
}
}
}