过滤器创建

时间:2018-02-26 09:55:39

标签: elasticsearch nest

经过多次实验后,我发现根据规格筛选产品所需的语法如下:

{

"post_filter": {
    "bool": {
        "filter": [{
                "nested": {
                    "path": "productSpecification",
                    "query": {
                        "bool": {
                            "filter": [{
                                    "term": {
                                        "productSpecification.name": "Brand"
                                    }
                                },
                                {
                                    "terms": {
                                        "productSpecification.value": [
                                            "Brand1"
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            {
                "nested": {
                    "path": "productSpecification",
                    "query": {
                        "bool": {
                            "filter": [{
                                    "term": {
                                        "productSpecification.name": "Guarantee"
                                    }
                                },
                                {
                                    "terms": {
                                        "productSpecification.value": [
                                            "3 years"
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

}

我现在正在尝试创建一个QueryContainer函数来根据所选的名称/值来构建它。如果有人能给我一个正确的方向,那将非常感激。 感谢

1 个答案:

答案 0 :(得分:0)

好的我现在正在构建一个来自nest的查询,但是比上面的DSL查询有点麻烦(不确定是否有人可以发现我在下面做的事情,这使得它有一些不必要的bool过滤器

这是我的嵌套语法

.PostFilter(pf => FilterTest(elasticParams,pf))

private static QueryContainer FilterTest(ElasticParams elasticParams, QueryContainerDescriptor<Product>q) => q
        .Bool(b => b
            .Filter(fi => FilterSelected(elasticParams, fi)));

private static QueryContainer FilterSelected2(ElasticParams elasticParams, QueryContainerDescriptor<Product> q) =>
        elasticParams.Filters.Aggregate(new QueryContainer(), (c, s) => c && +q.Nested(n => n
            .Path(p => p.ProductSpecification)
            .Query(qq => qq
                .Bool(bo => bo
                    .Filter(fi => fi
                        .Term(tt => tt
                            .Field(a => a.ProductSpecification.Suffix("name"))
                            .Value(s.name)) && fi
                        .Terms(ttt => ttt
                            .Field(fff => fff.ProductSpecification.Suffix("value"))
                            .Terms(s.Values))

                )))));

如前所述,这是我想要的过滤器,但它输出的DSL相当难看,如下所示

{

"post_filter": {
    "bool": {
        "filter": [{
            "bool": {
                "filter": [{
                    "nested": {
                        "query": {
                            "bool": {
                                "filter": [{
                                    "bool": {
                                        "must": [{
                                            "term": {
                                                "productSpecification.name": {
                                                    "value": "Brand"
                                                }
                                            }
                                        }, {
                                            "terms": {
                                                "productSpecification.value": ["Brand1", "Brand2"]
                                            }
                                        }]
                                    }
                                }]
                            }
                        },
                        "path": "productSpecification"
                    }
                }, {
                    "nested": {
                        "query": {
                            "bool": {
                                "filter": [{
                                    "bool": {
                                        "must": [{
                                            "term": {
                                                "productSpecification.name": {
                                                    "value": "Guarantee"
                                                }
                                            }
                                        }, {
                                            "terms": {
                                                "productSpecification.value": ["3 years"]
                                            }
                                        }]
                                    }
                                }]
                            }
                        },
                        "path": "productSpecification"
                    }
                }]
            }
        }]
    }
}

}