ElasticSearch入门

瘦欲@ 提交于 2020-11-19 04:57:50

安装

单实例安装

mkdir imooc
cd imooc
brew install wget
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-darwin-x86_64.tar.gz
ls
tar -vxf elasticsearch-7.0.0-darwin-x86_64.tar.gz
cd elasticsearch-7.0.0
java -version
sh ./bin/elasticsearch
localhost:9200
cd elasticsearch-7.0.0
ls
vim config/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
./bin/elasticsearch -d

插件安装

github -> mobz/elasticsearch-head

cd imooc
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
ls
unzip master.zip
ls
cd elasticsearch-head-master
ls
node -v
npm install
npm run start
localhost:9100

分布式安装

cd elasticsearch-7.0.0
vim config/elasticsearch.yml

cluster.name: bing
node.name: master
node.master: true
network.host: 127.0.0.1

ps -ef | grep `pwd`
kill -9 5531

./bin/elasticsearch -d
clear
cd imooc
mkdir es_slave
cp elasticsearch-7.0.0-darwin-x86_64.tar.gz es_slave
cd es_slave
ls
tar -vxf elasticsearch-7.0.0-darwin-x86_64.tar.gz
cp -r elasticsearch-7.0.0 es_slave1
cp -r elasticsearch-7.0.0 es_slave2

cd es_slave1
vim config/elasticsearch.yml

cluster.name: bing
node.name: slave1
network.host: 127.0.0.1
http.port: 10200
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
./bin/elasticsearch -d

cd es_slave2
vim config/elasticsearch.yml

cluster.name: bing
node.name: slave2
network.host: 127.0.0.1
http.port: 11200
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
./bin/elasticsearch -d

基础概念

集群/节点/索引(数据库)/类型(表)/文档(记录)/分片/备份

基本用法

索引创建

api格式:http://<ip>:<port>/<索引>/<类型>/<文档id> 非格式化索引:http://localhost:9100/ -> 索引 -> 新建索引 格式化索引:postman->put: http://localhost:9200/dogs -> body -> raw

{
	"settings": {
		"number_of_shards": 3,
		"number_of_replicas": 1
	},
	"mappings": {
		"properties": {
			"name": {
				"type": "text"
			},
			"country": {
				"type": "keyword"
			},
			"age": {
				"type": "integer"
			},
			"date": {
				"type": "date",
				"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
			}
		}
	}
}

插入

post: http://127.0.0.1:9200/dogs/_doc/ put: http://127.0.0.1:9200/dogs/_doc/1

{
	"name": "walili",
	"country": "China",
	"age": 23,
	"date": "1988-08-08" 
}

修改

put: http://127.0.0.1:9200/dogs/_doc/1

删除

查询

{
	"query": {
		"match_all": {}
	}
}
{
	"query": {
		"match_all": {}
	},
	"from": 0,
	"size": 1
}
{
	"query": {
		"match": {
			"name": "walili"
		}
	}
}
{
	"query": {
		"match": {
			"country": "China"
		}
	},
	"sort": [
		{
			"date": {"order": "desc"}
		}
	]
}
  • 聚合查询
{
	"aggs": {
		"group_by_age": {
			"terms": {
				"field": "age"
			}
		},
		"group_by_date": {
			"terms": {
				"field": "date"
			}
		}
	}
}
{
	"aggs": {
		"grades_age": {
			"stats": {
				"field": "age"
			}
		}
	}
}
{
	"aggs": {
		"grades_age": {
			"min": {
				"field": "age"
			}
		}
	}
}

高级查询

query

get: http://127.0.0.1:9200/dogs/_search

  • 全文本查询:准对文本型数据
  • 字段级别查询:针对结构化数据

模糊查询

{
	"query": {
		"match": {
			"name": "wali大师"
		}
	}
}

短语查询

{
	"query": {
		"match_phrase": {
			"name": "wali大师"
		}
	}
}

多个字段模糊查询

{
	"query": {
		"multi_match": {
			"query": "wali",
			"fields": ["name", "country"]
		}
	}
}

语法查询

{
	"query": {
		"query_string": {
			"query": "lili OR 大师",
			"fields": ["name", "country"]
		}
	}
}

字段查询

{
	"query": {
		"range": {
			"age": {
				"gte": "30",
				"lte": "100"
			}
		}
	}
}

filter

{
	"query": {
		"bool": {
			"filter": {
				"term": {
					"age": "77"
				}
			}
		}
	}
}

复合查询

get: http://127.0.0.1:9200/_search

{
	"query": {
		"match": {
			"name": "wali"
		}
	}
}

固定分数查询

{
	"query": {
		"constant_score": {
			"filter": {
				"match": {
					"age": "77"
				}
			},
			"boost": 2
		}
	}
}

布尔查询

{
	"query": {
		"bool": {
			"should": [
				{
					"match": {
						"name": "wali"
					}
				},
				{
					"match": {
						"country": "大师"
					}
				}
			]
		}
	}
}
{
	"query": {
		"bool": {
			"must": [
				{
					"match": {
						"name": "wali"
					}
				},
				{
					"match": {
						"country": "大师"
					}
				}
			]
		}
	}
}
{
	"query": {
		"bool": {
			"must_not": [
				{
					"term": {
						"name": "wali"
					}
				}
			]
		}
	}
}

SB 集成 ES

SB 集成 ES

<properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>5.5.2</elasticsearch.version>
</properties>
...
<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>transport</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-core</artifactId>
	<version>2.7</version>
</dependency>
package com.qianbill.admin_api;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Auther: beanho
 * @Date: 2019/4/17 15:51
 * @Description:
 */
@Configuration
public class ElasticSearch {

    @Bean
    public TransportClient client() throws UnknownHostException {
        InetSocketTransportAddress node = new InetSocketTransportAddress(
                InetAddress.getByName("localhost"),
                9300
        );

        Settings settings = Settings.builder()
                .put("cluster.name", "bing")
                .build();

        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);

        return client;
    }
}

查询接口开发

增加接口开发

删除接口开发

更新接口开发

复合查询接口开发

总结

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!