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

后端 未结 3 1492
执笔经年
执笔经年 2020-12-19 11:01

I am looking for ElasticSearch nest query which will provide exact match on string having spaces in it using C#.

for example - I want to search for a word like \'XYZ

相关标签:
3条回答
  • 2020-12-19 11:09

    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() { });
    
    0 讨论(0)
  • 2020-12-19 11:13

    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

    0 讨论(0)
  • 2020-12-19 11:13

    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

    0 讨论(0)
提交回复
热议问题