An elastic search-search string containing spaces and special characters in it using C #

I am looking for an ElasticSearch nest query that will ensure an exact match of a string containing spaces in it using C #.

for example - I want to find a word, for example, "XYZ Company Solutions". I tried the query with the query, but it gives me all the records regardless of the search result. I also read in the post and found that we need to add some mappings for the field. I tried the "Not_Analyzed" analyzer on the field, but still it did not work.

Here is my C # code

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 has a complete example or steps in C #, can you share with me?

+5
source share
3 answers

Please refer to the code below, I think it will meet your requirements. Here I created and mapped the index to a dynamic template, and then made 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 Raghuvanshi

+2
source

Have you tried match_phrase?

The DSL request request is as follows:

 "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 out the official documentation for more information:

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

+1
source

It looks like you just need to update the new index after the reindexing operation.

Using the sample code (and your first query for terms), I saw the same result - 0 hits.

Adding the following Refresh call after a call to reindex.Subscribe() single hit:

 objElasticClient.Refresh(new RefreshRequest() { }); 
0
source

All Articles