ES常用DSL的查询和简单统计

DSL?
英文全称Domain Specific Language,解释为领域专用语言。

DSL是针对某个特定领域而开发的语言,而我们平时接触 到的C/C++,Java,Python/Ruby,
都属于通用语言,DSL就是为了弥补这些通用语言而出现的。

说到编程离不开最基础的CURD

DSL也一样

我们先说查询,直接上实例

1.全检索

GET 索引名/_search
{
  "query": {
        {
          "match_all": {}
        }  

    }
  }

}

2.根据条件检索

Bool查询现在包括四种子句,must,filter,should,must_not

must的两个条件都必须满足,should中的两个条件至少满足一个就可

#名字叫张三的所有人

GET 索引名/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "name": {
              "query": "张三"
            }
          }
        }
      ]

    }
  }
 

}

#统计江苏省的所有城市

GET 索引名/_search
{
  "aggs": {
    "2": {
      "terms": {
        "field": "city",
        "order": {
          "_count": "desc"
        },
        "size": 100
      }
    }
  },
  "size": 0
}

#大桶套小桶分级统计

GET 索引名/_search
{
  "aggs": {
    "2": {
      "terms": {
        "field": "city",
        "order": {
          "_count": "desc"
        },
        "size": 5
      },
      "aggs": {
        "3": {
          "terms": {
            "field": "county",
            "order": {
              "_count": "desc"
            },
            "size": 5
          },
          "aggs": {
            "4": {
              "terms": {
                "field": "garden",
                "order": {
                  "_count": "desc"
                },
                "size": 50
              }
            }
          }
        }
      }
    }
  },
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "city": {
              "query": "九江"
            }
          }
        },
        {
          "match_phrase": {
            "county": {
              "query": "建邺区"
            }
          }
        }
      ]
   

    }
  }
}

发布了15 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Nryana0/article/details/104052101