简体   繁体   中英

Value in list of values exists in elastic field list nest c#

Could not solve the issue with elastic.

Given:

  1. List with colors: var colors_to_filter = ["red", "blue", "black"];
  2. Field colors in elasticsearch: colors: ["green", "red", "black"]

Task:

Get all products from elasticsearch with colors which has been defined in colors_to_filter list. If any of the colors in colors_to_filter exists in elasticsearch colors field, return this product.

My current code which is not working: (Take a look at this line):
.Bool(bl => bl.Filter(fl => fl.Terms( tr => tr.Field(fs => fs.Sizes.Suffix("keyword")).Terms(sizesList))

  .Aggregations(a => a.Terms(sizesAggName, tt => tt.Field(o => o.Sizes.Suffix("keyword")))
                                .Max(priceAggNameMax, st => st.Field(o => o.SalePrice)))
  .TrackTotalHits()
  .Sort(p => GetSortType(sortType))
  .Index(GetIndexName())
  .From(from)
  .Size(size)
  .Query(q => q.Bool(b => GetQuery(mainCategory, subCategory, subSubcategory, term)))
  .PostFilter( ppf => ppf
  .Bool(bl => bl.Filter(fl => fl.Terms( tr => tr.Field(fs => fs.Sizes.Suffix("keyword")).Terms(sizesList))
  && ppf.Range(r => {
               r = r.Field(f => f.SalePrice);
               if (minPrice > 0) r = r.GreaterThanOrEquals((double)minPrice);
               if (maxPrice > 0) r = r.LessThanOrEquals((double)maxPrice);
               return r;
  })))));

I have no idea about elasticsearch. However the requirements seems clear:

First filter wich elements Color are in colors_to_filter . Then filter that results again by wich colors are also in colors . Only things that survive both filterings, are in the final result.

An optimsiation would be to cut those two lists down to one with all colors that are in both (wich would be "red" and "black"). Then use that amalgm array as filter. I am not sure wich kind of join this was again.

I am guessing elastisearch is supposed to do that optimisation automagically and this is a example to shows that to you? The exact order or even a preparatory join of two similar filters for performance? That is the only thing I could think being "elastic" with any search/filtering.

Working code:

.Aggregations(a => a.Terms(sizesAggName, tt => tt.Field(o => o.Sizes.Suffix("keyword")))
                                .Max(priceAggNameMax, st => st.Field(o => o.SalePrice)))
  .TrackTotalHits()
  .Sort(p => GetSortType(sortType))
  .Index(GetIndexName())
  .From(from)
  .Size(size)
  .Query(q => q.Bool(b => GetQuery(mainCategory, subCategory, subSubcategory, term)))
  .PostFilter( ppf => ppf
  .Terms(tr =>
         {
             var tt = tr.Field(fs => fs.Sizes.Suffix("keyword"));
             return sizesList.Lenght() > 0 ? tt.Terms(selectedSizes) : tt;
         })
  && ppf.Range(r => {
             r = r.Field(f => f.SalePrice);
             if (minPrice > 0) r = r.GreaterThanOrEquals((double)minPrice);
             if (maxPrice > 0) r = r.LessThanOrEquals((double)maxPrice);
             return r;
  })));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM