title: C# NEST conditional filter for different types
body: <p>I have to write a query that has to return if my object has a parking or not. In the index the parking are stored as integer, to indicate how many parking places belong to the object. In the query the property is boolean, to indicate does we want or not to search if the object has parking places.</p>
  <p>I tried this with conditional query, but as I see now it is  wrong way.</p>
  <p>My question is, how to write the <code>HasParking()</code> function, where I can check if the field value is greater then 0, that indicates that the object has parking place(s).</p>
  <p>This is how I started:</p>
  <pre><code>public partial class ElasticSearchService
  {
      private List&lt;Func&lt;QueryContainerDescriptor&lt;AdvertisementObjectEntityExtended&gt;, QueryContainer&gt;&gt; filters = null;
  
      public async Task&lt;List&lt;AdvertisementObjectEntityExtended&gt;&gt; AdvancedSearchAsync(AdvancedFilter filter)
      {
              filters = new List&lt;Func&lt;QueryContainerDescriptor&lt;AdvertisementObjectEntityExtended&gt;, QueryContainer&gt;&gt;();
              
              string location = !string.IsNullOrEmpty(filter.LocationSuggestion.SubLocation) ? filter.LocationSuggestion.SubLocation : filter.LocationSuggestion.City;
  
              HasOrientation(filter.Orientations);
              HasStructure(filter.Structures);
              HasHeatingType(filter.HeatingTypes);
              HasDescription(filter.ObjectDescriptions);
              HasPropertyCondition(filter.PropertyConditions);
              HasBelongingSurface(filter.BelongingSurfaces);
  
              ISearchResponse&lt;AdvertisementObjectEntityExtended&gt; result = await Client.SearchAsync&lt;AdvertisementObjectEntityExtended&gt;(s =&gt; s
                 .Index(ElasticClientFactorySettings.AdvertisementObjectIndex)
                 .Query(q =&gt;
                      q.QueryString(qs =&gt; qs.DefaultField(f =&gt; f.Location.ToLower()).Query($&quot;{location.ToLower()}&quot;)) &amp;&amp;
                      q.MatchPhrase(x =&gt; x.Field(t =&gt; t.SubCategoryKey).Query(filter.Subcategory.Key)) &amp;&amp;
                      q.Term(t =&gt; t.IsRentable, filter.IsRentable) &amp;&amp;
                      q.Term(t =&gt; t.Garage, filter.Garage) &amp;&amp;
                      q.Term(t =&gt; t.WiFi, filter.WiFi) &amp;&amp;
                      q.Term(t =&gt; t.Elevator, filter.Elevator) &amp;&amp;
                      q.Term(t =&gt; t.FurnishState, filter.FurnishState) &amp;&amp;
                      q.Term(t =&gt; t.Deposit, filter.Deposit) &amp;&amp;
                      q.Term(t =&gt; t.Duplex, filter.Duplex) &amp;&amp;
                      q.Term(t =&gt; t.SeparateKitchen, filter.SeparateKitchen) &amp;&amp;
                      q.Term(t =&gt; t.LaundryAndDryingRoom, filter.LaundryAndDryingRoom) &amp;&amp;
                      q.Term(t =&gt; t.Basement, filter.Basement) &amp;&amp;
                      q.Term(t =&gt; t.Terrace, filter.Terrace) &amp;&amp;
                      q.Term(t =&gt; t.Balcony, filter.Balcony) &amp;&amp;
                      q.Term(t =&gt; t.Loggia, filter.Loggia) &amp;&amp;
                      q.Term(t =&gt; t.FrenchBalcony, filter.FrenchBalcony) &amp;&amp;
                      q.Term(t =&gt; t.EnergyPassport, filter.EnergyPassport) &amp;&amp;
                      q.Term(t =&gt; t.ImmediatelyHabitable, filter.ImmediatelyHabitable) &amp;&amp;
                      q.Term(t =&gt; t.VATRefount, filter.VATRefount) &amp;&amp;
                      q.Term(t =&gt; t.ExchangePossible, filter.ExchangePossible) &amp;&amp;
                      q.Term(t =&gt; t.Filed, filter.Filed) &amp;&amp;
                      q.Term(t =&gt; t.Mortgaged, filter.Mortgaged) &amp;&amp;
                      q.Term(t =&gt; t.Longue, filter.Longue) &amp;&amp;
                      q.Term(t =&gt; t.Wardrobe, filter.Wardrobe) &amp;&amp;
                      q.Term(t =&gt; t.Penthouse, filter.Penthouse) &amp;&amp;
                      q.Term(t =&gt; t.WarmWater, filter.WarmWater) &amp;&amp;
                      q.Term(t =&gt; t.Intercom, filter.Intercom) &amp;&amp;
                      q.Term(t =&gt; t.Climate, filter.Climate) &amp;&amp;
                      q.Term(t =&gt; t.Phone, filter.Phone) &amp;&amp;
                      q.Term(t =&gt; t.Alarm, filter.Alarm) &amp;&amp;
                      q.Term(t =&gt; t.Security, filter.Security) &amp;&amp;
                      q.Term(t =&gt; t.SecurityDoor, filter.SecurityDoor) &amp;&amp;
                      q.Term(t =&gt; t.VideosSurveillance, filter.VideosSurveillance) &amp;&amp;
                      q.Term(t =&gt; t.CableTV, filter.CableTV) &amp;&amp;
                      q.Term(t =&gt; t.Fireplace, filter.Fireplace) &amp;&amp;
                      q.Term(t =&gt; t.BestDeal, filter.IsBestDeal) &amp;&amp;
                      q.Range(r =&gt; r.Field(f =&gt; f.Price)
                       .GreaterThanOrEquals(filter.PriceFrom)
                       .LessThanOrEquals(filter.PriceTo)) &amp;&amp;
                      q.Range(r =&gt; r.Field(f =&gt; f.Quadrature)
                       .GreaterThanOrEquals(filter.QuadratureFrom)
                       .LessThanOrEquals(filter.QuadratureTo)) &amp;&amp;
                      q.Range(r =&gt; r.Field(f =&gt; f.Badrooms)
                       .GreaterThanOrEquals(filter.BadroomsFrom)
                       .LessThanOrEquals(filter.BadroomsTo)) &amp;&amp;
                      q.Bool(bq =&gt; bq.Filter(filters)))
                 .Sort(s =&gt;
                      s.Field(f =&gt; SortByFilter(f, filter.Sort))));
  
              return result.Documents.ToList();
      }
  
      private QueryBase HasParking(bool value)
      {
          return !value ? 
                     null :
                     +new TermQuery
                     {
                         Field = Infer.Field&lt;AdvertisementObjectEntityExtended&gt;(f =&gt; f.Parking),
                         Value = 1
                     };
       }
  
       private QueryBase HasShowcase3D()
       {
           throw new NotImplementedException();
       }
  
       private QueryBase HasImages()
       {
           throw new NotImplementedException();
       }
  
       private QueryBase HasVideoURL()
       {
           throw new NotImplementedException();
       }
  
       private void HasOrientation(AdvertisementObjectOrientation[] orientations)
       {
              if (orientations != null &amp;&amp; orientations.Any())
              {
                  filters.Add(fq =&gt; fq.Terms(t =&gt; t.Field(f =&gt; f.Orientation).Terms(orientations)));
              }
       }
  
       private void HasStructure(AdvertisementObjectStructure[] structures)
       {
              if (structures != null &amp;&amp; structures.Any())
              {
                  filters.Add(fq =&gt; fq.Terms(t =&gt; t.Field(f =&gt; f.Structure).Terms(structures)));
              }
       }
  
       private void HasHeatingType(AdvertisementObjectHeating[] heatingTypes)
       {
              if (heatingTypes != null &amp;&amp; heatingTypes.Any())
              {
                  filters.Add(fq =&gt; fq.Terms(t =&gt; t.Field(f =&gt; f.HeatingType).Terms(heatingTypes)));
              }
       }
  
       private void HasDescription(AdvertisementObjectDescription[] descriptions)
       {
              if (descriptions != null &amp;&amp; descriptions.Any())
              {
                  filters.Add(fq =&gt; fq.Terms(t =&gt; t.Field(f =&gt; f.ObjectDescription).Terms(descriptions)));
              }
       }
  
       private void HasPropertyCondition(AdvertisementObjectPropertyCondition[] propertyConditions)
       {
              if (propertyConditions != null &amp;&amp; propertyConditions.Any())
              {
                  filters.Add(fq =&gt; fq.Terms(t =&gt; t.Field(f =&gt; f.PropertyCondition).Terms(propertyConditions)));
              }
       }
  
       private void HasBelongingSurface(AdvertisementObjectBelongingSurfaces[] belongingSurfaces)
       {
              if (belongingSurfaces != null &amp;&amp; belongingSurfaces.Any())
              {
                  filters.Add(fq =&gt; fq.Terms(t =&gt; t.Field(f =&gt; f.BelongingSurfaces).Terms(belongingSurfaces)));
              }
       }
  }
  </code></pre>