title: C# NEST Elastic Search Query Multiple Conditions
body: <p>I'm doing some tests to change my architecture. We want to drop MongoDB and use ElasticSearch instead. But I dont really know this technology. I'm using NEST as driver and can't translate a query that I used to use in mongo.</p>
  
  <pre><code>public async Task&lt;IEnumerable&lt;Keyword&gt;&gt; GetKeywordsAsync(string prefix, int startIndex, int totalItems, int minimumTotalSearch, CancellationToken cancellationToken)
      {
          return await _mongoReader.GetEntitiesAsync&lt;KeywordEntity, Keyword&gt;(CollectionName,
                      queryable =&gt;
                          queryable.Where(entity =&gt; entity.KeywordName.StartsWith(prefix) &amp;&amp; entity.TotalSearch &gt;= minimumTotalSearch)
                                   .OrderBy(entity =&gt; entity.KeywordName)
                                   .Select(_keywordConverter.GetConverter())
                                   .Skip(startIndex)
                                   .Take(totalItems),
                      cancellationToken).ConfigureAwait(false);
      }
  
  public async Task&lt;IEnumerable&lt;TModel&gt;&gt; GetEntitiesAsync&lt;TDocument, TModel&gt;(string collectionName,
              Func&lt;IMongoQueryable&lt;TDocument&gt;, IMongoQueryable&lt;TModel&gt;&gt; getQueryable,
              CancellationToken cancellationToken)
          {
              var documents = GetDocuments&lt;TDocument&gt;(collectionName);
              var query = getQueryable(documents.AsQueryable());
              return await query.ToListAsync(cancellationToken).ConfigureAwait(false);
          }
  </code></pre>
  
  <p>And here is a simple find that I made for ElasticSearch :</p>
  
  <pre><code>public async Task&lt;IEnumerable&lt;TModel&gt;&gt; FindAsync&lt;TModel, TValue&gt;(string index,
          Expression&lt;Func&lt;TModel, TValue&gt;&gt; findExpression, TValue value, int limit,
          CancellationToken cancellationToken) where TModel : class
      {
          var searchRequest = new SearchRequest&lt;TModel&gt;(index)
          {
              Query =
                  Query&lt;TModel&gt;.Match(
                      a =&gt; a.Field(findExpression).Query(string.Format(CultureInfo.InvariantCulture, "{0}", value))),
              Size = limit
          };
  
          var resGet = await _elasticClientFactory.Create().SearchAsync&lt;TModel&gt;(searchRequest, cancellationToken).ConfigureAwait(false);
  
          return resGet?.Documents;
      }
  </code></pre>
  
  <p>The problem is I can't translate my Query Mongo in Elastic ...</p>
  
  <p>It was painfull but here is the elastic Query :</p>
  
  <pre><code>{
    "query": {
      "bool": {
        "must": [
          {"range" : { "totalSearch" : { "gte" : minimumTotalSearch }}},
          {"prefix": { "keywordName": prefix}}
        ]
      }
    },
    "from": startIndex,
    "size": totalItems
  }
  </code></pre>
  
  <p>--> Solution :
  After some struggle-coding I found a way to do the query in C# :</p>
  
  <pre><code>var result =
              ecf.Create()
                  .Search&lt;KeywordEntity&gt;(
                      a =&gt; a.Query(
                          z =&gt;
                              z.Bool(
                                  e =&gt;
                                      e.Must(r =&gt; r.Range(t =&gt; t.Field(y =&gt; y.TotalSearch).GreaterThanOrEquals(minimumTotalSearch)),
                                          t =&gt; t.Prefix(y =&gt; y.KeywordName, prefix)))).Index("keywords"));
  </code></pre>
  
  <p>But now I'm asking myself if this is the best way to do this query (without skip/take which is quite easy). As I'm new there is maybe a more optimized query ...</p>