CentOS 7.3 离线安装ElasticSearch和kibana

本文是基于CentOS 7.3系统环境,安装和测试GitLab:

  • CentOS 7.3
  • elasticsearch-6.2.4.tar.gz
  • kibana-6.2.4-x86_64.rpm

一、ElasticSearch的概念

1. 介绍

Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。

2. 特点

索引集:逻辑上的完整索引
分片:数据拆分后的各个部分
副本:每个分片的拷贝

二、安装前的准备工作

1. 下载安装包

2. 新建用户

  • 出于安全考虑,ES默认不允许以root用户运行
useradd bailang
passwd bailang
su - bailang  # exit 可以退出用户

3. 解压压缩包

  • 修改压缩包的权限
chown bailang:bailang elasticsearch-6.2.4.tar.gz
chmod 755 elasticsearch-6.2.4.tar.gz
tar -xzvf elasticsearch-6.2.4.tar.gz
mv elasticsearch-6.2.4.tar.gz elasticsearch

4. 配置

  • 配置JVM堆内存大小
cd elasticsearch/config
vi jvm.options
## 设置为电脑总内存的一半
-Xms4g
-Xmx4g
tar -xzvf elasticsearch-6.2.4.tar.gz
mv elasticsearch-6.2.4.tar.gz elasticsearch
  • 配置elasticsearch.yml
cd elasticsearch/config
vi elasticsearch.yml
## 设置为电脑总内存的一半
path.data: /home/bailang/elasticsearch/data
path.logs: /home/bailang/elasticsearch/logs
network.host 0.0.0.0
  • 配置max file descriptors
exit # 退出bailang用户
vi /etc/security/limits.comf

# 在最后添加配置信息
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
  • 配置虚拟内存 max virtual memory area
vi /etc/sysctl.conf
#
vm.max_map_count=655360
  • 执行sysctl命令
sysctl -p
  • 创建data目录
su - bailang
mkdir /home/bailang/elasticsearch/data
  • 开放防火墙9200端口
firewall-cmd --add-port=9200/tcp --permanent
firewall-cmd --reload
cp elasticsearch-analysis-ik-6.2.4.zip /home/bailang/elasticsearch/plugins
unzip elasticsearch-analysis-ik-6.2.4.zip
mv elasticsearch ik-analyzer
  • 重启计算机,并重新建立ssh连接
shutdown -r now

5. 运行

  • 切换bailang用户
su - bailang
cd /bin
./elasticsearch

6. 安装kibana

yum install -y kibana-6.0.0-x86_64.rpm

7. 修改配置

  • kibana.yml
vi /etc/kibana/kibana.yml 
##
server.port: 5601        						
server.host: "192.168.1.31"      					
elasticsearch.url: "http://192.168.0.206:9200"
  • 开放防火墙5601端口
firewall-cmd --add-port=5601/tcp --permanent
firewall-cmd --reload
  • 开启服务
systemctl enable kibana
systemctl start kibana

三、ElasticSearch的用法

1. 创建索引

  • 请求方式: PUT
  • 请求路径: /索引库名
  • 请求参数: json格式
# number_of_shards: 分片数量
# number_of_replicas: 副本数量
{
	"settings":{
		"number_of_shards": 3,
		"number_of_replicas": 2
	}
}

2. 查询索引

  • 请求方式: GET
  • 请求路径: /索引库名
GET /zs200

2. 查询所有的索引库

GET _cat/indices

3. 删除索引

  • 请求方式: DELETE
  • 请求路径: /索引库名
DELETE /zs200

4. 创建映射字段

  • 请求方式: PUT
  • 请求路径: /索引库名/_mapping/类型名称
  • 请求参数: json格式
{
	"properties":{
		"字段名": {
			"type": "类型",
			"index": true, # 影响字段索引情况
			"store": false, # 是否将数据进行额外存储
			"analyzer": "分词器",
		}
	}
}
## 商品实例
PUT /zs200/_mapping/goods
{
	"properties":{
		"title": {
			"type": "text",
			"analyzer": "ik_max_word",
		},
		"images": {
			"type": "keyword",
			"index": false,
		},
		"price": {
			"type": "float"
		}
	}
}

