NEST FunctionScore()在添加函数之前返回所有索引项,在添加函数之后抛出异常

本文关键字:函数 添加 索引 抛出异常 之后 FunctionScore NEST 返回 | 更新日期: 2023-09-27 18:04:20

好吧,所以查询工作完美的感觉在Chrome。我使用以下查询:

{
"size":127,
"query": {
    "function_score": {
        "query": {
            "bool": {
                "must": [
                    {
                        "prefix": {
                            "name": {
                                "value": "incomp"
                            }
                        }
                    },
                    {
                        "match": {
                            "name": "a word that is"
                        }
                    }
                ]
            }
        },
        "functions": [
            {
                "exp": {
                    "date": {
                        "origin": "now/d",
                        "scale": "3w",
                        "offset": "10d",
                        "decay": "0.88"
                    }
                }
            }
        ]
    }
}
}

简而言之,我匹配ES中自定义类型的索引"name"属性,优先考虑最近添加的项,并支持"键入建议"——因此是前缀查询。它工作得很好,调优了,所以我的下一步是在NEST中进行复制。

然而,我在下面的。net NEST代码中面临一些问题:

var results4 = _client.Search<customDataType>(
s => s.Size(5030)
.Query(q => q
    .FunctionScore(fs => fs
        .Name("another_named_query")
        .BoostMode(FunctionBoostMode.Multiply)
        .ScoreMode(FunctionScoreMode.Multiply)
        .Query(qu => qu
            .Bool(b => b
                .Must(m => m
                    .Prefix(p => p
                        .Field(ff => ff.Name)
                        .Value(prefixVal)))
                .Must(m2 => m2
                    .Match(mh => mh
                        .Field(f2 => f2.Name)
                        .Query(stringBeforePrefixVal)))))                                  
        /*.Functions( fcs => fcs.ExponentialDate(
            exp => exp
            .Origin(DateMath.Now)
            .Scale(new Time(1814400000))
            .Offset(new Time(864000000))
            .Decay(0.88d))
        )*/)));

我不明白为什么使用"FunctionScore"方法的任何尝试都会导致MatchAll()所做的事情-返回所有记录。

同时,当添加函数(如上所述)时,我得到一个在巢。fieldresolver的NullReference内部异常的unexpected delasticsearchclientexception。解析(Field Field)在C:'code'elasticsearch-net'src'Nest'CommonAbstractions'Infer'Field'FieldResolver.cs:line 31.

我对这一切感到困惑,似乎没有类似的问题,我可以作为一个起点。我是否可以做些什么来运行上面的查询,或者我是否应该手动执行restful API调用?

NEST FunctionScore()在添加函数之前返回所有索引项,在添加函数之后抛出异常

几乎正确,但是您错过了指数日期衰减函数应该运行的字段。假设您的POCO看起来像

public class customDataType
{
    public string Name { get; set; }
    public DateTime Date { get; set; }
}

查询应该是

var prefixVal = "incomp";
var stringBeforePrefixVal = "a word that is";
var results4 = client.Search<customDataType>(s => s
    .Size(5030)
    .Query(q => q
        .FunctionScore(fs => fs
            .Name("another_named_query")
            .BoostMode(FunctionBoostMode.Multiply)
            .ScoreMode(FunctionScoreMode.Multiply)
            .Query(qu => qu
                .Bool(b => b
                    .Must(m => m
                        .Prefix(p => p
                            .Field(ff => ff.Name)
                            .Value(prefixVal)))
                    .Must(m2 => m2
                        .Match(mh => mh
                            .Field(f2 => f2.Name)
                            .Query(stringBeforePrefixVal)))))
            .Functions(fcs => fcs
                .ExponentialDate(exp => exp
                    .Field(f => f.Date)
                    .Origin("now/d")
                    .Scale("3w")
                    .Offset("10d")
                    .Decay(0.88)
                )
            )
        )
    )
);
收益率

{
  "size": 5030,
  "query": {
    "function_score": {
      "_name": "another_named_query",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": {
                  "query": "a word that is"
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "exp": {
            "date": {
              "origin": "now/d",
              "scale": "3w",
              "offset": "10d",
              "decay": 0.88
            }
          }
        }
      ],
      "score_mode": "multiply",
      "boost_mode": "multiply"
    }
  }
}

您可以利用NEST中的运算符重载,通过将prefixmatch查询改为&&来进一步缩短bool查询

var results4 = client.Search<customDataType>(s => s
    .Size(5030)
    .Query(q => q
        .FunctionScore(fs => fs
            .Name("another_named_query")
            .BoostMode(FunctionBoostMode.Multiply)
            .ScoreMode(FunctionScoreMode.Multiply)
            .Query(qu => qu
                .Prefix(p => p
                    .Field(ff => ff.Name)
                    .Value(prefixVal)
                ) && qu  
                .Match(mh => mh
                    .Field(f2 => f2.Name)
                    .Query(stringBeforePrefixVal)
                )
            )
            .Functions(fcs => fcs
                .ExponentialDate(exp => exp
                    .Field(f => f.Date)
                    .Origin("now/d")
                    .Scale("3w")
                    .Offset("10d")
                    .Decay(0.88)
                )
            )
        )
    )
);