# Elasticsearch NEST query not behaving like the equivalent query in Kibana Dev Tools

**URL:** <https://discuss.elastic.co/t/elasticsearch-nest-query-not-behaving-like-the-equivalent-query-in-kibana-dev-tools/261867>\
**Category:** Elasticsearch\
**Created:** [January 22, 2021, 4:41am UTC](https://discuss.elastic.co/t/elasticsearch-nest-query-not-behaving-like-the-equivalent-query-in-kibana-dev-tools/261867 "2021-01-22T04:41:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonsagara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonsagara/32/26170_2.png) [@jonsagara](https://discuss.elastic.co/u/jonsagara)\
**Post date:** [January 22, 2021, 4:41am UTC](https://discuss.elastic.co/t/elasticsearch-nest-query-not-behaving-like-the-equivalent-query-in-kibana-dev-tools/261867/1 "2021-01-22T04:41:58Z")

</div>

I'm trying to construct a query against my index that will use `function_score` to boost records where fields have certain values. In Kibana Dev Tools, I have the following query that returns 3 hits as expected:

```auto
GET /myindex/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query": "somename",
          "fields": ["licenseName", "businessName"]
        }
      }
    }
  }
}

```

But when I try to reproduce this with `NEST`, no filtering happens, and it just returns all records in the index. To me this looks equivalent, but I must be missing something:

```auto
var byNameSearchResult = await _elasticClient.SearchAsync<MyModel>(sr => sr
    .Index("myindex")
    .Query(qcd => qcd
        .FunctionScore(fsqd => fsqd
            .Query(fsqcd => fsqcd
                .MultiMatch(mmqd => mmqd
                    .Query(message.Name)
                    .Fields(fd => fd
                        .Field(f => f.LicenseName)
                        .Field(f => f.BusinessName)
                    )
                )
            )
        )
    )
);

```

Any ideas on what I'm missing in the `NEST` query that would make it function like the raw query in Dev Tools?

---

<div class="post-metadata">

**Author:** ![jonsagara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonsagara/32/26170_2.png) [@jonsagara](https://discuss.elastic.co/u/jonsagara)\
**Post date:** [January 22, 2021, 5:05pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-query-not-behaving-like-the-equivalent-query-in-kibana-dev-tools/261867/2 "2021-01-22T17:05:13Z")

</div>

I think I found the answer. From the docs for [function\_score](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html):

> To use function\_score, the user has to define a query and one or more functions, that compute a new score for each document returned by the query.

So, unlike my manual query in Dev Tools, I added a single function to the `NEST` query:

```csharp
var byNameSearchResult = await _elasticClient.SearchAsync<MyModel>(sr => sr
    .Index("myindex")
    .Query(qcd => qcd
        .FunctionScore(fsqd => fsqd
            .Query(fsqcd => fsqcd
                .MultiMatch(mmqd => mmqd
                    .Query(message.Name)
                    .Fields(fd => fd
                        .Field(f => f.LicenseName)
                        .Field(f => f.BusinessName)
                    )
                )
            )
            .Functions(sfd => sfd
                .Weight(wfd => wfd
                    .Filter(wfqcd => wfqcd
                        .Term(tqd => tqd.Field(tqd => tqd.AccountId).Value(accountId))
                        )
                    .Weight(100)
                    )
                )
            )
        )
);

```

And I got the query and results I was expecting:

```auto
Valid NEST response built from a successful (200) low level call on POST: /myindex/_search?pretty=true&error_trace=true&typed_keys=true
# Audit trail of this API call:
 - [1] HealthyResponse: Node: http://192.168.100.2:9200/ Took: 00:00:00.1412548
# Request:
{"query":{"function_score":{"functions":[{"filter":{"term":{"accountId":{"value":"9978f652-700a-4f63-8c71-ea4a11e6ddc9"}}},"weight":100.0}],"query":{"multi_match":{"fields":["licenseName","businessName"],"query":"somename"}}}}}
# Response:
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 10.921473,
    "hits" : [
      // 3 records
    ]
  }
}

# TCP states:
  Established: 82
  CloseWait: 14
  TimeWait: 5

# ThreadPool statistics:
  Worker: 
    Busy: 1
    Free: 32766
    Min: 8
    Max: 32767
  IOCP: 
    Busy: 1
    Free: 999
    Min: 8
    Max: 1000

```

---

<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:** [February 19, 2021, 5:05pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-query-not-behaving-like-the-equivalent-query-in-kibana-dev-tools/261867/3 "2021-02-19T17:05:25Z")

</div>

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