【问题标题】:Using nested objects within query hits multiple indices in Nest ElasticSearch在查询中使用嵌套对象会命中 Nest ElasticSearch 中的多个索引
【发布时间】:2017-12-27 11:50:41
【问题描述】:

我在 productaccount 索引中有两种类型 producttypeaccounttype,我需要构建一个搜索查询以同时命中它们。

现在我得到了以下查询:

var searchResponse = elasticClient.Search<object>(s => s
            .Index(indices)
            .Type(Types.Type(typeof(ProductType), typeof(accountType)))
            .Query(q => q
                 q.Nested(n => n
                   .Path(Infer.Field<ProductType>(ff => ff.Keywords))
                   .Query(nq => nq
                       .Match(t => t
                         .Field(Infer.Field<ProductType>(ff => ff.Keywords.First().Keyword))
                         .Query(query)
                       )
                       ||
                       nq.Term(Infer.Field<ProductType>(ff => ff.Keywords.First().Keyword.Suffix("keyword")), query)
                   )
                 )
                && 
                +q.Term("_type", "producttype") 
                || 
                q.MultiMatch(m => m
                    .Fields(f => f
                        .Field(Infer.Field<accountType>(ff => ff.AccountName, 1.5))
                        .Field(Infer.Field<accountType>(ff => ff.Description, 0.8))
                    )
                    .Operator(Operator.Or)
                    .Query(query)
                ) && 
                +q.Term("_type", "accounttype")
            )
        );

当我运行此查询时,它不起作用,因为在 accounttype 中找不到关键字嵌套对象(但在我的情况下,我按 _type 过滤,所以它应该可以工作)。

那么当我在一个索引中嵌套对象时,如何按_index/_type 进行过滤?

【问题讨论】:

    标签: nest


    【解决方案1】:

    尝试在嵌套对象中添加 .IgnoreUnmapped(true)。

    var searchResponse = elasticClient.Search<object>(s => s
                .Index(indices)
                .Type(Types.Type(typeof(ProductType), typeof(accountType)))
                .Query(q => q
                     q.Nested(n => n
                       .IgnoreUnmapped(true)
                       .Path(Infer.Field<ProductType>(ff => ff.Keywords))
                       .Query(nq => nq
                           .Match(t => t
                             .Field(Infer.Field<ProductType>(ff => ff.Keywords.First().Keyword))
                             .Query(query)
                           )
                           ||
                           nq.Term(Infer.Field<ProductType>(ff => ff.Keywords.First().Keyword.Suffix("keyword")), query)
                       )
                     )
                    && 
                    +q.Term("_type", "producttype") 
                    || 
                    q.MultiMatch(m => m
                        .Fields(f => f
                            .Field(Infer.Field<accountType>(ff => ff.AccountName, 1.5))
                            .Field(Infer.Field<accountType>(ff => ff.Description, 0.8))
                        )
                        .Operator(Operator.Or)
                        .Query(query)
                    ) && 
                    +q.Term("_type", "accounttype")
                )
            );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多