2

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"

Does anyone have a complete example or steps in C#?

Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
Mukesh
  • 422
  • 2
  • 4
  • 9
  • Can you show us what the mapping for the index looks like? The first two c# queries generate the exact same elastic query. Are you sure your property in the index is called "Name", and not "name"? Elastic is case sensitive... – samjudson May 01 '15 at 09:34
  • http://nest.azurewebsites.net/nest/indices/put-mapping.html – Mukesh May 04 '15 at 05:30
  • yes I am sure that my property in the index called "Name". – Mukesh May 04 '15 at 05:31
  • See my answer on the duplicate question: http://stackoverflow.com/a/30022660/150568. You need to make a `Refresh` call against the new index after the reindexing operation. – Rick Haffey May 05 '15 at 11:30
  • I have tried with this also but not getting any result. Thanks – Mukesh May 05 '15 at 13:29
  • Do you get results back when running the query _outside_ of NEST? `POST /{elastic_newindexname}/_search { "query": { "term": { "Name": { "value": "XYZ Company Solutions" } } } }` – Rick Haffey May 05 '15 at 14:51
  • No I am not getting result there also with term operator but with query string I am getting results but not exact match. – Mukesh May 06 '15 at 09:25
  • This one worked for me- IIndicesOperationResponse result = null; if (!objElasticClient.IndexExists(elastic_indexname).Exists) { result = objElasticClient.CreateIndex(elastic_indexname, c => c.AddMapping(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))))))); } – Mukesh May 13 '15 at 06:19
  • I have created and mapped index using dynamic template and then did XDCR.So it worked for me. while adding mapping Type "default" is suffixed and prefixed with underscore i am not able to mention here. Thanks Mukesh – Mukesh May 13 '15 at 06:23
  • 1
    This is answered here http://stackoverflow.com/questions/29960168/elastic-search-search-string-having-spaces-and-special-characters-in-it-using-c – Diboliya May 13 '15 at 06:51

0 Answers0