一、搜索:
在海量数据进行搜索=分词+搜索
水平全网搜索引擎=百度,谷歌等
垂直搜索引擎=淘宝,京东,万方,知网等
二、海量数据存储
1.静态资源存储:nginx,阿里oss静态存储服务器
2.分类信息=>经常被查询信息=>前端常量数据=>redis,apolitical域
3.查询=分类查询+全文搜索
tip:session仅针对同一个客户端起作用
三、倒排索引:
传统的数据保存方式:
记录->单词
倒排索引保存数据:
单词->记录
索引引擎基于分词技术构建倒排索引,其存储数据如下:
分词->文档ID->文档{一般为json格式}
四、ElasticSearch
1.概念:
开源,分布式,全文搜索引擎
近乎实时的存储,支持集群,可以使用java开发。
Lucene为核心检索功能,对Lucene进行封装,对外只保留了RESTful API
2.Lucene:
Lucene是ElasyicSearch核心,Lucene可以进行处理分词,构建倒排索引等等,
但Lucene只提供了全文检索功能类库的核心工具包,而Lucene也支持另外一个搜索引擎框架Solr
3.ElasticSearch和Solr:
Solr利用Zookeeper进行分布式管理,而ElasyicSearch自带分布式协同管理功能
ElasyicSearch仅仅支持json数据格式,Solr支持众多数据格式
ElasyicSearch本身更注重核心功能,高级功能由第三方插件提供,ElasyicSearch实时性较好
4.ElasticSearch使用场景:
提供关键字进行全网搜索
ELK(ElasyicSearch{查询},Logstash{收集},Kibana{数据可视化})实现企业海量日志的处理分析的解决方案
5.ElasticSearch使用:
在Windows环境下,到ElasticSearch的解压后的bin目录下点击elasticsearch.bat文件,就可以启动ElasticSearch服务器,在本地浏览器中输入localhost:9200即可查看服务器是否启动成功。
ElasticSearch无法启动的常见原因:{
jdk版本不满足要求,一般要求1.8及其以上
服务器启动窗口闪退,通过路径访问追踪错误,若是“空间不足”,则需要修改config/jvm.options配置文件
{
-Xms1g---》-Xms512m
-Xmx1g---》-Xmx512m
}
}
安装ElasticSearch的两个测试工具kibana和Postman,两个工具和tomcat一样,都是立即解压立即使用的绿色工具。其中kibana需要通过浏览器输入http://localhost:5601/app/kibana#/home使用,而postman自带客户端,可以使用其客户端
6.ElasticSearch相关概念:
ElasticSearch是文档型数据库,数据以json为文档序列化格式以文件进行存储。
ElasticSearch与关系型数据库的对照{
ElasticSearch----MySql
索引Index----数据库DataBase
类型Type(7.0及其以上版本没有了)----表Table
文档Docments----行Rows
字段Fields----列Columns
映射mapper(对数据的存储规则做一些映射)
}
tip:所有新技术的学习都要去官网查看技术手册去学习
五、ElasticSearch的基本操作:
1.索引库操作{
建库:PUT /自定义库名称
删库:DELETE /目标库
查看所有索引库:GET /_cat/indices?v
查看某一个索引库:GET /目标索引库名称
}
tip:在6版本自己建立的索引有默认5个分片;在7版本则只有1个;只是使用delete删除索引库不代表真正删除索引库,真正删除索引是在合并索引段或者更新索引的时候
2.类型(表)及映射(约束)操作{
tip:在6版本还有类型,7版本建议删除类型,8版本没有类型
1.建表
{
PUT /索引库名/_mapping/类型表名称{//6版本写法
"properties":{
"字段名称":{
"type【类型】":"类型",
"index【是否索引】":true,
"store【是否存储】":false,
"analyzer【分词器】":"具体分词器",
}
}
}
PUT /索引库名/类型表名称/_mapping?include_type_name=true{//在7版本中写6版本的语法
"properties":{
"字段名称":{
"type【类型】":"类型",
"index【是否索引】":true,
"store【是否存储】":false,
"analyzer【分词器】":"具体分词器",
}
}
}
}
建议:先建索引库后建类型表
2.查看类型表{
查看类型表:
GET /索引库/类型表/_mapping
或
GET /索引库/类型表/_mapping?include_type_name=true
}
3.建索引库同时建类型表{
PUT /索引库{
"settings":{},
"mappings":{
"类型表名称"{
"properties":{
"字段名称":{
"type【类型】":"类型",
"index【是否索引】":true,
"store【是否存储】":false,
"analyzer【分词器】":"具体分词器",
}
}
}
}
}
}
4.对类型表进行数据的增删改查{
添加{
POST /目标索引库/目标类型表/自定义id号
{
"字段1":"对应值1",
"字段2":"对应值2",
"字段3":对应值3
}
}
添加或修改{
POST /目标索引库/目标类型表/自定义目标id号
{
"字段1":"修改值1",
"字段2":"修改值2",
"字段3":修改值3
}
修改指定字段
POST /目标索引库/目标类型表/自定义目标id号/_update
{
"doc":{
"目标字段":修改值1
}
}
}
删除{
DELETE /目标索引库/目标类型表/自定义目标id号
根据条件删除文档
POST /目标索引库/_delete_by_query
{
"query":{
"match":{
"title":"目标字段" #含目标字段的全部删除
}
}
}
}
}
}
3.请求体查询DSL(领域特定语言){
一、查询{
1.查询所有数据{
GET /shopping/_search
{
"query":{
"match_all":{}
}
}
}
2.指定查询{
GET /shopping/_search
{
"query":{
"match":{
"title":"目标"
}
}
}
}
3.多字段匹配查询{
GET /shopping/_search
{
"query":{
"multi_match":{
"query":"目标",
"fields":["title","subtitle"]
}
}
}
}
4.精确查询(以分词索引作为精确对象){
GET /shopping/_search
{
"query": {
"term": {
"title":{
"value":"目标索引分词"
}
}
}
}
}
5.多关键词精准查询{
GET /shopping/_search
{
"query": {
"terms": {
"price":[目标字段1,目标字段2]
}
}
}
}
6.结果过滤{
GET /shopping/_search
{
"_source":["title","price"],
(
或
"_source":{
"includes":["title","price"],
或
"exclude":["images"]
}
)
"query": {
"terms": {
"price":[1999]
}
}
}
}
7.boolean组合{
GET /shopping/_search
{
"query": {
"bool":{
"must":[
{
"match":{
"title":"小米"
}
}
],
"must_not":[
{
"match":{
"title":"电视"
}
}
]
"should":[
"match":{
"title":"手机"
}
]
}
}
}
}
8.范围查询{
范围{
gt是>
gte是>=
lt是<
lte是<=
}
GET /shopping/_search
{
"query":{
"range":{
"price":{
"gte":2500
"lte":4000
}
}
}
}
}
9.模糊查询(纠错查询){
3个字符写错很难纠错
6个字符内的可以纠错一次
6个字符以上可以纠错两次
POST /shopping/product/4
{
"title":"apple手机",
"images":"4.jpg",
"price":5999.00
}
POST /shopping/product/5
{
"title":"apple",
"images":"5.jpg",
"price":3999.00
}
GET /shopping/_search
{
"query": {
"fuzzy": {
"title": "epplle"
}
}
}
或(不考虑耗费性能的条件下)
GET /shopping/_search
{
"query": {
"fuzzy": {
"title": {
"value":"eplpe",
"fuzziness": 2
}
}
}
}
}
10.查询排序{
单字段排序{
GET /shopping/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"或"asc"
}
}
]
}
}
多字段排序{
GET /shopping/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "asc"
},
"_score":{
"order": "desc"
}
}
]
}
}
11.高亮查询{
GET /shopping/_search
{
"query": {
"match": {
"title": "华为"
}
},
"highlight": {
"pre_tags":"<font color='red'>",
"post_tags": "</font>",
"fields": {
"title": {}
}
}
}
}
12.分页查询{
查询公式:from=(pageNum-1)*size
GET /shopping/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "asc"
},
"_score":{
"order": "desc"
}
}
],
"from":0,
"size":2
}
}
}
}
六、ElasticSearch集群{
一、基本概念:
1.集群cluster:由一个或多个结点组织在一起,它们拥有整个数据,并且提供索引和搜索功能
二、集群步骤{
先弄出三个ElasticSearch,然后在config/elasticSearch.yml中修改配置
{
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-elasticsearch
cluster.routing.allocation.disk.threshold_enabled: false
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 127.0.0.1
#
# Set a custom port for HTTP:
#
http.port: 9201
#
# For more information, consult the network module documentation.
transport.tcp.port: 9301
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["127.0.0.1:9301", "127.0.0.1:9302","127.0.0.1:9303"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
}
若在集群之前,单击版本ElasticSearch有数据,那么在集群之后要将原来的数据进行清理,把data文件夹下的内容删除即可。
隔一定的时间分别启动集群的各个ElasticSearch服务器,有一定间隔时间系因为集群之间要选出一个主机,一个集群只有一个主机,如果没有时间间隔那么集群就会出现多个主机(脑裂问题{脑裂问题的起因也可能是网络出现故障})
}
}