我在一个弹性搜索模式中有4个字段。
date
status
type
createdAt现在,我需要获取所有的行
date=today
status = "confirmed"
and where type is not equals to "def"但是,如果
type=def exists 但是,只有当字段createdAt不等于today时。
我当前的查询如下:
{
must: [
{ "bool":
{
"must": [
{"term": {"date": 'now/d'}},
{"term": {"status": 'confirmed'}},
]
}
}
],
mustNot: [
{"match": {'createdAt': 'now/d'}},
{"match":{"type": "def"}}
]
}获取类型不等于"def“的行。但是,如果一个行有type=def AND createdAT any date but today,那么该行就不会出现。
我做错了什么?
发布于 2018-09-10 15:10:14
这个查询应该可以工作。
{
"query": {
"bool": {
"must": [
{ "term": {"date": "now/d" } },
{ "term": {"status": "confirmed" } }
],
"must_not": {
"bool": {
"must": [
{ "match": { "createdAt": "now/d" } },
{ "match": { "type": "def" } }
]
}
}
}
}
}我相信您的版本不起作用的原因是must_not中的每个查询都不能匹配。precision
所有的必须子句必须匹配,所有的must_not子句必须不匹配,但是应该匹配多少个子句?默认情况下,除一个例外情况外,不需要任何一个there子句匹配:如果没有at子句,那么至少有一个least子句必须匹配。
发布于 2018-09-10 23:36:45
假设这样的设置:
PUT twitter
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"_doc": {
"properties": {
"date": {
"type": "date",
"format": "epoch_millis"
},
"createdAt": {
"type": "date",
"format": "epoch_millis"
},
"status": {
"type": "keyword"
},
"type": {
"type": "keyword"
}
}
}
}
}和这样的示例文档(调整值以测试查询):
post twitter/_doc/1
{
"date": 1536562800000, //start of TODAY September 10, 2018 in UTC
"createdAt": 1536562799999,
"status": "confirmed",
"type": "def"
}以下查询应该可以工作:
get twitter/_search
{
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"range": {
"date": {
"gte": "now/d",
"lte": "now/d"
}
}
},
{
"term": {
"status": "confirmed"
}
}
],
"must_not": [
{
"range": {
"createdAt": {
"gte": "now/d",
"lte": "now/d"
}
}
},
{
"term": {
"type": "def"
}
}
]
}
}
}
}
}这是一个过滤的查询,我认为这个场景更好,因为它不计算分数。如果您想要计算分数,只需从顶部删除bool和过滤器即可。
https://stackoverflow.com/questions/52259238
复制相似问题