Elasticsearch

安装

在Docker中安装Elasticsearch

概念

  1. cluster:集群,一个节点也是集群,对外部而言,每个节点提供的功能是一致的,都同时提供读写操作,表现为去中心化;对内部而言,有一个主节点,该主节点主要负责一些元数据的管理(集群名称、节点数量等)
  2. index:索引,类似于关系型数据库的表
  3. shards:primary shard-主分片,一个索引可以被拆分成多个primary shard(只能在index创建时指定)
  4. replicas:replica shard-从分片,primary shard的备份
  5. type:类型,Es.5.x及以下版本,一个index中可有多个type;Es.6.x中,一个index中只能有一个type;Es.7.x及以上,没有type
  6. document:文档,类似关系型数据库的column,一般使用json格式表示,一个type可以包含多个document
  7. field:字段,一个document可以包含多个field
  8. analyzer:分词器,ES在存储数据时会将数据分词
  9. mapping:映射
  10. 倒排索引:

操作

1.查看整体信息

可使用postman练习下面的语句

--查看集群状态
get http://localhost:9200/_cat/health?v
--查看所有的index信息
get http://localhost:9200/_cat/indices?v
--查看所有的shards信息
get http://localhost:9200/_cat/shards?v

--查看指定index的shards信息
get http://localhost:9200/user_index/_search_shards
--查看所有节点信息
get http://localhost:9200/_cat/nodes?v

2.index操作

--新增index
put http://localhost:9200/user_index
{
	"number_of_shards":1,
	"number_of_replicas":0
}
--删除index
delete http://localhost:9200/user_index
--修改index(shards数量一旦创建便不可修改)
put http://localhost:9200/user_index/_settings
{
	"number_of_replicas":1
}
--查询index
get http://localhost:9200/user_index

3.document操作

--新增document-put
put http://localhost:9200/user_index/user_type/1
{
	"id":1,
	"name":"赵大土",
	"age":26
}

--新增document-post
post http://localhost:9200/user_index/user_type
{
	"id":1,
	"name":"赵大土",
	"age":26
}
--put和post的区别就是put必须传id,post可以不传id(会自动生成一个GUID(UUID加强版-分布式中不会重复))
--由于post和put新增时,指定相同的id会变为更新,所以检测重复id可使用_create(如果已经存在id为1的document,则执行以下语句会报错)
post http://localhost:9200/user_index/user_type/1/_create
{
	"id":1,
	"name":"赵大土",
	"age":26
}
--删除document
delete http://localhost:9200/user_index/user_type/1
--部分修改document
post http://localhost:9200/user_index/user_type/1/_update
{
	"doc":{
		"name":"赵大士",
		"desc":"this is a new field"
	}
}
--主键查询document
get http://localhost:9200/user_index/user_type/1

4.分词器操作

--测试自带标准分词器
get http://localhost:9200/_analyze
{
	"text":"我是中华人民共和国公民",
	"analyzer":"standard"
}

--测试ik smart分词器
get http://localhost:9200/_analyze
{
	"text":"我是中华人民共和国公民",
	"analyzer":"ik_smart"
}

--测试ik max word分词器
get http://localhost:9200/_analyze
{
	"text":"我是中华人民共和国公民",
	"analyzer":"ik_max_word"
}

5.mapping操作

--查看index的mapping
get http://localhost:9200/user_index/_mapping
--新增自定义mapping(默认)
put http://localhost:9200/user_index
{
	"settings":{
		"number_of_shards":1,
		"number_of_replicas":0
	},
	"mappings":{
		"user_type":{
			"properties":{
				"id":{
					"type":"long"
				},
				"name":{
					"type":"text",
					"analyzer":"ik_smart"
				}
				"age":{
					"type":"byte"
				}
			}
		}
	}
}

6.搜索操作

6.1QueryString搜索

--搜索全部的document(默认分页-只查询前10)
get http://localhost:9200/_search

--搜索全部的document(10ms内查询到的数据)
get http://localhost:9200/_search?timeout=10ms

--搜索指定的index中的全部document(默认分页-只查询前10)
get http://localhost:9200/user_index/_search

--全字段条件搜索
get http://localhost:9200/user_index/_search?q=大土

