Q:有两个人掉到陷阱里了,死的人叫死人,活人叫什么?

es使用bool查询,使用should查询出来数据,然后需要再加上一个条件数据反而多了

Elasticsearch | 作者 clean | 发布于2018年02月27日 | 阅读数:10584

原始脚本:
 
GET /logstash-jsy/nginx-access/_search
{
   "size": 0,
   "query": {
      "bool": {
         "should": [
            {
               "match_phrase": {
                  "req_url": "/account/login"
               }
            },
            {
               "match_phrase": {
                  "req_url": "/account/register"
               }
            }
         ]
      }
   },
   "aggs": {
      "sales": {
         "date_histogram": {
            "field": "@timestamp",
            "interval": "6h",
            "format": "yyyy-MM-dd-HH"
         }
      }
   }
}
 
 
结果:
 

 
3w多条数据
 
增加条件
 
 
GET /logstash-jsy/nginx-access/_search
{
   "size": 0,
   "query": {
      "bool": {
         "should": [
            {
               "match_phrase": {
                  "req_url": "/account/login"
               }
            },
            {
               "match_phrase": {
                  "req_url": "/account/register"
               }
            }
         ],
         "must": [
            {

  1.                "match": {

                  "geoip.city_name": "Shenzhen,Beijing"
               }
            }
         ]
      }

   },
   "aggs": {
      "sales": {
         "date_histogram": {
            "field": "@timestamp",
            "interval": "6h",
            "format": "yyyy-MM-dd-HH"
         }
      }
   }
}
 
 
query部分增加了一个 must 条件,
结果:
 

捕获q.PNG

 
结果反而变成了70多万,
 
希望结果是增加一个城市的条件后, 结果应该比3w多小
 
请问问题出在什么地方, 应该如何修改?
已邀请:

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自: clean

(round AND (red OR blue)):

{
"query": {
"bool": {
"must": [
{
"term": {"shape": "round"},
"bool": {
"should": [
{"term": {"color": "red"}},
{"term": {"color": "blue"}}
]
}
}
]
}
}
}
参考一下

clean

赞同来自:

GET /logstash-jsy/nginx-access/_search
{
   "size": 0,
   "query": {
      "bool": {
         "must": [
            {
               "bool": {
                  "should": [
                     {
                        "match_phrase": {
                           "req_url": "/account/login"
                        }
                     },
                     {
                        "match_phrase": {
                           "req_url": "/account/register"
                        }
                     }
                  ]
               }
            },
            {
               "match": {
                  "geoip.city_name": "Beijing"
               }
            }
         ]
      }
   },
   "aggs": {
      "sales": {
         "date_histogram": {
            "field": "@timestamp",
            "interval": "6h",
            "format": "yyyy-MM-dd-HH"
         }
      }
   }
}
 
 
这样解决的

要回复问题请先登录注册