5. 数据类型

  • 字符型
text # 字符,可分词
keyword # 字符,不可分词
  • 数字型
long
integer 
short
byte
double
float
half_float
scaled_float
  • 日期型
date
  • 布尔型
boolean
  • 二进制型
binary
  • 区间型
long_range
integer_range
date_range
double_range
float_range
  • 数组型
Array
  • 对象型
Object
  • Nested型
Nested

6. 新增数据

  • 随机生成id
POST /索引库名/类型名
{
	"key":"value"
}
POST /zs200/goods/
{
	"title":"小米手机",
	"images":"http://192.168.0.140/222.png",
	"price":2399.00
}
  • 指定id
POST /索引库名/类型名/id
{
	"key":"value"
}
POST /zs200/goods/1
{
	"title":"大米手机",
	"images":"http://192.168.0.140/233.png",
	"price":2999.00
}

7. 查询数据

GET /索引库名/类型名/1
# 查询所有
GET /zs200/_search
{
	"query":{
		"match_all":{}
	}
}

8. 修改数据

PUT /索引库名/类型名/1
# 查询所有
PUT /zs200/goods/1
{
	"title":"超大米手机",
	"images":"http://192.168.0.140/233.png",
	"price":3999.00
}

9. 删除数据

DELETE /索引库名/类型名/1
# 查询所有
DELETE /zs200/goods/1

10. 查看分词结果

GET /${index}/${type}/${id}/_termvectors?fields=${fields_name}
# 查看分词结果
GET /heima/goods/5/_termvectors?fields=title
# 结果
{
  "_index": "heima",
  "_type": "goods",
  "_id": "5",
  "_version": 2,
  "found": true,
  "took": 1,
  "term_vectors": {
    "title": {
      "field_statistics": {
        "sum_doc_freq": 8,
        "doc_count": 3,
        "sum_ttf": 8
      },
      "terms": {
        "华为": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 0,
              "start_offset": 0,
              "end_offset": 2
            }
          ]
        },
        "手机": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 1,
              "start_offset": 2,
              "end_offset": 4
            }
          ]
        }
      }
    }
  }
}

四、基本查询

1. 初始化环境

  • 新建索引库
put /heima
{
  "settings":{
    "number_of_shards": 3,
		"number_of_replicas": 2
  }
}
  • 新增映射类型
PUT heima/_mapping/goods
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "ik_max_word"
    },
    "images": {
      "type": "keyword",
      "index": "false"
    },
    "price": {
      "type": "float"
    }
  }
}
  • 新增数据
POST /heima/goods/1
{
    "title":"大米手机",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2699.00
}
POST /heima/goods/2
{
    "title":"小米手机",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2699.00
}
POST /heima/goods/3
{
    "title":"超米手机",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2699.00
}
POST /heima/goods/4
{
    "title":"超米电视",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2699.00
}
GET  /索引库名/_search
{
	"query":{
		"查询类型":{
			"查询条件":"查询条件值"
		}
	}
}

2. 查询所有

