elasticsearch 7.x 中的DSL查询

query_string , default_field指定搜索字段,在不指定字段的状况下默认搜索 _all 字段,_all综合了全部的内容,同时query 不仅是搜索一个关键词他包含了Lucene中的语法,容许使用and or 或(-)号排除某文档api

GET fanya_statistics/_search
{
  "query": {
    "query_string": {
      "query": "国际经济与贸易",
      "default_field": "zy"
    }
  }
}

term查询与term过滤,term查询实际上是对应Lucene中TermQuery,是没有通过词条分析的精确匹配查询jvm

GET fanya_statistics/_search
{
  "query": {
    "term": {"mz.keyword": "藏族"}
  }
}

terms查询与term相似,terms指的是能够匹配某文档字段中的多个词条,minimum_should_match 限制文档必须匹配的词条数量oop

GET fanya_statistics/_search
{
    "query":{
      "terms":{
        "tags":["jvm", "hadoop"],
        "minimum_should_match":2
      }
    }
}

match查询, 默认状况下使用布尔行为和OR操做符,如咱们要搜索文本“ElasticSearch Denver”时,实际上是搜索包含ElasticSearch或包含Denver的文档,若是想变成搜索“ElasticSearch Denver”须要设置operator:and性能

GET fanya_statistics_fanyajw_zs/_search
{
  "query":{
    "match":{
      "nj":{
        "query":"ElasticSearch Denver",
        "operator":"and"
      }
    }
  }
}

match查询指定词条间隔,假如咱们只记得名称中的两个词还不是挨着的,此时可使用match查询的词组查询行为,指定type:phrase 和 slop词条之间的间距(容许词条之间有间隔数值类型)测试

GET fanya_statistics_fanyajw_zs/_search
{
  "query":{
    "match":{
      "nj":{
        "type": "phrase", 
        "slop":1,
        "query":"ElasticSearch Denver"
      }
    }
  }
}

在6.x已经不支持在math里面使用type,改成match_phraseui

GET fanya_statistics_fanyajw_zs/_search
{
  "query":{
    "match_phrase":{
      "nj":{
        "slop":1,
        "query":"ElasticSearch Denver"
      }
    }
  }
}

match查询,前缀匹配phrase_prefix类型code

GET fanya_statistics_fanyajw_zs/_search
{
  "query":{
    "match":{
      "nj":{
        "type": "phrase_prefix", 
        "Max_expansions":1,
        "query":"ElasticSearch Denver"
      }
    }
  }
}

在6.x后的写法hadoop

GET fanya_statistics_fanyajw_zs/_search
{
  "query":{
    "match_phrase_prefix":{
      "uuid":{
        "query":"ElasticSearch",
        "max_expansions": 10
      }
    }
  }
}

multi_match查询,与terms比较相似terms是在一个字段中匹配多个词,multi_match是指定搜索词条在多个字段中匹配文档

GET fanya_statistics_fanyajw_zs/_search
{
  "query":{
    "multi_match":{
      "query":"2019",
      "fields": ["nj","cc"]
    }
  }
}

bool查询是指组合查询或复合查询,如咱们查询指定学校必须包含2019级数据,同时应该包含2018级数据(没有也无所谓),层次不能包含专升本的string

must 返回结果必须有的数据,只有匹配上才能返回数据,相似于AND,query1 AND query2

should 返回结果应该有的数据,若是没有找到就不返回,相似于OR,query1 OR query2

must_not 返回结果中不该该存在的数据,相似于NOT,query1 AND NOT  query2

假如must域should同时存在,若是must条件不知足的话,即便知足should条件,也不会返回数据由于不知足must条件

GET /fanya_statistics_fanyajw_zs/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": { "type": 1 }},
        {"term": { "sflq": 1 }},
        {"term": { "schoolId": 23398 }},
        {"term": { "nj": "2019级" }}
      ],
      "should": [
        {"term": { "type": 1 }},
        {"term": { "sflq": 1 }},
        {"term": { "schoolId": 23398 }},
        {"term": { "nj": "2018级" }}
      ],
      "must_not": [
        {"term": { "cc": "专升本"}}
      ]
    }
  }
}

range查询,范围查询,如查询添加时间9-18~10-18之间的数据

gt大于,gte大于等于

lt小于,lte小于等于

GET /fanya_statistics_fanyajw_zs/_search
{
  "query": {
    "range": {
      "tjsj": {
        "gte": 1537200000000, // 2018/9/18 0:0:0
        "lte": 1539792000000  // 2018/10/18 0:0:0
      }
    }
  }
}

prefix查询,前缀搜索

GET fanya_statistics_fanyajw_zs/_search
{
    "query":{
      "prefix": {"nj": "2019" }
    }
}

wildcard查询,通配符查询,须要注意的是通配符查询的性能问题,须要仔细测试

*能够表明任意数量的任意字符

?表明一个任意字符

GET fanya_statistics_fanyajw_zs/_search
{
  "query": {
    "wildcard": {"hsz": "北京*大学*"}
  }
}

exists过滤器,须要注意的是高版本es filter的写法改成在bool中指定filter了,并移除了missing的api,能够在 bool must not 中指定exists表明不存在

GET fanya_statistics_fanyajw_zs/_search
{
  "query": {
    "bool": {
      "must": [
        {"match_all": {}}
      ],
      "filter": {
        "exists": {
          "field": "nj"
        }
      }
    }
  }
}
相关文章
相关标签/搜索