Elastic Search-Search string having spaces and special characters in it using C#

孤者浪人 提交于 2019-11-29 10:50:55

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

Thanks

Mukesh Raghuwanshi

Have you tried the match_phrase query?

The query DSL the request is the following:

"query": {
    "match_phrase": {
        "title": "XYZ Company Solutions"
    }
}

In C# try the following:

_client.Search<T>(s => s
    .Index(IndexName)
    .Types(typeof (T))
    .Query(q => q.MatchPhrase(m => m
        .OnField(f => f.Name)
        .Query("XYZ Company Solutions"))));

Check the official documentation for more information:

http://www.elastic.co/guide/en/elasticsearch/guide/master/phrase-matching.html#phrase-matching

It looks like you just need to refresh the new index following the re-indexing operation.

Using your code example (and your first term query), I was seeing the same result -- 0 hits.

Adding the following Refresh call after the reindex.Subscribe() call results in a single hit being returned:

objElasticClient.Refresh(new RefreshRequest() { });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!