GET  /heima/_search
{
	"query":{
		"match_all": {}
	}
}
# 结果
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 1,
        "_source": {
          "title": "小米电视4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "超米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

3. 字段匹配查询

GET /heima/_search
{
    "query":{
        "match":{
          "title": "手机"
        }
    }
}
# 结果
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.80259144,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 0.80259144,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 0.19856805,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3",
        "_score": 0.16853254,
        "_source": {
          "title": "超米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

4. 默认会将查询的所有分词用or连接

GET /heima/_search
{
    "query":{
        "match":{
          "title": "小米手机"
        }
    }
}
# 结果
{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1.0137006,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1.0137006,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 0.19856805,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3",
        "_score": 0.16853254,
        "_source": {
          "title": "超米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 0.16044298,
        "_source": {
          "title": "小米电视4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

5. 查询的所有分词用and连接

GET /heima/_search
{
    "query":{
        "match":{
          "title": {
             "query": "小米手机",
             "operator": "and"
          }
        }
    }
}
# 结果
{
  "took": 21,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.0137006,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1.0137006,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

6. 最小匹配度查询

get /heima/_search
{
    "query":{
        "match":{
          "title": {
             "query": "小米手机电视",
             "minimum_should_match": "60%"
          }
        }
    }
}
# 结果
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.0137006,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1.0137006,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 0.77041245,
        "_source": {
          "title": "小米电视4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

五、多字段查询

  • 再新增一条数据
POST /heima/goods/5
{
    "title":"华为手机",
    "subTitle":"小米",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2699.00
}
GET  /heima/_search
{
	"query":{
		"multi_match":{
			"query":"小米",
			"fields": ["title", "subTitle"]
		}
	}
}
# 结果
{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "5",
        "_score": 0.5753642,
        "_source": {
          "title": "华为手机",
          "subTitle": "小米",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 0.52354836,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 0.3901917,
        "_source": {
          "title": "小米电视4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

六、词条匹配(针对未分词类型)

1. 单词条查询

# 面向不分词的类型
GET  /heima/_search
{
	"query":{
		"term":{
			"price":2699.00
		}
	}
}

2. 多词条查询

GET  /heima/_search
{
	"query":{
		"terms":{
			"title": [
			  "小米",
			  "华为"
			]
		}
	}
}
# 结果
{
  "took": 21,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 1,
        "_source": {
          "title": "小米电视4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "5",
        "_score": 1,
        "_source": {
          "title": "华为手机",
          "subTitle": "小米",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

七、结果过滤

1. 默认写法

GET  /heima/_search
{
  "_source": ["title","price"], 
	"query":{
		"terms":{
			"title": [
			  "小米",
			  "华为"
			]
		}
	}
}
# 结果
{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "price": 2699,
          "title": "小米手机"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 1,
        "_source": {
          "price": 2699,
          "title": "小米电视4A"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "5",
        "_score": 1,
        "_source": {
          "price": 2699,
          "title": "华为手机"
        }
      }
    ]
  }
}

2. 包含过滤

GET  /heima/_search
{
  "_source": {
    "includes": ["title","price"]
  }, 
	"query":{
		"terms":{
			"title": [
			  "小米",
			  "华为"
			]
		}
	}
}
# 结果
{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "price": 2699,
          "title": "小米手机"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 1,
        "_source": {
          "price": 2699,
          "title": "小米电视4A"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "5",
        "_score": 1,
        "_source": {
          "price": 2699,
          "title": "华为手机"
        }
      }
    ]
  }
}

3. 排除过滤

GET  /heima/_search
{
  "_source": {
    "excludes": ["title","price"]
  }, 
	"query":{
		"terms":{
			"title": [
			  "小米",
			  "华为"
			]
		}
	}
}
# 结果
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "images": "http://image.leyou.com/12479122.jpg"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 1,
        "_source": {
          "images": "http://image.leyou.com/12479122.jpg"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "5",
        "_score": 1,
        "_source": {
          "images": "http://image.leyou.com/12479122.jpg",
          "subTitle": "小米"
        }
      }
    ]
  }
}

八、高级查询

1. 布尔组合must

GET  /heima/_search
{
	"query":{
		"bool":{
			"must": [
			  {
			    "match": {
			      "title": "小米"
			    }
			  },
			  {
			    "terms": {
			      "price": ["2699","3799"]
			    }
			  }
			]
		}
	}
}
# 结果
{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.5235484,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1.5235484,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 1.3901917,
        "_source": {
          "title": "小米电视4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

2. 布尔组合should

GET  /heima/_search
{
	"query":{
		"bool":{
			"should": [
			  {
			    "match": {
			      "title": "小米"
			    }
			  },
			  {
			    "terms": {
			      "price": ["2699","3799"]
			    }
			  }
			]
		}
	}
}
# 结果
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1.5235484,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1.5235484,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 1.3901917,
        "_source": {
          "title": "小米电视4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "5",
        "_score": 1,
        "_source": {
          "title": "华为手机",
          "subTitle": "小米",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "超米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

3. 布尔组合must_not

GET  /heima/_search
{
	"query":{
		"bool":{
			"must_not": [
			  {
			    "match": {
			      "title": "小米"
			    }
			  }
			]
		}
	}
}
# 结果
{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "5",
        "_score": 1,
        "_source": {
          "title": "华为手机",
          "subTitle": "小米",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "超米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

4. 区间查询

GET /heima/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 2699,
        "lte": 3899
      }
    }
  }
}
# 结果
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 1,
        "_source": {
          "title": "小米电视4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "5",
        "_score": 1,
        "_source": {
          "title": "华为手机",
          "subTitle": "小米",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "超米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

5. 模糊查询(最大支持2位)

  • 再新增一条数据
POST /heima/goods/6
{
    "title":"oppo手机",
    "subTitle":"小米",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":3699.00
}
  • 模糊查询
GET /heima/_search
{
  "query": {
    "fuzzy": {
      "title": {
        "value": "eppe",
        "fuzziness": 2
      }
    }
  }
}
# 结果
{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.6556288,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "6",
        "_score": 0.6556288,
        "_source": {
          "title": "oppo手机",
          "subTitle": "小米",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 3699
        }
      }
    ]
  }
}

九、过滤

所有的查询都会影响到文档的评分及排名,如果需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就采用filter方式

GET /heima/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "title": "手机"
          }
        }
      ],
      "filter": {
        "range":{
          "price":{
            "gte": 2699,
            "lte": 2999
          }
        }
      }
    }
  }
}
GET /heima/_search
{
  "query":{
    "bool": {
      "filter": {
        "range":{
          "price":{
            "gte": 2699,
            "lte": 2999
          }
        }
      }
    }
  }
}

十、排序

  • 新建数据集
GET /heima/_search
{
  "query":{
    "bool": {
      "filter": {
        "range":{
          "price":{
            "gte": 2699,
            "lte": 3999
          }
        }
      }
    }
  },
  "sort": [
    {
      "price": {
        "order": "asc"
      }
    },
    {
      "_id": {
        "order": "desc"
      }
    }
  ]
}

十一、聚合

PUT /cars
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "transactions": {
      "properties": {
        "color": {
          "type": "keyword"
        },
        "make": {
          "type": "keyword"
        }
      }
    }
  }
}

POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
  • 聚合
GET /cars/_search
{
  "aggs": {
    "popular_color": {
      "terms": {
        "field": "color"
      }
    }
  }
}
# 结果
{
  "took": 20,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 1,
    "hits": [
      {
        "_index": "cars",
        "_type": "transactions",
        "_id": "B3cey3EBHa0NCAIRzJeB",
        "_score": 1,
        "_source": {
          "price": 10000,
          "color": "red",
          "make": "honda",
          "sold": "2014-10-28"
        }
      },
      {
        "_index": "cars",
        "_type": "transactions",
        "_id": "CHcey3EBHa0NCAIRzJeB",
        "_score": 1,
        "_source": {
          "price": 20000,
          "color": "red",
          "make": "honda",
          "sold": "2014-11-05"
        }
      },
      {
        "_index": "cars",
        "_type": "transactions",
        "_id": "CXcey3EBHa0NCAIRzJeB",
        "_score": 1,
        "_source": {
          "price": 30000,
          "color": "green",
          "make": "ford",
          "sold": "2014-05-18"
        }
      },
      {
        "_index": "cars",
        "_type": "transactions",
        "_id": "Cncey3EBHa0NCAIRzJeB",
        "_score": 1,
        "_source": {
          "price": 15000,
          "color": "blue",
          "make": "toyota",
          "sold": "2014-07-02"
        }
      },
      {
        "_index": "cars",
        "_type": "transactions",
        "_id": "C3cey3EBHa0NCAIRzJeB",
        "_score": 1,
        "_source": {
          "price": 12000,
          "color": "green",
          "make": "toyota",
          "sold": "2014-08-19"
        }
      },
      {
        "_index": "cars",
        "_type": "transactions",
        "_id": "DHcey3EBHa0NCAIRzJeB",
        "_score": 1,
        "_source": {
          "price": 20000,
          "color": "red",
          "make": "honda",
          "sold": "2014-11-05"
        }
      },
      {
        "_index": "cars",
        "_type": "transactions",
        "_id": "DXcey3EBHa0NCAIRzJeB",
        "_score": 1,
        "_source": {
          "price": 80000,
          "color": "red",
          "make": "bmw",
          "sold": "2014-01-01"
        }
      },
      {
        "_index": "cars",
        "_type": "transactions",
        "_id": "Dncey3EBHa0NCAIRzJeB",
        "_score": 1,
        "_source": {
          "price": 25000,
          "color": "blue",
          "make": "ford",
          "sold": "2014-02-12"
        }
      }
    ]
  },
  "aggregations": {
    "popular_color": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "red",
          "doc_count": 4
        },
        {
          "key": "blue",
          "doc_count": 2
        },
        {
          "key": "green",
          "doc_count": 2
        }
      ]
    }
  }
}
  • 不显示结果集的聚合
GET /cars/_search
{
  "size": 0, 
  "aggs": {
    "popular_color": {
      "terms": {
        "field": "color"
      },
      "avg":{
      "field":"pr"
    }
  }
}
  • 分桶聚合并求平均值
GET /cars/_search
{
  "size": 0, 
  "aggs": {
    "popular_color": {
      "terms": {
        "field": "color"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}
# 结果
{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "popular_color": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "red",
          "doc_count": 4,
          "avg_price": {
            "value": 32500
          }
        },
        {
          "key": "blue",
          "doc_count": 2,
          "avg_price": {
            "value": 20000
          }
        },
        {
          "key": "green",
          "doc_count": 2,
          "avg_price": {
            "value": 21000
          }
        }
      ]
    }
  }
}
  • 桶内嵌套
GET /cars/_search
{
  "size": 0, 
  "aggs": {
    "popular_color": {
      "terms": {
        "field": "color"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        },
        "maker":{
          "terms": {
            "field": "make"
          }
        }
      }
    }
  }
}
# 结果
{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "popular_color": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "red",
          "doc_count": 4,
          "maker": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "honda",
                "doc_count": 3
              },
              {
                "key": "bmw",
                "doc_count": 1
              }
            ]
          },
          "avg_price": {
            "value": 32500
          }
        },
        {
          "key": "blue",
          "doc_count": 2,
          "maker": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "ford",
                "doc_count": 1
              },
              {
                "key": "toyota",
                "doc_count": 1
              }
            ]
          },
          "avg_price": {
            "value": 20000
          }
        },
        {
          "key": "green",
          "doc_count": 2,
          "maker": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "ford",
                "doc_count": 1
              },
              {
                "key": "toyota",
                "doc_count": 1
              }
            ]
          },
          "avg_price": {
            "value": 21000
          }
        }
      ]
    }
  }
}
  • 阶梯分组
GET /cars/_search
{
  "size": 0, 
  "aggs": {
    "price_histogram": {
      "histogram": {
        "field": "price",
        "interval": 5000,
        "min_doc_count": 1
      }
    }
  }
}
# 结果
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_histogram": {
      "buckets": [
        {
          "key": 10000,
          "doc_count": 2
        },
        {
          "key": 15000,
          "doc_count": 1
        },
        {
          "key": 20000,
          "doc_count": 2
        },
        {
          "key": 25000,
          "doc_count": 1
        },
        {
          "key": 30000,
          "doc_count": 1
        },
        {
          "key": 80000,
          "doc_count": 1
        }
      ]
    }
  }
}