使用C#在其中具有空格和特殊字符的Elastic Search-Search字符串
我正在寻找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#中有完整的示例或步骤,那么可以和我分享一下吗?
-
请参考下面的代码,我认为这将满足您的要求。在这里,我已经使用动态模板创建并映射了索引,然后执行了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)
-
在Elastic Search中对数组元素进行查询字符串搜索
2021-02-01 关注 0 浏览107 1答案
-
使用Elastic Search检索标签内容和带连字符的单词
2021-02-01 关注 0 浏览229 1答案
-
Elastic Search 5.x嵌套多个查询C#
2021-02-01 关注 0 浏览98 1答案
-
比较字符串,其中一个字符串之前有空格,而另一个字符串之前没有空格
2021-05-10 关注 0 浏览111 1答案
-
Elastic search 带空格的术语
2021-02-01 关注 0 浏览143 1答案
-
Python:按所有空格字符分割字符串
2021-01-29 关注 0 浏览203 1答案
-
若要求从键盘读入含有空格字符的字符串,应使用函数
2022-05-11 关注 0 浏览9 1答案
-
从没有空格的字符串中删除重复的子字符串
2022-07-28 关注 0 浏览18 1答案
-
删除字符串中的所有空格
2022-03-22 关注 0 浏览30 1答案
-
删除字符串中的所有空格
2021-02-01 关注 0 浏览93 1答案