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

发布于 2021-02-01 11:59:31

我正在寻找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#中有完整的示例或步骤,那么可以和我分享一下吗?

关注者
0
被浏览
160
1 个回答
  • 面试哥
    面试哥 2021-02-01
    为面试而生,有面试问题,就找面试哥。

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



推荐阅读
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看