【问题标题】:Apply different filters after main MultiMatch query in Elastic Search NEST在 Elastic Search NEST 中的主 MultiMatch 查询后应用不同的过滤器
【发布时间】:2016-12-05 15:09:09
【问题描述】:

所以,这是我的查询:

_elasticClient.Search<SearchItem>(x =>
                x.Sort(sort).Size(itemsPerPage)
                .Query(q =>
                    q.MultiMatch(m => m
                    .Fields(fs => fs
                        .Field(p => p.Field1)
                        .Field(p => p.Field2)
                        .Field(p => p.Field3)
                        .Field(p => p.Field4)
                        .Field(p => p.Field5)
                        .Field(p => p.Field6)                            
                    )
                    .Operator(Operator.And)
                    .Query(pattern)
                )));

我必须应用不同的过滤器。范围过滤器(针对价格),过滤结果集,其中 field1 = "Audi" 和 field2 = "Sale Car"。我试图做这样的事情:

.Query(q =>
                    q.MultiMatch(m => m
                    .Fields(fs => fs
                        Field(p => p.Field1)
                            .Field(p => p.Field2)
                            .Field(p => p.Field3)
                            .Field(p => p.Field4)
                            .Field(p => p.Field5)
                            .Field(p => p.Field6)  
                    )
                    .Operator(Operator.And)
                    .Query(pattern)))
.Query(q=>q.Range(ra=>ra.Field(ff=>ff.SalePrice).GreaterThan(1000))));

但这不起作用。我已经从索引中获得了价格大于 1000 的所有结果,但只需要搜索结果。谁能帮帮我?

【问题讨论】:

标签: c# elasticsearch nest


【解决方案1】:

您可以 use a bool query to combine queries 并且 NEST 通过重载运算符以组合 QueryContainers(根查询类型)使这更容易使用。这是 NEST 2.x 的示例

void Main()
{
    var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var settings = new ConnectionSettings(connectionPool);

    var client = new ElasticClient(settings);

    var itemsPerPage = 20;
    var pattern = "match query";

    client.Search<SearchItem>(x => x
        .Sort(so => so
            .Descending("_score")
        )
        .Size(itemsPerPage)
        .Query(q => q
            .MultiMatch(m => m
                .Fields(fs => fs
                    .Field(p => p.Field1)
                    .Field(p => p.Field2)
                    .Field(p => p.Field3)
                    .Field(p => p.Field4)
                    .Field(p => p.Field5)
                    .Field(p => p.Field6)
                )
                .Operator(Operator.And)
                .Query(pattern)
            ) && q
            .Range(ra => ra
                .Field(ff=>ff.SalePrice)
                .GreaterThan(1000)
            )
        )
    );
}

public class SearchItem
{
    public int SalePrice { get; set; }

    public string Field1 { get; set; }

    public string Field2 { get; set; }

    public string Field3 { get; set; }

    public string Field4 { get; set; }

    public string Field5 { get; set; }

    public string Field6 { get; set; }
}

产生

{
  "size": 20,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "match query",
            "operator": "and",
            "fields": [
              "field1",
              "field2",
              "field3",
              "field4",
              "field5",
              "field6"
            ]
          }
        },
        {
          "range": {
            "salePrice": {
              "gt": 1000.0
            }
          }
        }
      ]
    }
  }
}

这将找到与 multi_match 查询匹配的文档并且也有一个大于 1000 的 salePrice。因为我们不需要为范围查询计算分数(a document 的 salePrice 大于 1000 或者没有),范围查询可以在过滤器上下文中运行。稍微精致的版本

client.Search<SearchItem>(x => x
    .Sort(so => so
        .Descending("_score")
    )
    .Size(itemsPerPage)
    .Query(q => q
        .MultiMatch(m => m
            .Fields(fs => fs
                .Field(p => p.Field1)
                .Field(p => p.Field2)
                .Field(p => p.Field3)
                .Field(p => p.Field4)
                .Field(p => p.Field5)
                .Field(p => p.Field6)
            )
            .Operator(Operator.And)
            .Query(pattern)
        ) && +q
        .Range(ra => ra
            .Field(ff=>ff.SalePrice)
            .GreaterThan(1000)
        )
    )
);

将一元 + 运算符附加到范围查询是布尔查询过滤器的简写。查询 json 现在看起来像

{
  "size": 20,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "match query",
            "operator": "and",
            "fields": [
              "field1",
              "field2",
              "field3",
              "field4",
              "field5",
              "field6"
            ]
          }
        }
      ],
      "filter": [
        {
          "range": {
            "salePrice": {
              "gt": 1000.0
            }
          }
        }
      ]
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2019-01-01
    • 2019-04-03
    • 2016-02-10
    • 1970-01-01
    相关资源
    最近更新 更多