Elasticsearch_DSL语言

query DSL:domain Specialed Lanaguage 在特定领域的语言

使用match_all 可以查询到所有文档,是没有查询条件下的默认语句。

all = {
    "query": {
        "match_all": {}
    }
}

排序

查询所有新闻类型里面包含"经济",同时按情感值排序

match_class_order = {
    "query": {
        "match": {
            "class1": "经济"
        }
    },
    "sort": [
        {
            "VideoSize": {
                "order": "desc"
            }
        }
    ]
}


实现分页查询

match_class_order_page = {
    "query": {
        "match": {
            "class1": "经济"
        }
    },
    "sort": [
        {
            "VideoSize": {
                "order": "desc"
            }
        }
    ],
    "from": 1,  # 从第几个数据开始
    "size": 5   # 每页数据个数
}

返回指定字段的数据

all = {
    "query": {
        "match_all": {}
    },
    "_source": ['id', "class1", "VioceSize"]
}

多条件

bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含以下操作符:

must :: 多个查询条件的完全匹配,相当于 and。

must_not :: 多个查询条件的相反匹配,相当于 not。

should :: 至少有一个查询条件匹配, 相当于 or。

这些参数可以分别继承一个过滤条件或者一个过滤条件的数组
must_should = {
    "query": {
        "bool":{
            "must":[
                {
                    "match": {
                        "class1": "经济"
                    }
                },
            ],
            "filter": {
                "range": {
                    "VideoSize": {
                        "gte": 3000,
                        "lte": 5000
                    }
                }
            }
        },
    }
}


不要分词,精确匹配

"query": {
  {
      "match_phrase": {
          "news_intro": "佛山市高明区"     
      }
  },
}

高亮选中

must_should = {
  "query": {
     "match_phrase": {
             "news_intro": "佛山市高明区"
    }
  },
  "highlight": {
        "fields": {
            "news_intro":{}
        }
    }
}


image.png

聚合分析

group = {
    "aggs":{
        "group_by_class1":{          # 名字可以随便起
            "terms":{
                "field": "class1"
            }
        }
    }
}

结果 在 hits 里面, 和 _source 同级


image.png

你可能感兴趣的:(Elasticsearch_DSL语言)