ElasticSearch 的Search Api

URI Search

在URL中直接使用查询参数

Request Body Search

使用ElasticSearch 提供的,给予JSON格式的更加完备的 Query Domain Specific Language

指定 查询的索引

语法范围
/_search集群上的所有索引
/index1/_searchindex1
/index1,index2 /_searchindex1 和 index2
/index*/_search搜索以 index 开头的索引

使用URI 查询实例

使用 ‘q’ 指定查询字符串,
“query string syntax ” , KV 键值对

curl -XGET "http://elasticsearch:9200/kibana_sample_data_ecommerce/_search?q=customer——firest_name:Eddie "——> 搜索一个叫做 Eddie 的客户

使用RequestBody

同时支持 POST 和 GET、

curl -XGET 
"http://elasticsearch:9200/kibana_sample_data_ecommerce/_search"   -H            // kibana_sample_data_ecommerce 索引名           
'Content-Type:application/json' -d '
{
	"query" : {                 // 查询
		"match_all" : {}     // match_all 返回所有的文档
	}
}’

在这里插入图片描述
URI Search 通过 URI query 实现搜索

对指定字段进行查询

GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
	"profile":true
}

GET /movies/_search?q=title:2012
{
	"profile":true
}
  • q 指定查询语句,使用query String Syntax
  • df 默认字段,不指定时会对所有字段进行查询
  • Sort 排序 /from 和 Size 用于分页
  • Profile 可以查看查询是如何被执行的
指定字段查询过程

指定字段查询过程

泛查询


// 泛查询
GET /movies/_search?q=2012
{
	"profile":true
}

泛查询
Term v.s Phrase
Beafutiful Mind 等效于 Beautiful OR Mind

“Beautiful Mind” 等效于 Beafutiful ADN MIND Phrase 查询, 还要求前后顺序保持一致

在这里插入图片描述

分组和引号

title:(Beautifule Mind)
在这里插入图片描述

title: “Beautifule Mind”

在这里插入图片描述

布尔操作

使用AND
在这里插入图片描述
使用 NOT
在这里插入图片描述
使用OR
在这里插入图片描述
分组:

  • + 表示 must
  • - 表示 must_not
范围查询

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

  • year : {2019 TO 2018}
  • year : {* TO 2018}
算数符号
  • year : > 2010
  • year : (> 2010 && <= 2018)
  • year : (+> 2010 +<=2018)
    在这里插入图片描述
通配符查询(通配符查询效率低,占用内存大,不建议使用,特别是放在最前面。)
  • ? 代表一个字符, * 代表 0 个或者 多个字符
    1. title: mi?d
    2. title: be*

在这里插入图片描述

正则表达式
  • title :[bt] oy
模糊匹配 与 近似查询
  • title :befutifl~1
  • title: “lord rings” ~2
  • 在这里插入图片描述
Request Body Search
  • 将查询语句通过 HTTP Request Body 发送给ElasticSearch

  • Query DSL

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

from 从 0 开始, 默认返回 10 个结果
获取靠后的翻页成本比较高

排序

GET /kibana_sample_data_ecommerce/_search
{
  "sort": [
    {
      "order_date": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 20,
  "query": {
     "match_all": {}
  }
}

sort 也可以这么来写

GET /kibana_sample_data_ecommerce/_search
{
  "sort":[{"order_date":"desc"}],
  "query": {
    "match_all": {}
  }
}

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

_source filter

如果并不是需要查询所有的信息,那么你可以过滤掉那些你不需要的信息
比如:


GET /kibana_sample_data_ecommerce/_search
{
  "_source": ["order_date","order_date","category.keyword"]
}

返回结果

{
 "_index" : "kibana_sample_data_ecommerce",
  "_type" : "_doc",
  "_id" : "ywUi6WsBipwyH8yLpMZE",
  "_score" : 1.0,
  "_source" : {
    "order_date" : "2019-07-22T09:28:48+00:00"
  }
}

如果_source 没有存储,那么久只返回匹配文档的元数据
** _source 支持使用通配符,_srouce[“name*”,“desc*”]**

GET /kibana_sample_data_ecommerce/_search
{
  "_source": ["order_*"]
}

返回

	{
      "_index" : "kibana_sample_data_ecommerce",
      "_type" : "_doc",
      "_id" : "ywUi6WsBipwyH8yLpMZE",
      "_score" : 1.0,
      "_source" : {
        "order_date" : "2019-07-22T09:28:48+00:00",
        "order_id" : 584677
      }
    },
脚本字段
GET /kibana_sample_data_ecommerce/_search
{
  "script_fields": {
    "new_field": {
      "script": {
        "lang":"painless",
        "source": "doc['order_date'].value + 'hello'"
      }
    }
  },
  "from": 0,
  "size": 20,
  "query": {
    "match_all": {}
  }
}

** 意思就是给文档中的 order_date 的值添加hello但是不修改文档。 **
查询结果

{
  "_index" : "kibana_sample_data_ecommerce",
   "_type" : "_doc",
   "_id" : "ywUi6WsBipwyH8yLpMZE",
   "_score" : 1.0,
   "fields" : {
     "new_field" : [
       "2019-07-22T09:28:48.000Zhello"
     ]
   }
 },
使用查询表达式 Match
// 使用查询表达式
GET movies/_search
{
  "query": {
    "match": {
      "title": "Last Christms"
    }
  },
  "profile": "true"
}

只需要满足 标题中存在 Last 和 Christms 就可以了。

GET movies/_search
{
  "query": {
    "match": {
      "title": {
        "query": "Last Christms",
        "operator": "and"
      }
    }
  },
  "profile": "true"
}

查询过程

"query" : [
              {
                "type" : "BooleanQuery",
                "description" : "+title:last +title:christms",
                },

描述中表示,必须要标题中存在 last 和 christms 才能被查询到

match_phrase查询

使用phrase 查询会考虑 词汇位置,
match查询增加, 不会考虑词汇位置

// match_phrase 查询
GET movies/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "one love"
      }
    }
  }, 
  "profile": "true"
}

搜索结果为空

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  } 
}  

使用slop


GET movies/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "one love",
        "slop": 1
      }
    }
  }
}
查询结果
"hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "113829",
        "_score" : 5.323583,
        "_source" : {
          "title" : "One I Love, The",
          "genre" : [
            "Comedy",
            "Drama",
            "Romance"
          ],
          "@version" : "1",
          "year" : 2014,
          "id" : "113829"
        }
      }
    ]
Query String Query
类似 URI Query
QueryString
- 类似QueryString,但是会忽略错误的语法,同时只支持部分的查询语法
- 不支持 AND OR NOT, 会当做字符串处理
- Term 之间的默认关系是 OR, 可以指定  Operator 
- 支持 部分逻辑
	1. +  替代 AND
	2. | 替代 OR
	3. - 替代 NOT
PUT /users/_doc/1
{
  "name" : "Gao Songsong",
  "about" : "Java"
}

PUT /users/_doc/2
{
  "name" : "Liu Songsong",
  "about" : "PHP"
}


POST users/_search
{
  "query": {
    "query_string": {
      "default_field": "name",
      "query": "Gao AND Songsong"
    }
  }
}

POST users/_search
{
  "query": {
    "query_string": {
      "fields": ["namme","about"],
      "query": "(Gao AND Songsong) OR (JaVA)"
    }
  }
}


// Simple Query 默认的是 operator 是 OR
POST users/_search
{
  "query": {
    "simple_query_string": {
      "query": "Gao songsong",
      "fields": ["name"],
      "default_operator": "AND"
    }
  }  
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值