ElasticSearch Query DSL

query DSL

POST index-test/type-test/_search

keyword

_search
_update
_bulk
_mapping
_delete_by_query
还和请求类型有关 GET, PUT, DELETE, POST

上下文

查询上下文(score vs 过滤上下文(是否)
* queries vs filters
    relevance(相关性) boolean
    not cached cached
    slow    faster

同一文档在不同query中的score没有可比性

bool

must :: 多个查询条件的完全匹配,相当于 and。 must_not :: 多个查询条件的相反匹配,相当于 not。 should :: 至少有一个查询条件匹配, 相当于 or。

{
 "bool": {
  "must": {
   "term": {
    "folder": "inbox"
   }
  },
  "must_not": {
   "term": {
    "tag": "spam"
   }
  },
  "should": [{
   "term": {
    "starred": true
   }
  },
  {
   "term": {
    "unread": true
   }
  }]
 }
}

match one

GET index-test/type-test/id

match all

GET or POST index-test/type-test/_search 如果使用GET,则默认是match_all

{
  "query": {
    "match_all": {}
  }
}

match phrase

{
  "query": {
    "match_phrase": {
      "age": 15
    }
  }
}

term

term主要用于精确匹配值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数据类型, string默认analyzed) 经过分析的文本类型无法精确匹配 (暂时没有找到设置为not_analyzed的方法)

{
  "query": {
    "term": {
      "age": 15
    }
  }
}

{
  "query": {
    "terms": {
      "age": [15, 16]
    }
  }
}

match

会对要匹配的内容进行分词

{
  "query": {
    "match": {
      "name": "Sam1"
    }
  }
}

wildcard

{
  "query": {
    "wildcard": {
      "name": "Sam*"
    }
  }
}

regexp

{
  "query": {
    "regexp": {
      "name": "Sam[0-9]"
    }
  }
}

exists

{
  "query": {
    "exists": {
      "field": "name"
    }
  }
}

range

POST index-test/type-test/_search

gt :: 大于
gte:: 大于等于
lt :: 小于
lte:: 小于等于

{
  "query": {
    "range": {
      "age": {
        "gte": 14
      }
    }
  }
}

prefix

{
  "query": {
    "prefix": {
      "date": "2020"
    }
  }
}