3.7-URISearch详解

URI Search - 通过URI query实现搜索

GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
 "profile":true
}
  • q 指定查询语句,使用Query String Syntax
  • df 默认字段,不指定时,会对所有字段进行查询
  • Sort 排序 / from 和 size用于分页
  • Profile 可以查看查询是如何被执行的

Query String Syntax(1)

  • 指定字段 v.s 泛查询

    • q=title:2012 / q=2012
  • Term v.s Phrase

    • Beautiful Mind 等效于 Beautiful OR Mind

    • "Beautiful Mind" 等效于 Beautiful AND Mind . Phrase查询,还要求前后顺序保持一致

  • 分组与引号

    • title:(Beautiful AND Mind) - Term Query

    • title:"Beautiful Mind" - Phrase Query

Query String Syntax(2)

  • 布尔操作

    • AND / OR / NOT 或者 && / || / !

      • 必须大写

      • title:(matrix NOT reloaded)

  • 分组

    • +表示must

    • -表示must_not

    • title(+matrix -reloaded)

Query String Syntax(3)

  • 范围查询

    • 区间表示: [] 闭区间, {} 开区间

      • year:{2019 TO 2018}

      • year:[* TO 2018]

  • 算数符号

    • year:>2010

    • year:(>2010 && <=2018)

    • year:(+>2010 +<=2018)

Query String Syntax(4)

  • 通配符查询(通配符查询效率低,占用内存大,不建议使用,特别是放在最前面)

    • ? 代表1个字符, * 代表0或多个字符

      • title:mi?d

      • title:be*

  • 正则表达

    • title:[bt]oy
  • 模糊查询与近似查询

    • title:beautifl~1

    • title:"Lord Rings"~2

课程Demo

  • 需要通过 Kibana 导入Sample Data的电商数据。具体参考“2.2节-Kibana的安装与界面快速浏览”
  • 需导入Movie测试数据,具体参考“2.4-Logstash安装与导入数据”
#ignore_unavailable=true,可以忽略尝试访问不存在的索引“404_idx”导致的报错
#查询movies分页
POST /movies,404_idx/_search?ignore_unavailable=true
{
  "profile": true,
    "query": {
        "match_all": {}
    }
}

POST /kibana_sample_data_ecommerce/_search
{
  "from":10,
  "size":20,
  "query":{
    "match_all": {}
  }
}

#对日期排序
POST kibana_sample_data_ecommerce/_search
{
  "sort":[{"order_date":"desc"}],
  "query":{
    "match_all": {}
  }

}

#source filtering
POST kibana_sample_data_ecommerce/_search
{
  "_source":["order_date"],
  "query":{
    "match_all": {}
  }
}

#脚本字段
GET kibana_sample_data_ecommerce/_search
{
  "script_fields": {
    "new_field": {
      "script": {
        "lang": "painless",
        "source": "doc['order_date'].value+'hello'"
      }
    }
  },
  "query": {
    "match_all": {}
  }
}

POST movies/_search
{
  "query": {
    "match": {
      "title": "last christmas"
    }
  }
}

POST movies/_search
{
  "query": {
    "match": {
      "title": {
        "query": "last christmas",
        "operator": "and"
      }
    }
  }
}

POST movies/_search
{
  "query": {
    "match_phrase": {
      "title":{
        "query": "one love"

      }
    }
  }
}

POST movies/_search
{
  "query": {
    "match_phrase": {
      "title":{
        "query": "one love",
        "slop": 1

      }
    }
  }
}

相关阅读

  • https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-uri-request.html
  • https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-search.html

你可能感兴趣的:(3.7-URISearch详解)