我正在使用elasticsearch Query dsl,但找不到表达以下内容的方法:
返回具有字段"price" > min budget
且具有“价格” <最大预算且具有has_price=true
的结果,并且还返回具有"has_price=false"
的所有结果
换句话说,我只想对has_price字段设置为true的结果使用范围过滤器,否则,对has_price字段设置为false的结果不考虑过滤器
以下是映射:
{
"formations": {
"mappings": {
"properties": {
"code": {
"type": "text"
},
"date": {
"type": "date",
"format": "dd/MM/yyyy"
},
"description": {
"type": "text"
},
"has_price": {
"type": "boolean"
},
"place": {
"type": "text"
},
"price": {
"type": "float"
},
"title": {
"type": "text"
}
}
}
}
}
答案 0 :(得分:0)
以下查询将 2个场景组合为布尔查询中的 2应该子句。并且由于只有应该子句,function getLocation(url) {
var fetched = UrlFetchApp.fetch(url, {followRedirects:false});
if (fetched.getHeaders().Location !== undefined) {
return getFullUrl(fetched.getHeaders().Location);
}
return url;
}
将为1,这意味着至少一个应子句必须匹配:
抽象代码段
minimum_should_match
实际示例代码段
GET formations/_search
{
"query": {
"bool": {
"should": [
{ <1st scenario: has_price = false> },
{ <2nd scenario> has_price = true AND price IN budget_range}
]
}
}
}
别忘了将克劳斯# 1. Create the index and populate it with some sample documents
POST formations/_bulk
{"index": {"_id": 1}}
{"has_price": true, "price": 2.0}
{"index": {"_id": 2}}
{"has_price": true, "price": 3.0}
{"index": {"_id": 3}}
{"has_price": true, "price": 4.0}
{"index": {"_id": 4}}
{"has_price": false, "price": 2.0}
{"index": {"_id": 5}}
{"has_price": false, "price": 3.0}
{"index": {"_id": 6}}
{"has_price": false, "price": 4.0}
# 2. Query assuming min_budget = 2.0 and max_budget = 4.0
GET formations/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"filter": {
"term": {
"has_price": false
}
}
}
},
{
"bool": {
"filter": [
{
"term": {
"has_price": true
}
},
{
"range": {
"price": {
"gt": 2,
"lt": 4
}
}
}
]
}
}
]
}
}
}
# 3. Result Snippet (4 hits: 3 from 1st scenario & 1 from 2nd scenario)
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
...
添加到bool查询中,以防您在bool查询中添加另一个不应该的子句。
让我知道这是否可以回答您的问题并解决您的问题。