Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 Company Solutions'. I tried querystring query but it gives me all the records irrespective of search result. Also i read on the post and found that we have to add some mappings for the field. I tried 'Not_Analyzed' analyzer on the field but still it does not worked.

Here is my code of 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"

If anyone have complete example or steps in C# then can you please share with me?

like image 748
Diboliya Avatar asked Oct 20 '22 12:10

Diboliya


2 Answers

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

like image 53
Manolis Avatar answered Oct 22 '22 10:10

Manolis


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

like image 24
Mukesh Avatar answered Oct 22 '22 08:10

Mukesh