Nest ElasticSearch添加条件条款

时间:2018-05-16 10:15:06

标签: elasticsearch nest

我正在构建以下查询,请注意,companyGroupId上的过滤似乎无法找到合适的方式来添加和条件Term

我想查看includeTerminationDate值并有条件地添加term

简而言之,如果false不返回具有终止日期的记录。

private Func<QueryContainerDescriptor<EmployeeDocument>, QueryContainer> EmployeeSearchQuery(string query, long companyGroupId, bool includeTerminationDate)
        {
            return q => q
                            .MultiMatch(m => m
                                .Query(query)

                                .Type(TextQueryType.MostFields)
                                .MinimumShouldMatch("90%")
                                .Fields(f => f
                                    .Field(ff => ff.FullName, 3)
                                    .Field(ff => ff.Number, 3)
                                    .Field(ff => ff.Email)
                                    .Field(ff => ff.JobNumber)
                                    .Field(ff => ff.Description))) && q.Term(f => f.CompanyGroupId, companyGroupId);
        }

为什么只是复制代码并根据条件添加Term。但我想继续DRY Principle。另一种方式是类似下面的东西,我似乎无法弄清楚。

return q => q
                            .MultiMatch(m => m
                                .Query(query)

                                .Type(TextQueryType.MostFields)
                                .MinimumShouldMatch("90%")
                                .Fields(f => f
                                    .Field(ff => ff.FullName, 3)
                                    .Field(ff => ff.Number, 3)
                                    .Field(ff => ff.Email)
                                    .Field(ff => ff.JobNumber)
                                    .Field(ff => ff.Description))) && q.Term(f => f.CompanyGroupId, companyGroupId)
                                                                     && !includeTerminationDate ? q.Term(f => f.TerminationDate, null) : 'Otherwise do not include term';

1 个答案:

答案 0 :(得分:1)

实现此目的的一种方法是将表达式转换为方法组,并有条件地添加查询

private Func<QueryContainerDescriptor<EmployeeDocument>, QueryContainer> EmployeeSearchQuery(string query, long companyGroupId, bool includeTerminationDate)
{
    return q =>
    {
        var qc = q.MultiMatch(m => m
                   .Query(query)
                   .Type(TextQueryType.MostFields)
                   .MinimumShouldMatch("90%")
                   .Fields(f => f
                       .Field(ff => ff.FullName, 3)
                       .Field(ff => ff.Number, 3)
                       .Field(ff => ff.Email)
                       .Field(ff => ff.JobNumber)
                       .Field(ff => ff.Description))) && 
                q.Term(f => f.CompanyGroupId, companyGroupId);

        // whatever your logic is for adding termination date
        if (includeTerminationDate)
            qc &= q.Term(f => f.TerminationDate, DateTime.UtcNow);

        return qc;
    };
}

然后使用

client.Search<EmployeeDocument>(s => s
    .Query(EmployeeSearchQuery("query", 2, false))
);

发射

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "type": "most_fields",
            "query": "query",
            "minimum_should_match": "90%",
            "fields": [
              "fullName^3",
              "number^3",
              "email",
              "jobNumber",
              "description"
            ]
          }
        },
        {
          "term": {
            "companyGroupId": {
              "value": 2
            }
          }
        }
      ]
    }
  }
}

client.Search<EmployeeDocument>(s => s
    .Query(EmployeeSearchQuery("query", 2, true))
);

发射

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "type": "most_fields",
            "query": "query",
            "minimum_should_match": "90%",
            "fields": [
              "fullName^3",
              "number^3",
              "email",
              "jobNumber",
              "description"
            ]
          }
        },
        {
          "term": {
            "companyGroupId": {
              "value": 2
            }
          }
        },
        {
          "term": {
            "terminationDate": {
              "value": "2018-05-16T23:21:16.8309753Z"
            }
          }
        }
      ]
    }
  }
}