[root@node01 ~]# curl -X GET 'node01:9200/_cat/health?v'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1586505396 07:56:36 elasticsearch yellow 1 1 20 20 0 0 20 0 - 50.0%
[root@node01 ~]# curl -X GET 'node01:9200'
{
"name" : "node-0",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "a4AJdVa8TaiWczmHXc80qQ",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
[root@node01 ~]# curl -X PUT "node01:9200/index1"
# 返回
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "index1"
}
[root@node01 ~]# curl -X GET "node01:9200/index1"
# 返回
{
"index1": {
"aliases": {},
"mappings": {}, # 字段信息
"settings": {
"index": {
"creation_date": "1586503723100",
"number_of_shards": "5", # 默认shard数:5
"number_of_replicas": "1", # 默认replica数:1
"uuid": "mnGGs-8yTbuDrcbwUmdTZQ",
"version": {
"created": "6060099"
},
"provided_name": "index1"
}
}
}
}
curl -X DELETE "node01:9200/index1"
# 返回
{
"acknowledged": true
}
curl -X GET "node01:9200/index1,index2"
# 返回
{
"index1": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1586504252493",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "ICoU-6pfT36cHvEGSys7Ww",
"version": {
"created": "6060099"
},
"provided_name": "index1"
}
}
},
"index2": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1586504255661",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "r3bVxPGRQUC1aZslG2N3yw",
"version": {
"created": "6060099"
},
"provided_name": "index2"
}
}
}
}
# 方法一
curl -X GET "node01:9200/_all"
# 返回
{
"index1": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1586504252493",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "ICoU-6pfT36cHvEGSys7Ww",
"version": {
"created": "6060099"
},
"provided_name": "index1"
}
}
},
"index2": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1586504255661",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "r3bVxPGRQUC1aZslG2N3yw",
"version": {
"created": "6060099"
},
"provided_name": "index2"
}
}
},
"index3": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1586504463960",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "CrtgrJ7QSdyz0Qow_i0pBg",
"version": {
"created": "6060099"
},
"provided_name": "index3"
}
}
},
"test_index": {
"aliases": {},
"mappings": {
"test_type": {
"properties": {
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
},
"settings": {
"index": {
"creation_date": "1586503491778",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "KyWsIMTDQUyNPmRsR0DEvw",
"version": {
"created": "6060099"
},
"provided_name": "test_index"
}
}
}
}
# 方法二
curl -X GET "node01:9200/_cat/indices?v"
# 返回
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open index2 r3bVxPGRQUC1aZslG2N3yw 5 1 0 0 1.2kb 1.2kb
yellow open index1 ICoU-6pfT36cHvEGSys7Ww 5 1 0 0 1.2kb 1.2kb
yellow open test_index KyWsIMTDQUyNPmRsR0DEvw 5 1 1 0 5kb 5kb
yellow open index3 CrtgrJ7QSdyz0Qow_i0pBg 5 1 0 0 1.1kb 1.1kb
[root@node01 ~]# curl -I "node01:9200/index1"
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 231
[root@node01 ~]# curl -I "node01:9200/index100"
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 355
[root@node01 ~]# curl -X POST "node01:9200/index1/_close"
{
"acknowledged":true
}
[root@node01 ~]# curl -X POST "node01:9200/index1/_open"
{
"acknowledged":true,
"shards_acknowledged":true
}
# ES6.x需要在_mapping后指定type,type可以自动创建
curl -X PUT "node01:9200/nba/_mapping/_doc" -H 'Content-Type:application/json' -d'
{
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "keyword"
},
"play_year": {
"type": "keyword"
},
"jerse_no": {
"type": "keyword"
}
}
}
'
# ES7.x
curl -X PUT "node01:9200/nba/_mapping" -H 'Content-Type:application/json' -d'
{
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "keyword"
},
"play_year": {
"type": "keyword"
},
"jerse_no": {
"type": "keyword"
}
}
}
'
# ES6.x
curl -X PUT "node01:9200/index4" -H 'Content-Type:application/json' -d'
{
"mappings": {
"test_type": {
"properties": {
"id": {
"type": "text"
}
}
}
}
}
'
# ES7.x
curl -X PUT "node01:9200/index4" -H 'Content-Type:application/json' -d'
{
"mappings": {
"properties": {
"id": {
"type": "text"
}
}
}
}
'
# ES6.0需要指定type
curl -X GET "node01:9200/nba/_mapping/_doc"
# ES7.0不需要指定type
curl -X GET "node01:9200/nba/_mapping"
{
"nba": {
"mappings": {
"_doc": {
"properties": {
"jerse_no": {
"type": "keyword"
},
"name": {
"type": "text"
},
"play_year": {
"type": "keyword"
},
"position": {
"type": "keyword"
},
"team_name": {
"type": "text"
}
}
}
}
}
}
curl -X GET "ip:9200/index/_mapping/type"
curl -X GET "ip:9200/index/_mapping/type1,type2"
curl -X GET "ip:9200/index/_mapping"
curl -X GET "ip:9200/index1,index2/_mapping/type1,type2"
这时会做笛卡尔积,把所有可以查到的index的type的mapping返回
curl -X GET "ip:9200/index*,test*/_mapping"
curl -X GET "ip:9200/*1,*2/_mapping/type*"
使用通配符也会做笛卡尔积,所有满足通配符的index和type,只要可以这个index有这个type就会查出mapping并返回
curl -X GET "ip:9200/index/_mapping/type"
curl -X GET "ip:9200/index/_mapping"
由于只有一个type,所以这两条查询本质上是一样的
curl -X GET "ip:9200/index1,index2/_mapping/type1,type2"
curl -X GET "ip:9200/test*/_mapping"
同样,也是做笛卡尔积,只要可以查出来就返回
curl -X GET "ip:9200/index1,index2/_mapping"
curl -X GET "ip:9200/test*/_mapping"
curl -X GET "ip:9200/_mapping
curl -X GET "ip:9200/_all/_mapping/type1,type2
curl -X GET "ip:9200/_mapping"
curl -X GET "ip:9200/_all/_mapping"
curl -X GET node01:9200/_all/_mapping/test_type1,test_type2
{
"test_index2": {
"mappings": {
"test_type2": {
"properties": {
"name": {
"type": "text"
}
}
}
}
},
"test_index1": {
"mappings": {
"test_type1": {
"properties": {
"name": {
"type": "text"
}
}
}
}
}
}
curl -X GET node01:9200/_mapping
{
"index1": {
"mappings": {
"_doc": {
"properties": {
"jerse_no": {
"type": "keyword"
},
"name": {
"type": "text"
},
"play_year": {
"type": "keyword"
},
"position": {
"type": "keyword"
},
"team_name": {
"type": "text"
}
}
}
}
},
"index2": {
"mappings": {}
},
"test_index2": {
"mappings": {
"test_type2": {
"properties": {
"name": {
"type": "text"
}
}
}
}
},
"test_index": {
"mappings": {
"test_type": {
"properties": {
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
},
"test2": {
"mappings": {}
},
"nba": {
"mappings": {
"_doc": {
"properties": {
"jerse_no": {
"type": "keyword"
},
"name": {
"type": "text"
},
"play_year": {
"type": "keyword"
},
"position": {
"type": "keyword"
},
"team_name": {
"type": "text"
}
}
}
}
},
"test_index1": {
"mappings": {
"test_type1": {
"properties": {
"name": {
"type": "text"
}
}
}
}
},
"test1": {
"mappings": {}
},
"index3": {
"mappings": {}
}
}
# 只能新增field,不能修改已有field的数据类型
curl -X PUT "node01:9200/test_index1/_mapping/test_type1" -H 'Content-Type:application/json' -d'
{
"properties": {
"id": {
"type": "text"
}
}
}
'
# 如果要写上原有的字段,一定要和以前的数据类型一样
curl -X PUT "node01:9200/test_index1/_mapping/test_type1" -H 'Content-Type:application/json' -d'
{
"properties": {
"id": {
"type": "text"
},
"name": {
"type": "text"
}
}
}
'
# EX7.x不需要指定type
curl -X PUT "node01:9200/test_index1/_mapping" -H 'Content-Type:application/json' -d'
{
"properties": {
"id": {
"type": "text"
}
}
}
'
curl -X PUT "node01:9200/nba"
curl -X PUT "node01:9200/nba" -H 'Content-Type:application/json' -d'
{
"mappings": {
"_doc": {
"properties": {
"jerse_no": {
"type": "keyword"
},
"name": {
"type": "text"
},
"play_year": {
"type": "keyword"
},
"position": {
"type": "keyword"
},
"team_name": {
"type": "text"
},
"country": {
"type": "keyword"
}
}
}
}
}
'
当向一个不存在的index中添加document时,可以自动创建索引,也可以根据传入的数据自动创建mapping,ES也会自动对这些文档进行倒排索引
# 查看ES集群配置
curl -X GET "node01:9200/_cluster/settings"
{
"persistent":{},
"transient":{}
}
# 设置自动创建index功能
curl -X PUT "node01:9200/_cluster/settings" -H 'Content-Type:application/json' -d'
{
"persistent": {
"action.auto_create_index": "false"
}
}
'
{ "acknowledged":true,
"persistent":{
"action":{
"auto_create_index":"false"
}
},
"transient":{}
}
当把action.auto_create_index设置成false后,就不可以向不存在的index插入数据了
# 手动指定id
curl -X PUT "node01:9200/nba/_doc/1" -H 'Content-Type:application/json' -d'
{
"name":"哈登",
"team_name":"火箭",
"position":"得分后卫",
"play_year":"10",
"jerse_no":"13",
"country": "美国"
}
'
# 也可以让ES自动生成id,不指定id要使用POST请求
curl -X POST "node01:9200/nba/_doc" -H 'Content-Type:application/json' -d'
{
"name":"库里",
"team_name":"勇士",
"position":"控球后卫",
"play_year":"10",
"jerse_no":"13",
"country": "美国"
}
'
# 可以手动指定操作类型
# 指定操作类型时,必须手动设置id
# 当不指定操作时,假如原来已经有id=2的document,那么执行以下操作就会把原来的覆盖掉
# 而指定操作为"create",那么当id=2的document已经存在时,就会报错
curl -X POST "node01:9200/nba/_doc/2?op_type=create" -H 'Content-Type:application/json' -d'
{
"name":"姚明",
"team_name":"火箭",
"position":"中锋",
"play_year":"9",
"jerse_no":"11",
"country": "中国"
}
'
curl -X GET "node01:9200/nba/_doc/1"
{
"_index":"nba",
"_type":"_doc",
"_id":"1",
"_version":1,
"_seq_no":0,
"_primary_term":1,
"found":true,
"_source": {
"name":"哈登",
"team_name":"火箭",
"position":"得分后卫",
"play_year":"10",
"jerse_no":"13",
"country": "美国"
}
}
curl -X POST "node01:9200/_mget" -H 'Content-Type:application/json' -d'
{
"docs": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1"
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2"
}
]
}
'
curl -X POST "node01:9200/nba/_mget" -H 'Content-Type:application/json' -d'
{
"docs": [
{
"_type": "_doc",
"_id": "1"
},
{
"_type": "_doc",
"_id": "2"
}
]
}
'
curl -X POST "node01:9200/nba/_doc/_mget" -H 'Content-Type:application/json' -d'
{
"docs": [
{
"_id": "1"
},
{
"_id": "2"
}
]
}
'
curl -X POST "node01:9200/nba/_doc/_mget" -H 'Content-Type:application/json' -d'
{
"ids": ["1", "2"]
}
'
# 方法一:直接覆盖原文档
# 但是要列出所有的field,即使这个field不修改
# 否则新的文档就不包含你没有写的那个field了
# 当然,如果就是要删除这个字段,就不用写了
curl -X PUT "node01:9200/nba/_doc/1" -H 'Content-Type:application/json' -d'
{
"name":"大胡子",
"team_name":"火箭",
"position":"得分后卫",
"play_year":"9",
"jerse_no":"13",
"country": "美国"
}
'
# 方法二:修改指定的字段
curl -X POST "node01:9200/nba/_doc/1/_update" -H 'Content-Type:application/json' -d'
{
"doc": {
"name":"登哥"
}
}
'
# ES7.x
curl -X POST "node01:9200/nba/_update/1" -H 'Content-Type:application/json' -d'
{
"doc": {
"name":"登哥"
}
}
'
# 增加一个字段
curl -X POST "node01:9200/nba/_doc/1/_update" -H 'Content-Type:application/json' -d'
{
"script": "ctx._source.age=18"
}
'
# ES7.x
curl -X POST "node01:9200/nba/_update/1" -H 'Content-Type:application/json' -d'
{
"script": "ctx._source.age=18"
}
'
# 删除一个字段
curl -X POST "node01:9200/nba/_doc/1/_update" -H 'Content-Type:application/json' -d'
{
"script": "ctx._source.remove(\"age\")"
}
'
# ES7.x
curl -X POST "node01:9200/nba/_update/1" -H 'Content-Type:application/json' -d'
{
"script": "ctx._source.remove(\"age\")"
}
'
# 根据参数值,更新字段,要求文档存在并且修改的field存在
curl -X POST "node01:9200/nba/_doc/1/_update" -H 'Content-Type:application/json' -d'
{
"script": "ctx._source.age+=params.age",
"params": {
"age": 4
}
}
'
# 根据参数值,更新字段,如果文档不存在,新创建一个文档,并且将upsert中的数据插入到该文档中
curl -X POST "node01:9200/nba/_doc/3/_update" -H 'Content-Type:application/json' -d'
{
"script": {
"source": "ctx._source.age+=params.age",
"params": {
"age": 4
}
},
"upsert": {
"age": 10
}
}
'
curl -X DELETE "node01:9200/nba/_doc/3"
# text:用于全文检索,该类型的字段将通过分词器进行分词
# keyword:不分词,只能搜索该字段完整的值
curl -X PUT "node01:9200/shop" -H 'Content-Type:application/json' -d'
{
"mappings": {
"product": {
"properties": {
"name": {
"type": "keyword"
},
"desc": {
"type": "text"
}
}
}
}
}
'
curl -X PUT "node01:9200/shop/product/1" -H 'Content-Type:application/json' -d'
{
"name": "Yunnanbaiyao Toothpaste",
"desc": "Yunnanbaiyao Toothpaste"
}
'
curl -X PUT "node01:9200/shop/product/2" -H 'Content-Type:application/json' -d'
{
"name": "Darlie Toothpaste",
"desc": "Darlie Toothpaste"
}
'
curl -X PUT "node01:9200/shop/product/3" -H 'Content-Type:application/json' -d'
{
"name": "ZhongHua Toothpaste",
"desc": "ZhongHua Toothpaste"
}
'
# 查看全部文档
curl -X GET "node01:9200/shop/product/_search"
# 搜索商品名称中包含“Toothpaste”的商品,而且按照price降序排序
curl -X GET "node01:9200/shop/product/_search?q=name:Toothpaste&sort=price:desc"
适用于临时的在命令行使用一些工具,比如curl,快速的发出请求,来检索想要的信息;但是如果查询请求很复杂,是很难去构建的,在生产环境中,几乎很少使用Query String Search
# 查询全部文档
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {"match_all": {}}
}
'
使用的最多的查询方式,更加适合生产环境的使用,可以构建复杂的查询
term
和match
与数据本身的类型息息相关# 词条查询
# 没有结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"term": {
"name": "Toothpaste"
}
}
}
'
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"term": {
"name": "Darlie Toothpaste"
}
}
}
'
# 没有结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"term": {
"desc": "Toothpaste"
}
}
}
'
# 没有结果
# 这个没有结果,是因为"Darlie Toothpaste"分词后分成了"darlie"和"toothpaste"
# 所有完全匹配"Darlie Toothpaste"是无法匹配到的
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"term": {
"desc": "Darlie Toothpaste"
}
}
}
'
# 有3条结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"term": {
"desc": "toothpaste"
}
}
}
# 没有数据
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"terms": {
"name": ["Darlie", "Toothpaste"]
}
}
}
'
# 两条结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"terms": {
"name": ["Darlie Toothpaste", "Yunnanbaiyao Toothpaste"]
}
}
}
'
# 一条结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"terms": {
"name": ["Darlie", "Yunnanbaiyao Toothpaste"]
}
}
}
'
# 没有结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"terms": {
"desc": ["Darlie Toothpaste", "Yunnanbaiyao Toothpaste"]
}
}
}
'
# 1个结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"terms": {
"desc": ["darlie", "Yunnanbaiyao Toothpaste"]
}
}
}
'
# 有结果(1条)
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match": {
"name": "Darlie Toothpaste"
}
}
}
'
# 没有结果
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match": {
"name": "Toothpaste"
}
}
}
'
# 有结果(3条)
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match": {
"desc": "Toothpaste"
}
}
}
'
# 有结果(3条)
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match": {
"desc": "Darlie Toothpaste"
}
}
}
'
curl -X PUT "node01:9200/shop/product/5" -H 'Content-Type:application/json' -d'
{
"name": "Apple Toothpaste",
"desc": "Apple Darlie"
}
'
curl -X PUT "node01:9200/shop/product/6" -H 'Content-Type:application/json' -d'
{
"name": "Orage Darlie",
"desc": "Orage"
}
'
# 3条结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"multi_match": {
"query": "Darlie",
"fields": ["name", "desc"]
}
}
}
'
# 4条结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"multi_match": {
"query": "Orage Darlie",
"fields": ["name", "desc"]
}
}
}
'
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_phrase": {
"desc": "Darlie Toothpaste"
}
}
}
'
# 有结果(3条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_phrase": {
"desc": "Toothpaste"
}
}
}
'
# 没有结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_phrase": {
"name": "Toothpaste"
}
}
}
'
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_phrase": {
"name": "Darlie Toothpaste"
}
}
}
'
# 查询全部数据
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_all": {}
}
}
'
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_all": {
"desc": "Darlie Toothpaste"
}
}
}
'
# 结果
{
"error":{
"root_cause":[
{
"type":"parsing_exception",
"reason":"[5:13] [match_all] unknown field [desc], parser not found","line":5,
"col":13
}
],
"type":"parsing_exception",
"reason":"[5:13] [match_all] unknown field [desc], parser not found",
"line":5,
"col":13,
"caused_by":{
"type":"x_content_parse_exception",
"reason":"[5:13] [match_all] unknown field [desc], parser not found"}
},
"status":400
}
}
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_all": {}
},
"from": 0,
"size": 10
}
'
curl -X PUT "node01:9200/shop/product/4" -H 'Content-Type:application/json' -d'
{
"name": "Darlie Pro Toothpaste",
"desc": "Darlie Pro Toothpaste"
}
'
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_phrase_prefix": {
"name": "Darlie Toothpaste"
}
}
}
'
# 没有结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_phrase_prefix": {
"name": "Darlie"
}
}
}
'
# 有结果(2条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_phrase_prefix": {
"desc": "Darlie"
}
}
}
'
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_phrase_prefix": {
"desc": "Darlie Pro"
}
}
}
'
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
"query": {
"match_phrase_prefix": {
"desc": "Darlie Toothpaste"
}
}
}
'
keyword | text | |
---|---|---|
term | 完全匹配才返回 | 完全匹配分词后的单词才返回 |
terms | 传入多个字符串,返回那些可以完全匹配的结果 | 每个传入的单词,在分词后的所有单词中进行匹配,完全匹配才返回 |
match_all | 查询全部数据,不能传入任何参数 | 查询全部数据,不能传入任何参数 |
match | 完全匹配才返回 | 对输入字符串进行分词,指定的字段文本分词后的词语中包含任意一个输入字符串的分词词语,就算匹配,就可以作为结果返回 |
multi_match | 指定的多个字段都完全匹配才返回 | 对输入字符串进行分词,指定的字段文本分词后的词语中包含任意一个输入字符串的分词词语,就算匹配,就可以作为结果返回 |
match_phrase | 完全匹配才返回 | 输入字符串不分词,指定的字段文本分词后的词语中包含完整的输入字符串,才可以算匹配,才能作为结果返回 |
match_phrase_prefix | 完全匹配才返回 | 输入一个单词,例如"hello",只要指定的字段文本分词后的词语中有一个词语是以"hello"作为前缀,就算匹配,输入一个短语例如"hello world tom",那么先匹配分词的后的词语中包含"hello world"的文档,然后在这些文档中过滤,只要这些文档的词语中包含以"tom"开头的词语,就算匹配 |
# 使用pretty参数可以让返回结果更好看
[root@node01 ~]# curl -X GET "node01:9200/shop/product/_search?pretty" -H 'Content-Type:application/json' -d'
> {
> "query": {"match_all": {}}
> }
> '
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 6,
"max_score" : 1.0,
"hits" : [
{
"_index" : "shop",
"_type" : "product",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "Apple Toothpaste",
"desc" : "Apple Darlie"
}
},
{
"_index" : "shop",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Darlie Toothpaste",
"desc" : "Darlie Toothpaste"
}
},
{
"_index" : "shop",
"_type" : "product",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "Darlie Pro Toothpaste",
"desc" : "Darlie Pro Toothpaste"
}
},
{
"_index" : "shop",
"_type" : "product",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"name" : "Orage Darlie",
"desc" : "Orage"
}
},
{
"_index" : "shop",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Yunnanbaiyao Toothpaste",
"desc" : "Yunnanbaiyao Toothpaste"
}
},
{
"_index" : "shop",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "ZhongHua Toothpaste",
"desc" : "ZhongHua Toothpaste"
}
}
]
}
}