赞
踩
如何选择POST还是PUT,看是否有幂等性(提交多次结果是否一致,若一致就可以用PUT,每次如果都不一样就用POST)
ElasticSearch:对数据进行大量分析、分布式索引、监控&指标
安装好之后点开 bin/elasticsearch.bat
之后访问 http://localhost:9200/
可以看到json格式的数据就是正常打开
ES数据的发送和返回都是json格式的
为什么叫倒排索引
在没有搜索引擎时,我们是直接输入一个网址,然后获取网站内容,这时我们的行为是:
document -> to -> words
通过文章,获取里面的单词,此谓「正向索引」,forward index.
后来,我们希望能够输入一个单词,找到含有这个单词,或者和这个单词有关系的文章:
word -> to -> documents
于是我们把这种索引,成为inverted index,直译过来,应该叫「反向索引」,国内翻译成「倒排索引」,有点委婉了。
原文:https://blog.csdn.net/u013008898/article/details/116493167
shopping是索引名称
创建索引(等于创建数据库,发送PUT请求)
http://127.0.0.1:9200/shopping
得到索引(发送GET请求)
http://127.0.0.1:9200/shopping
得到全部:http://127.0.0.1:9200/_cat/indices?v
删除索引(发送DELETE请求)
http://127.0.0.1:9200/shopping
{
"name":"haige",
"age",123
}
{
"doc" :{
"name":"haige",
}
}
(查询都是GET)
{
"query":{
"match":{
"name":"哈喽"
}
}
}
{
"query":{
"match":{
"name":"哈喽"
}
},
"from":0,
"size":2
}
表示从第0项开始显示,显示2项
{
"query":{
"match":{
"name":"哈喽"
}
},
"from":0,
"size":2,
"_source" : ["name"],
"sort" : {
"age" : {
"order" : "desc"
}
}
}
多条件查询
{
"query":{
"bool" :{
"must" :[
{
"match" :{
"name":"测试"
}
},
{
"match" :{
"age":20
}
}
]
}
}
}
多条件查询
{
"query":{
"bool" :{
"must" :[
{
"match" :{
"name":"测试"
}
},
{
"match" :{
"age":20
}
}
]
}
}
}
{
"query":{
"bool" :{
"should" :[
{
"match" :{
"name":"测试"
}
}
],
"filter" :{
"range":{
"age":{
"gt" : 20
}
}
}
}
}
}
全文检索
match默认全文检索
完全匹配
match_phrase
高亮显示
highlight
{
"query":{
"match_phrase":{
"name":"测试3"
}
},
"highlight":{
"fields":{
"name":{}
}
}
}
{
"aggs":{ // 聚合操作
"price_group":{ // 分组,随意起名
"terms":{ // 分组
"field":"age" // 分组字段
}
}
},
"size":0 // 不显示原始数据
}
{
"aggs":{ // 聚合操作
"price_avg":{ // 平均值名字,随意起名
"avg":{ // 平均值
"field":"age" // 分组字段
}
}
},
"size":0 // 不显示原始数据
}
{
"properties":{
"name":{
"type":"text",
"index":true
},
"sex":{
"type":"keyword",
"index":true
},
"tel":{
"type":"keyword",
"index":false
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>es</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
<!-- elasticsearch依赖2.x的log4j-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
package com.itguigu.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import java.io.IOException;
public class ESTest_Create_index {
public static void main(String[] args) throws IOException {
// 创建客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")));
// 创建索引
CreateIndexRequest request = new CreateIndexRequest("user1");
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
// 相应状态
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("索引创建结果:"+acknowledged);
// 关闭客户端
esClient.close();
}
}
// 查询索引
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse getIndexResponse = esClient.indices().get(request, RequestOptions.DEFAULT);
// 相应状态
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getDataStreams());
System.out.println(getIndexResponse.getSettings());
//删除索引
DeleteIndexRequest request = new DeleteIndexRequest("user");
AcknowledgedResponse delete = esClient.indices().delete(request, RequestOptions.DEFAULT);
//返回状态信息
boolean acknowledged = delete.isAcknowledged();
System.out.println("删除状态:"+acknowledged);
// 创建客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")));
// 创建实体类
User user = new User();
user.setName("小明");
user.setSex("男");
user.setAge(22);
// 创建index连接
IndexRequest request = new IndexRequest("user1").id("123");
// 将实体类转换为json格式字符串
ObjectMapper mapper = new ObjectMapper();
String userJSON = mapper.writeValueAsString(user);
// 绑定到request连接中
request.source(userJSON, XContentType.JSON);
// 查询并打印结果
IndexResponse index = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(index.getResult());
// 关闭客户端
esClient.close();
// 修改数据
UpdateRequest request = new UpdateRequest();
request.index("user1").id("123");
request.doc(XContentType.JSON,"name","小黄");
// 修改数据
UpdateResponse update = esClient.update(request, RequestOptions.DEFAULT);
System.out.println(update.getResult());
// 查询数据
GetRequest request = new GetRequest();
request.index("user1").id("123");
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
//打印结果
System.out.println(response.getSourceAsString());
DeleteRequest request = new DeleteRequest();
request.index("user1").id("123");
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
批量添加:
// 用Bulk批量添加
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index("user1").id("1001").source(XContentType.JSON,"name","zhangsan"));
request.add(new IndexRequest().index("user1").id("1002").source(XContentType.JSON,"name","lisi"));
request.add(new IndexRequest().index("user1").id("1003").source(XContentType.JSON,"name","wangwu"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getIngestTookInMillis());
批量删除:
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest().index("user1").id("1001"));
request.add(new DeleteRequest().index("user1").id("1002"));
request.add(new DeleteRequest().index("user1").id("1003"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getIngestTook());
public static void main(String[] args) throws IOException {
// 创建客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")));
SearchRequest request = new SearchRequest();
request.indices("user1");
request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
// 关闭客户端
esClient.close();
}
SearchRequest request = new SearchRequest();
request.indices("user1");
request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("name","zhangsan1")));
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
SearchRequest request = new SearchRequest();
request.indices("user1");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
query.from(0);
query.size(2);
request.source(query);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
SearchRequest request = new SearchRequest();
request.indices("user1");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
query.sort("age", SortOrder.DESC);
request.source(query);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
SearchRequest request = new SearchRequest();
request.indices("user1");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
String[] includes = {"name"};
String[] excludes = {};
query.fetchSource(includes,excludes);
request.source(query);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
//创建请求
SearchRequest request = new SearchRequest();
request.indices("user1");
//搜索源
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//范围查询类
RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder("age");
rangeQueryBuilder.gte(24);
rangeQueryBuilder.lte(50);
sourceBuilder.query(rangeQueryBuilder);
request.source(sourceBuilder);
//返回结果
SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : search.getHits()) {
System.out.println(hit.getSourceAsString());
}
public static void main(String[] args) throws IOException {
// 创建客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")));
// 创建请求
SearchRequest request = new SearchRequest("user1");
//搜索类型源构建者
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//添加模糊fuzzyQuery选项
searchSourceBuilder.query(QueryBuilders.fuzzyQuery("name","zhan").fuzziness(Fuzziness.ONE));
request.source(searchSourceBuilder);
SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : search.getHits()) {
System.out.println(hit.getSourceAsString());
}
// 关闭客户端
esClient.close();
}
public static void main(String[] args) throws IOException {
// 创建客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")));
SearchRequest request = new SearchRequest("user1");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zhangsan1");
//设置高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<font style='color:red'>");
highlightBuilder.postTags("</font>");
searchSourceBuilder.highlighter(highlightBuilder);
searchSourceBuilder.query(termQueryBuilder);
request.source(searchSourceBuilder);
SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : search.getHits()) {
System.out.println(hit.getSourceAsString());
}
// 关闭客户端
esClient.close();
}
windows下:将es客户复制三份,配置文件写
第一个客户端:
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1001
node.master: true
node.data: true
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: localhost
#
# Set a custom port for HTTP:
#
http.port: 1001
transport.tcp.port: 9301
#
# For more information, consult the network module documentation.
#
# --------------------------------- 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: ["host1", "host2"]
#
# 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.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"
第二个客户端配置文件:
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1002
node.master: true
node.data: true
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: localhost
#
# Set a custom port for HTTP:
#
http.port: 1002
transport.tcp.port: 9302
discovery.seed_hosts: ["localhost:9301"]
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
#
# For more information, consult the network module documentation.
#
# --------------------------------- 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: ["host1", "host2"]
#
# 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.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"
第三个客户端配置文件:
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1003
node.master: true
node.data: true
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: localhost
#
# Set a custom port for HTTP:
#
http.port: 1003
transport.tcp.port: 9303
discovery.seed_hosts: ["localhost:9301","localhost:9302"]
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
#
# For more information, consult the network module documentation.
#
# --------------------------------- 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: ["host1", "host2"]
#
# 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.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。