Search code examples
elasticsearchnest

Convert Elasticsearch Query to .net NEST


How do I write this elasticsearch match query to C# .net NEST.

 GET /disney2/character/_search
     {
     "query": {
     "match": {
     "name.phonetic": {
     "query": "Jahnnie Smeeth",
    "operator": "and"
     }
     }
     }
     } 

Solution

  • Using objecting initializer syntax:

            ConnectionSettings _clientSettings = new ConnectionSettings(new Uri("http://localhost:9200/disney2"));
            var client = new ElasticClient(_clientSettings);
            var query = new SearchRequest<disney>(); // You're going to want to replace <object> with a model corresponding to your type, or specify the type via string
            var matcher = new MatchQuery();
            matcher.Field = "name.phonetic";
            matcher.Query = "Jahnnie Smith";
            matcher.Operator = Operator.And;
            query.Query = matcher;
    
            //var res = elasticClient.Search<disney>(query);
            var res = client.Search<disney>(query);