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

676 views
Skip to first unread message

RAJEEV DIBOLIYA

unread,
May 7, 2015, 1:42:32 AM5/7/15
to couc...@googlegroups.com

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?

panchal neel

unread,
May 7, 2015, 12:46:13 PM5/7/15
to couc...@googlegroups.com

I dont know in C# but i have solve this issue in  java script..

You can use match and in match you can give operator : or so it will search any of the keyword.

I have tried it and woking perfectly.

--
You received this message because you are subscribed to the Google Groups "Couchbase" group.
To unsubscribe from this group and stop receiving emails from it, send an email to couchbase+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages