欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

6.Request Query 详解

程序员文章站 2024-02-09 18:02:34
...

1. Request Body Search

将查询语句通过HTTP Request Body 发送给Elasticsearch

1.1 404_idx

//ignore_unavailable=true,可以忽略尝试访问不存在的索引“404_idx”导致的报错
POST /test,404_idx/_search?ignore_unavailable=true
{
  "profile": true,
	"query": {
		"match_all": {}
	}
}
// 没有test 这个索引 但是并不会报错 而是返回hit number 为0
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [ ]
  }
}

1.2 分页

from表示从第10个开始显示
size表示一页显示20个
获取靠后的翻页成本较高

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

1.3 排序

最好在“数字型”"日期型"字段上排序
因为对于多值类型或分析过的字段排序,系统会选一个值,无法得知该值

// 以下按照order_date日期字段降序排序
POST kibana_sample_data_ecommerce/_search
{
  "sort":[{"order_date":"desc"}],
  "from": 10,
  "size": 5, 
  "query":{
    "match_all": {}
  }
}

1.4 _source filtering

如果_source 没有存储,那就只返回匹配的文档的元数据
_source 支持使用通配符

POST /kibana_sample_data_ecommerce/_search
{
  "_source": ["currency","customer_first_name"],
  "query": {"match_all": {}}
}
# 返回结果 可见 source 只有两个字段
"hits" : [
  {
    "_index" : "kibana_sample_data_ecommerce",
    "_type" : "_doc",
    "_id" : "IxbH4WsBSYM1C2oZjVdu",
    "_score" : 1.0,
    "_source" : {
      "customer_first_name" : "rania",
      "currency" : "EUR"
    }
  },

1.5 脚本字段

订单中有不同的汇率,需要结合汇率对订单价格进行排序

   GET kibana_sample_data_ecommerce/_search
{
  "script_fields": {
    "new_field": {
      "script": {
        "lang":"painless",  	// 指定脚本语言 painless
        "source":"doc['order_date'].value+'_hello'"
      }
    }
  },
  "query": {"match_all": {}}
}

1.6 使用查询表达式 Match

POST movies/_search
{
  "profile": "true", 
  "query": {
    "match": {
      "title":{
        "query": "last christmas"    // OR 的关系
      }
    }
  }
}
# 返回结果
 "query" : [
          {
            "type" : "BooleanQuery",
            "description" : "title:last title:christmas",

布尔操作

POST movies/_search
{
  "profile": "true", 
  "query": {
    "match": {
      "title":{
        "query": "last christmas", 
        "operator": "and"	//AND 的关系
      }
    }
  }
}
# 返回结果
"query" : [
          {
            "type" : "BooleanQuery",
            "description" : "+title:last +title:christmas",

短语操作 Match Phrase

 	POST movies/_search
{
  "profile": "true", 
  "query": {
    "match_phrase": {
      "title":{
        "query": "one love"
      }
    }
  }
}
# 输出结果
 "query" : [
          {
            "type" : "PhraseQuery",
            "description" : """title:"one love"""",

slop的使用

   POST movies/_search
{
  "profile": "true", 
  "query": {
    "match_phrase": {
      "title":{
        "query": "one love",
        "slop":1 			//slop 表示两个单词中间可以有几个词
      }
    }
  }
}
# 输出结果
"query" : [
          {
            "type" : "PhraseQuery",
            "description" : """title:"one love"~1""",

2.1 Query String

1.存入数据

PUT /users/_doc/1
{
  "name":"Ruan Yiming",
  "about":"java, golang, node, swift, elasticsearch"
}

PUT /users/_doc/2
{
  "name":"Li Yiming",
  "about":"Hadoop"
}

query_string 支持AND OR

POST users/_search
{
  "query": {
    "query_string": {
      "default_field": "name",
      "query": "Ruan AND Yiming"
    }
  }
}

POST users/_search
{
  "query": {
    "query_string": {
      "fields":["name","about"],
      "query": "(Ruan AND Yiming) OR (hadoop)"
    }
  }
}

Simple Query

Simple Query 不支持 and or not 会当做字符串处理可以使用
+替代 AND
|替代 OR
-替代NOT

//Simple Query 默认的operator是 Or
POST users/_search
{
  "query": {
    "simple_query_string": {
      "query": "Ruan -Yiming",	 //查找 有Ruan 并且无Yiming的文档
      "fields": ["name"],
      "default_operator": "AND"
    }
  }
}

POST users/_search
{
  "query": {
    "simple_query_string": {
      "query": "Ruan Yiming",
      "fields": ["name"],
      "default_operator": "AND"
    }
  }
}