# Modify SearchDescriptor to Add field\_value\_factor using .NEST

**URL:** <https://discuss.elastic.co/t/modify-searchdescriptor-to-add-field-value-factor-using-nest/102483>\
**Category:** Elasticsearch\
**Created:** [October 2, 2017, 8:25pm UTC](https://discuss.elastic.co/t/modify-searchdescriptor-to-add-field-value-factor-using-nest/102483 "2017-10-02T20:25:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghohenstatt](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@ghohenstatt](https://discuss.elastic.co/u/ghohenstatt)\
**Post date:** [October 2, 2017, 8:25pm UTC](https://discuss.elastic.co/t/modify-searchdescriptor-to-add-field-value-factor-using-nest/102483/1 "2017-10-02T20:25:41Z")

</div>

I need to revise a method that builds a SearchDescriptor using .Nest so that the score is higher for  
product search results for items having a contract price (value of zero).

I captured the serialized version of the query added "field\_value\_factor" to return the results in the desired order. I have not determined how to achieve this in the .Nest query statement.

Can someone recommend how to revise the .NEST client statements to produce the same search descriptor?

Thank you

Below is the query we want to achieve where you will see field\_value\_factor at the bottom:

```auto
{
	  "from": 0,
  "size": 3000,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "priceStatus": {
        "order": "asc"
      }
    },
    {
      "unitPrice": {
        "order": "asc"
      }
    }
  ],
  "aggs": {
    "PriceStatus": {
      "terms": {
        "field": "priceStatus",
        "size": 5
      }
    },
    "VendorName": {
      "terms": {
        "field": "vendorName",
        "size": 5
      }
    },
    "CatalogName": {
      "terms": {
        "field": "catalogName",
        "size": 5
      }
    },
    "ManufacturerName": {
      "terms": {
        "field": "manufacturerName",
        "size": 5
      }
    },
    "IsGreen": {
      "terms": {
        "field": "isGreen",
        "size": 5
      }
    },
    "IsValuePack": {
      "terms": {
        "field": "isValuePack",
        "size": 5
      }
    },
    "UnitOfMeasure": {
      "terms": {
        "field": "unitOfMeasure",
        "size": 5
      }
    },
    "Attributes": {
      "nested": {
        "path": "attributes"
      },
      "aggs": {
        "TheAttributeName": {
          "terms": {
            "field": "attributes.name",
            "size": 10
          },
          "aggs": {
            "TheAttributeValue": {
              "terms": {
                "field": "attributes.value",
                "size": 5
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "function_score": { 
       "query": {
        "bool": {
          "should": [
            {
              "multi_match": {
                "type": "phrase",
                "query": "pen",
                "slop": 3,
                "boost": 16.0,
                "fields": [
                  "itemNumber*^4",
                  "shortDescription*^4",
                  "subCategory1Name*^1.5",
                  "subCategory2Name*^2.0",
                  "categoryName*^0.9",
                  "longDescription*^0.6",
                  "catalogName*^0.30",
                  "manufactureName*^0.20",
                  "vendorName*^0.15",
                  "upcCode*^0.10"
                ]
              }
            },
            {
              "multi_match": {
                "query": "pen",
                "boost": 15.0,
                "minimum_should_match": "75%",
                "fields": [
                  "itemNumber*^4",
                  "shortDescription*^4",
                  "subCategory1Name*^1.5",
                  "subCategory2Name*^2.0",
                  "categoryName*^0.9",
                  "longDescription*^0.6",
                  "catalogName*^0.30",
                  "manufactureName*^0.20",
                  "vendorName*^0.15",
                  "upcCode*^0.10"
                ]
              }
            },
            {
              "multi_match": {
                "query": "pen",
                "fuzziness": 1.0,
                "slop": 2,
                "minimum_should_match": "75%",
                "fields": [
                  "itemNumber*^4",
                  "shortDescription*^4",
                  "subCategory1Name*^1.5",
                  "subCategory2Name*^2.0",
                  "categoryName*^0.9",
                  "longDescription*^0.6",
                  "catalogName*^0.30",
                  "manufactureName*^0.20",
                  "vendorName*^0.15",
                  "upcCode*^0.10"
                ]
              }
            }
          ]
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "catalogId": [
                  "fbb3dd2c-f81c-4ff3-bd5b-9c2cffc51540"
                ]
              }
            }
          ]
        }
      },
            "field_value_factor": {
                "field": "priceStatus",
                "factor": -1,
                "modifier": "none"
            }
        }
    }
}

```

Below is the current method that builds the SearchDescriptor:

```auto

        private SearchDescriptor<SearchItem> BuildSearchDescriptor(
            string searchTerm,
            IList<Guid> catalogIds,
            int from,
            int size,
            string index,
            string preference,
            int attrSize,
            int valueSize,
            Dictionary<string, string[]> filterProps,
            Dictionary<string, string[]> filterAttrs,
            Guid? categoryId)
        {
            var searchDescriptor = new SearchDescriptor<SearchItem>()
                .From(from)
                .Size(size)
                .Query(q =>
                    q.Filtered(fd => BuildFilterTerms(fd, filterProps, filterAttrs, catalogIds, categoryId)
                        .Query(iq => BuildQueryContainer(iq, searchTerm))
                    )                    
                )
                .Index(index)
                .Preference(preference)
                .Aggregations(agg => BuildAggregationDescriptor(agg, attrSize, valueSize, catalogIds.Count))          
                .Sort(sort => sort.OnField("_score").Descending())
                .SortAscending(p=> p.PriceStatus)
                .SortAscending(p => p.UnitPrice);

            // Debug the raw string that will post to the ES servers i.e. use this in postman
            //var str = System.Text.Encoding.UTF8.GetString(client.Serializer.Serialize(searchDescriptor));

            return searchDescriptor;
        }

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [October 6, 2017, 7:14am UTC](https://discuss.elastic.co/t/modify-searchdescriptor-to-add-field-value-factor-using-nest/102483/2 "2017-10-06T07:14:32Z")

</div>

[I answered the same question on StackOverflow](https://stackoverflow.com/questions/46544464/elastic-nest-equivelent-of-field-value-factor/46595656#46595656). Including here for posterity.

Your JSON query isn't valid; `field_value_factor` is a function of a `function_score` query. In NEST 1.x, this would look like

```
var response = client.Search<Document>(x => x
    .Query(q => q
        .FunctionScore(fs => fs
            .Functions(fu => fu
                .FieldValueFactor(fvf => fvf
                    .Field(f => f.PriceStatus)
                    .Factor(-1)
                    .Modifier(FieldValueFactorModifier.None)
                )
            )
        )
    )
);

public class Document
{
    public string Title { get; set; }

    public int PriceStatus { get; set; }
}

```

which produces the query

```
{
  "query": {
    "function_score": {
      "functions": [
        {
          "field_value_factor": {
            "field": "priceStatus",
            "factor": -1.0,
            "modifier": "none"
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 3, 2017, 7:14am UTC](https://discuss.elastic.co/t/modify-searchdescriptor-to-add-field-value-factor-using-nest/102483/3 "2017-11-03T07:14:49Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
