Elasticsearch NEST 查询的行为与 Kibana Dev Tools 中的等效查询不同

问题描述

我正在尝试针对我的索引构建一个查询,该查询将使用 function_score 来提升字段具有特定值的记录。在 Kibana Dev Tools 中,我有以下查询按预期返回 3 个命中:

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

但是当我尝试用 nesT 重现这个时,没有过滤发生,它只返回索引中的所有记录。对我来说,这看起来是等价的,但我一定遗漏了一些东西:

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)
                    )
                )
            )
        )
    )
);

预计到达时间:这是 Debuginformation 输出

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.3650673
# Request:
{}
# Response:
{
  "took" : 9,"timed_out" : false,"_shards" : {
    "total" : 2,"successful" : 2,"skipped" : 0,"Failed" : 0
  },"hits" : {
    "total" : {
      "value" : 10000,"relation" : "gte"
    },"max_score" : 1.0,"hits" : [
      // 10 records here
    ]
  }
}

# TCP states:
  Established: 78
  CloseWait: 14
  TimeWait: 12

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

关于 nesT 查询中我遗漏的内容有什么想法可以使它像开发工具中的原始查询一样运行吗?

解决方法

我想我找到了答案。来自 function_score 的文档:

要使用 function_score,用户必须定义​​一个查询和一个或多个函数,为查询返回的每个文档计算一个新的分数。

因此,与我在开发工具中的手动查询不同,我向 NEST 查询添加了一个函数:

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)
                    )
                )
            )
        )
);

我得到了我期待的查询和结果:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...