gpt4 book ai didi

elasticsearch - 如何在elasticSearch中清除(取消选中过滤器)过滤的查询

转载 作者:行者123 更新时间:2023-12-03 00:28:32 24 4
gpt4 key购买 nike

GET /Spotafood/Restaurants/_search
{
"query": {
"match": {
"name": "Pizza"
}
}
, "filter": {
"and": {
"filters": [
{
"term": {
"Location": "New Jersey"
}
} ,
{
"term": {
"Cusines": "Pizza"
}
} ,{
"numeric_range": {
"rating": {
"from": 1,
"to": 5
}
}
}
]
}
}
}

我如上所述创建了一些过滤器。假设如果我不想过滤位置(即包括所有位置的结果),是否可以使用任何通配符来实现而无需重写查询?

最佳答案

最好的方法是使用AndFIlterBuilder并使用add()方法创建动态查询。

public static SearchSourceBuilder search(SearchParams params) {



Map<String, String> map = new HashMap<String, String>();
map = params.getParametermapping();

SearchSourceBuilder searchsourcequerybuilder = new SearchSourceBuilder();


AndFilterBuilder finalfilter = FilterBuilders.andFilter();
TermsFacetBuilder locationfacet = null;

for (Map.Entry<String, String> entry : map.entrySet()) {
if (entry.getKey().equalsIgnoreCase("querystring")) {
if (entry.getValue().length() == 0) {
searchSourceBuilder = searchSourceBuilder.query(
QueryBuilders.matchAllQuery()).sort(
SortBuilders.fieldSort("rating").order(SortOrder.DESC));

} else {

searchsourcequerybuilder = searchsourcequerybuilder
.query(QueryBuilders.fuzzyLikeThisQuery("name")
.likeText(entry.getValue()));
}
}
if (entry.getKey().equalsIgnoreCase("locationName")) {
if (entry.getValue().length() == 0) {
System.out.println("location Filter is nulll");
locationfacet = FacetBuilders.termsFacet("locationName")
.field("locationName").size(20);

} else {
locationfacet = FacetBuilders
.termsFacet("locationName")
.facetFilter(
FilterBuilders.termFilter("locationName",
entry.getValue()))
.field("locationName").size(20);
finalfilter = finalfilter.add(FilterBuilders.termFilter(
"locationName", entry.getValue()));
}
}
if (entry.getKey().equalsIgnoreCase("dinein")) {
if (entry.getValue().length() == 0) {
System.out.println("Dine in Filter Disabled");
} else {
finalfilter = finalfilter.add(FilterBuilders.termFilter(
"dinein", entry.getValue()));
}
}
if (entry.getKey().equalsIgnoreCase("rating")) {
if (entry.getValue().length() == 0) {

System.out.println("Rating Filter Disabled");
} else {
finalfilter = finalfilter.add(FilterBuilders
.numericRangeFilter("rating")
.from(entry.getValue()).to("5"));

}
}
if (entry.getKey().equalsIgnoreCase("barAvailable")) {
if (entry.getValue().length() == 0) {
System.out.println("barAvailable Filter Disabled");

} else {
finalfilter = finalfilter.add(FilterBuilders.termFilter(
"barAvailable", entry.getValue()));

}
}
}

searchsourcequerybuilder = searchsourcequerybuilder.filter(finalfilter)
.facet(locationfacet);
System.out.println(searchsourcequerybuilder.toString());
return searchsourcequerybuilder;
}

}

关于elasticsearch - 如何在elasticSearch中清除(取消选中过滤器)过滤的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21019387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com