一尘不染

使用C#在其中具有空格和特殊字符的Elastic Search-Search字符串

elasticsearch

我正在寻找ElasticSearch嵌套查询,它将使用C#在字符串上提供完全匹配的字符串。

例如-我想搜索“ XYZ Company
Solutions”之类的词。我尝试使用querystring查询,但是无论搜索结果如何,它都会为我提供所有记录。我也阅读了帖子,发现我们必须为该字段添加一些映射。我在现场尝试了“
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"

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


阅读 308

收藏
2020-06-22

共1个答案

一尘不染

请参考下面的代码,我认为这将满足您的要求。在这里,我已经使用动态模板创建并映射了索引,然后执行了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)))))));
                }

谢谢

穆克什(Mukesh Raghuwanshi)

2020-06-22