C# 4.0 使用C的弹性搜索字符串,其中包含空格和特殊字符#
我正在寻找ElasticSearch nest查询,它将使用C#对包含空格的字符串提供精确匹配 例如,我想搜索像“XYZ公司解决方案”这样的词。我尝试了querystring查询,但不管搜索结果如何,它都会提供所有记录。我还读了这篇文章,发现我们必须为字段添加一些映射。我在现场尝试了“未分析”分析仪,但仍然不起作用 这是我的C代码#C# 4.0 使用C的弹性搜索字符串,其中包含空格和特殊字符#,c#-4.0,elasticsearch,couchbase,nest,C# 4.0,elasticsearch,Couchbase,Nest,我正在寻找ElasticSearch nest查询,它将使用C#对包含空格的字符串提供精确匹配 例如,我想搜索像“XYZ公司解决方案”这样的词。我尝试了querystring查询,但不管搜索结果如何,它都会提供所有记录。我还读了这篇文章,发现我们必须为字段添加一些映射。我在现场尝试了“未分析”分析仪,但仍然不起作用 这是我的C代码# var indexDefinition=新的RootObjectMapping { 属性=新字典(), Name=elastic_newindexname }; v
var indexDefinition=新的RootObjectMapping
{
属性=新字典(),
Name=elastic_newindexname
};
var notAnalyzedField=新的StringMapping
{
Index=FieldIndexOption.notanalysis
};
indexDefinition.Properties.Add(“Name”,notAnalyzedField);
DeleteIndex(d=>d.Index(elastic_newindexname));
var reindex=objElasticClient.reindex(r=>r.FromIndex(elastic_oldindexname).ToIndex(elastic_newindexname).Query(q=>q.MatchAll()).Scroll(“10s”).CreateIndex(i=>i.AddMapping(m=>m.InitializeUsing(indexDefinition));
ReindexObserver o=新的ReindexObserver(onError:e=>{});
重新索引订阅(o)**
**ISearchResponse ivals=objElasticClient.Search(s=>s.Index(elastic_newindexname).AllTypes().Query(q=>q.Term(“名称”,“XYZ公司解决方案”));**//这将提供0条记录
**ISearchResponse IVAS1=objElasticClient.Search(s=>s.Index(elastic_newindexname).AllTypes().Query(q=>q.Term(u=>u.OnField(“Name”).Value(“XYZ公司解决方案”));**//这将提供0条记录
**ISearchResponse ivals=objElasticClient.Search(s=>s.Index(elastic_newindexname).AllTypes().Query(@“Name='XYZ公司解决方案”);**//这将提供所有字段值以“XYZ”开头的记录
如果有人在C#中有完整的示例或步骤,请与我分享好吗?看起来您只需要在重新索引操作后刷新新索引 使用您的代码示例(和第一个术语查询),我看到了相同的结果——0次点击 在
reindex.Subscribe()
调用之后添加以下Refresh
调用将导致返回一次点击:
objElasticClient.Refresh(新的RefreshRequest(){});
您是否尝试过匹配短语查询
请求的查询DSL如下所示:
"query": {
"match_phrase": {
"title": "XYZ Company Solutions"
}
}
_client.Search<T>(s => s
.Index(IndexName)
.Types(typeof (T))
.Query(q => q.MatchPhrase(m => m
.OnField(f => f.Name)
.Query("XYZ Company Solutions"))));
在C#中,尝试以下操作:
"query": {
"match_phrase": {
"title": "XYZ Company Solutions"
}
}
_client.Search<T>(s => s
.Index(IndexName)
.Types(typeof (T))
.Query(q => q.MatchPhrase(m => m
.OnField(f => f.Name)
.Query("XYZ Company Solutions"))));
\u client.Search(s=>s
.Index(索引名)
.类型(类型(T))
.Query(q=>q.MatchPhrase(m=>m
.OnField(f=>f.Name)
.Query(“XYZ公司解决方案”);
有关更多信息,请查看官方文档:
请参考以下代码,我认为这将满足您的要求。 在这里,我使用动态模板创建并映射索引,然后执行XDCR。 现在,所有字符串字段都不会被分析
IIndicesOperationResponse result = null;
if (!objElasticClient.IndexExists(elastic_indexname).Exists)
{
result = objElasticClient.CreateIndex(elastic_indexname, c => c.AddMapping<dynamic>(m => m.Type("_default_").DynamicTemplates(t => t
.Add(f => f.Name("string_fields").Match("*").MatchMappingType("string").Mapping(ma => ma
.String(s => s.Index(FieldIndexOption.NotAnalyzed)))))));
}
IIndicesOperationResponse结果=null;
如果(!objElasticClient.indexists(elastic_indexname).Exists)
{
result=objElasticClient.CreateIndex(弹性索引名,c=>c.AddMapping(m=>m.Type(“\u默认值”)).DynamicTemplates(t=>t
.Add(f=>f.Name(“字符串\字段”).Match(“*”).MatchMappingType(“字符串”).Mapping(ma=>ma
.String(s=>s.Index(FieldIndexOption.notanalysisd‘‘‘‘‘‘‘)’);
}
谢谢
Mukesh Raghuwanshi感谢Rick的回复,他尝试了这一点,但仍然得到了相同的结果,即0次点击。您能否提供您在elasticsearch上创建的索引模式?curl-XGET localhost:9200/name_of_the_index?pretty=true基本上,为了匹配精确的字符串,您需要确保在索引时不分析它。感谢Mukesh,这正是我需要的…-)