【问题标题】:Elasticsearch need to search from multiple types with type specific where clauseElasticsearch 需要从具有特定类型 where 子句的多种类型中进行搜索
【发布时间】:2017-02-08 14:32:08
【问题描述】:

我有多种类型的 Es 索引,每种类型都有自己的过滤器参数。现在我们正在为多种类型构建 Es 上的全局搜索,我有点困惑如何使用类型特定的 where 子句包含在 NEST 中。 弹性搜索 -> 类型 1(其中 x=1) -> 类型 2(其中 y=1)

现在我们正在构建一个搜索查询

var result = client.Search<ISearchDto>(s => s
                .From(from)
                .Size(PageSize)
                .Types(lstTypes)
                .Query(q => q.QueryString(qs => qs.Query(query)))
                );

*lstTypes 将具有类型 1 和类型 2

现在如何在 NEST 中为所有 x=1 的类型 1 项目和 y=1 的所有类型 2 项目添加 where 子句。

希望问题很清楚,对此的任何帮助将不胜感激。

【问题讨论】:

    标签: search elasticsearch nest


    【解决方案1】:

    您可以像查询任何其他字段一样查询_type 元字段。要在一个搜索查询中根据类型执行不同的查询,您可以使用带有多个子句的 bool 查询

    client.Search<ISearchDto>(s => s
        .From(from)
        .Size(pageSize)
        .Type(Types.Type(typeof(FirstSearchDto), typeof(SecondSearchDto)))
        .Query(q => q
            .Bool(b => b
                .Should(sh => sh
                    .Bool(bb => bb
                        .Filter(
                            fi => fi.Term("_type", "firstSearchDto"),
                            fi => fi.Term(f => f.X, 1)
                        )
                    ), sh => sh
                    .Bool(bb => bb
                        .Filter(
                            fi => fi.Term("_type", "secondSearchDto"),
                            fi => fi.Term(f => f.Y, 1)
                        )
                    )
                )
            )
        )
    );
    

    我们有一个带有 2 个 should 子句的 bool 查询;每个should 子句都是一个bool 查询,由2 个filter 子句组合而成,一个用于_type,另一个用于每个类型要查询的属性。

    NEST 支持运算符重载,因此可以更简洁地编写此查询

    client.Search<ISearchDto>(s => s
        .From(from)
        .Size(pageSize)
        .Type(Types.Type(typeof(FirstSearchDto), typeof(SecondSearchDto)))
        .Query(q => (+q
            .Term("_type", "firstSearchDto") && +q
            .Term(f => f.X, 1)) || (+q
            .Term("_type", "secondSearchDto") && +q
            .Term(f => f.Y, 1))
        )
    );
    

    两者都产生以下查询

    {
      "from": 0,
      "size": 20,
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "filter": [
                  {
                    "term": {
                      "_type": {
                        "value": "firstSearchDto"
                      }
                    }
                  },
                  {
                    "term": {
                      "x": {
                        "value": 1
                      }
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "filter": [
                  {
                    "term": {
                      "_type": {
                        "value": "secondSearchDto"
                      }
                    }
                  },
                  {
                    "term": {
                      "y": {
                        "value": 1
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多