弹性搜索 - 使用C#在其中包含空格和特殊字符的字符串

Dib*_*iya 5 c#-4.0 elasticsearch couchbase nest

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

例如 - 我想搜索像'XYZ Company Solutions'这样的词.我尝试了查询字符串查询但它给了我所有的记录,无论搜索结果如何.我也读了帖子,发现我们必须为该字段添加一些映射.我在球场上尝试了'Not_Analyzed'分析仪,但它仍无法正常工作.

这是我的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"
Run Code Online (Sandbox Code Playgroud)

如果有人在C#中有完整的示例或步骤,那么请您与我分享?

Muk*_*esh 1

请参考下面的代码,我想这可以满足您的要求。在这里,我使用动态模板创建并映射了索引,然后执行了 XDCR。现在所有字符串字段都将被 not_analysis。

 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)))))));
                }
Run Code Online (Sandbox Code Playgroud)

谢谢

穆克什·拉古万希