使用 C# 的弹性搜索搜索字符串中包含空格和特殊字符 [英] Elastic Search-Search string having spaces and special characters in it using C#

查看:41
本文介绍了使用 C# 的弹性搜索搜索字符串中包含空格和特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 ElasticSearch 嵌套查询,该查询将使用 C# 对包含空格的字符串提供精确匹配.

I am looking for ElasticSearch nest query which will provide exact match on string having spaces in it using C#.

例如 - 我想搜索像XYZ 公司解决方案"这样的词.我尝试了查询字符串查询,但无论搜索结果如何,它都会为我提供所有记录.我还阅读了帖子,发现我们必须为该字段添加一些映射.我在现场尝试了Not_Analyzed"分析器,但仍然无效.

for example - I want to search for a word like 'XYZ Company Solutions'. I tried querystring query but it gives me all the records irrespective of search result. Also i read on the post and found that we have to add some mappings for the field. I tried 'Not_Analyzed' analyzer on the field but still it does not worked.

这是我的C#代码

var indexDefinition = new RootObjectMapping
{
  Properties = new Dictionary<PropertyNameMarker, IElasticType>(),
  Name = elastic_newindexname
};
var notAnalyzedField = new StringMapping
{
  Index = FieldIndexOption.NotAnalyzed
};
indexDefinition.Properties.Add("Name", notAnalyzedField);
objElasticClient.DeleteIndex(d => d.Index(elastic_newindexname));
var reindex = objElasticClient.Reindex<dynamic>(r => r.FromIndex(elastic_oldindexname).ToIndex(elastic_newindexname).Query(q => q.MatchAll()).Scroll("10s").CreateIndex(i => i.AddMapping<dynamic>(m => m.InitializeUsing(indexDefinition))));
ReindexObserver<dynamic> o = new ReindexObserver<dynamic>(onError: e => { });
reindex.Subscribe(o);**

**ISearchResponse<dynamic> ivals = objElasticClient.Search<dynamic>(s => s.Index(elastic_newindexname).AllTypes().Query(q => q.Term("Name","XYZ Company Solutions")));** //this gives 0 records

**ISearchResponse<dynamic> ivals1 = objElasticClient.Search<dynamic>(s => s.Index(elastic_newindexname).AllTypes().Query(q => q.Term(u => u.OnField("Name").Value("XYZ Company Solutions"))));** //this gives 0 records

**ISearchResponse<dynamic> ivals = objElasticClient.Search<dynamic>(s => s.Index(elastic_newindexname).AllTypes().Query(@"Name = 'XYZ Company Solutions'"));** //this gives all records having fields value starting with "XYZ"

如果有人在 C# 中有完整的示例或步骤,可以分享给我吗?

If anyone have complete example or steps in C# then can you please share with me?

推荐答案

请参考下面的代码,我认为这将满足您的要求.在这里,我使用动态模板创建并映射了索引,然后进行了 XDCR.现在所有字符串字段都将不被分析.

Please refer the below code ,I think this will meet your requirements. Here I have created and mapped index with dynamic template and then did the XDCR. Now all string fields will be not_analysed.

 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)))))));
                }

谢谢

穆克什·拉古万希

这篇关于使用 C# 的弹性搜索搜索字符串中包含空格和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