--指定字段条件搜索
get http://localhost:9200/user_index/_search?q=name:大土

--分页搜索
get http://localhost:9200/user_index/_search?size=2&from=0

--包含搜索
get http://localhost:9200/user_index/_search?q=大土
get http://localhost:9200/user_index/_search?q=+大土

--不包含搜索
get http://localhost:9200/user_index/_search?q=-大土

--排序搜索(默认升序)
get http://localhost:9200/user_index/_search?sort=id
get http://localhost:9200/user_index/_search?sort=id:asc
--降序排序
get http://localhost:9200/user_index/_search?sort=id:desc

6.2 QueryDSL搜索

DSL-Domain Specified Language(特殊领域语言)

--全部搜索
get http://localhost:9200/user_index/_search
{
	"query":{
		"match_all":{}
	}
}

--条件搜索
get http://localhost:9200/user_index/_search
{
	"query":{
		"match":{
			"name":"大"
		}
	}
}

--短语搜索(条件短语不会被分词,match搜索中短语会被分词)
get http://localhost:9200/user_index/_search
{
	"query":{
		"match_phrase":{
			"name":"大土"
		}
	}
}

--范围搜索(18<=age<=23)
get http://localhost:9200/user_index/_search
{
	"query":{
		"range":{
			"age":{
				"gte":18,
				"lte":23
			}
		}
	}
}

--复合条件搜索(name中包含"大",且age大于18)
get http://localhost:9200//user_index/_search
{
	"query":{
		"bool":{
			"must":[
				{
					"match":{
						"name":"大"
					},
				},
				{
					"range":{
						"age":{
							"gt":18
						}
					}
				}
			]
		}
	}
}

--复合条件搜索(name包含"大",且age大于18,且desc可以包含"好好学习")
get http://localhost:9200/user_index/_search
{
	"query":{
		"bool":{
			"must":[
				{
					"match":{
						"name":"大"
					}
				},
				{
					"range":{
						"age":{
							"gt":18
						}
					}
				}
			]
		},
		"should":[
			{
				"match_phrase":{
					"desc":"好好学习"
				}
			}
		]
	}
}

--复合条件搜索(name包含"大",且age可以大于18,且desc不能包含"好好学习")
get http://localhost:9200/user_index/_search
{
	"query":{
		"bool":{
			"must":[
				{
					"match":{
						"name":"大"
					}
				}
			],
			"should":[
				{
					"range":{
						"age":{
							"gt":18
						}
					}
				}
			],
			"must_not":[
				"match_phrase":{
					"desc":"好好学习"
				}
			]
		}
	}
}

--单词组搜索(条件不分词,只在反向索引中精准搜索,短语搜索是在反向和正向索引中都搜索)
get http://localhost:9200/user_index/_search
{
	"query":{
		"term":{
			"name":{
				"value":"大土"
			}
		}
	}
}

--多词组搜索
get http://localhost:9200/user_index/_search
{
	"query":{
		"terms":{
			"name":["大土","大士"]
		}
	}
}

--分页排序
get http://localhost:9200/user_index/_search
{
	"size":2,
	"from":0,
	"sort"[
		{
			"age":{
				"order":"desc"
			}
		}
	]
}

--高亮搜索
get http://localhost:9200/user_index/_search
{
	"query":{
		"match":{
			"name":"大"
		}
	},
	"highlight":{
		"fields":{
			"name":{}
		},
		"pre_tags":"<em>",
		"post_tags":"</em>"
	}
}

--高亮分段搜索(fragment_size表示每段大小(不会拆词),number_of_fragements表示返回哪一段)
get http://localhost:9200/user_index/_search
{
	"query":{
		"match":{
			"name":"大"
		}
	},
	"highlight":{
		"fields":{
			"name":{
				"fragment_size":2,
				"number_of_fragments":1
			}
		},
		"pre_tags":"<em>",
		"post_tags":"</em>"
	}
}

--聚合搜索(统计性别为男且不重名的人数)
{
	"query":{
		"match":{
			"sex":"1"
		}
	},
	"aggs":{
		"distinct_count":{
			"cardinality":{
				"field":"name"
			}
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值