1. ES的不同之处: 全文检索、处理同义词、通过相关性给文档评分, 从同样的数据中生成分析与聚合数据, 实时大型批处理。
安装es与kibana
1、下载:https://www.elastic.co/downloads/elasticsearch
2、解压到d:\elasticsearch-2.3.3目录
3、配置Elasticsearch
(1)配置 config\elasticsearch.yml文件
cluster.name: lgs-es
node.name: node-1
(2)配置 bin\elasticsearch.in.bat文件
set ES_MIN_MEM=4g
set ES_MAX_MEM=4g
4、运行服务
双击 bin\elasticsearch.bat 运行
2、安装kibana插件
(1)下载地址: https://www.elastic.co/downloads/kibana
(2)解压到d:\kibana-4.5.1-windows目录
(3)配置config\kibana.yml文件
elasticsearch.url: "http://localhost:9200"
(4)bin\kibana.bat //启动kibana
(5)浏览器输入http://localhost:5601
2. 查看集群状态:
http://localhost:9200/_cluster/health
3. 设置分片: 它本身就是一个完整的搜索引擎, 我们的文档被存储和索引到分片内,但是应用程序是直接与索引而不是与分片进行交互
PUT /blogs
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}
4. 设置存储:
_index : 一个索引仅仅是逻辑上的命名空间, 这个命名空间由一个或者多个分片组合在一起。
_type : types 的文档可能有不同的字段,但最好能够非常相似。
ID : 是一个字符串, 当它和 _index 以及 _type 组合就可以唯一确定 Elasticsearch 中的一个文档。 当你创建一个新的文档,要么提供自己的 _id ,要么让 Elasticsearch 帮你生成。
5. 分布式存储:
分片规则:: shard = hash(routing) % number_of_primary_shards routing: 文档 _id
6. 搜索:
/_all/u*,tweet/_search 在多索引搜索
GET /_search?size=5&from=5 分页
GET /_search?q=mary 查询所有包含 mary 字符串的文档
GET /_search 匹配查询
{
"query": {
"match": {
"tweet": "elasticsearch"
}
}
}
{ 合并查询
"bool": {
"must": { "match": { "tweet": "elasticsearch" }},
"must_not": { "match": { "name": "mary" }},
"should": { "match": { "tweet": "full text" }},
"filter": { "range": { "age" : { "gt" : 30 }} }
}
}
GET /_search 多级排序查询
{
"query" : {
"bool" : {
"must": { "match": { "tweet": "manage text search" }},
"filter" : { "term" : { "user_id" : 2 }}
}
},
"sort": [
{ "date": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}
7. Mapping结构
8. 使用分析器:
GET /_analyze { "analyzer": "standard", "text": "Text to analyze" 要分析(分词)的文本 }
9. 语言处理
GET /my_index/my_type/_search
{
"query": { 短语查询 slop 参数告诉 match_phrase 查询词条相隔多远时仍然能将文档视为匹配
"match_phrase": {
"title": {
"query": "quick fox",
"slop": 1
}
}
}
}
GET /my_index/my_type/_search
{
"query": {
"bool": {
"must": { must 子句从结果集中包含或者排除文档
"match": {
"title": {
"query": "quick brown fox",
"minimum_should_match": "30%"
}
}
},
"should": { should 子句增加了匹配到文档的相关度评分。
"match_phrase": {
"title": {
"query": "quick brown fox",
"slop": 50
}
}
}
}
}
}
GET /my_index/address/_search
{
"query": {
"regexp": { 正则查询
"postcode": "W[0-9].+"
}
}
}
{
"match_phrase_prefix" : { 即时查询: 相当于 walker johnnie bl*
"brand" : {
"query": "walker johnnie bl",
"slop": 10
}
}
}
PUT /my_index/my_type/_mapping 为索引与搜索构建不同的索引
{
"my_type": {
"properties": {
"name": {
"type": "string",
"index_analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
10. 实践
GET /news/test/_search
{
"query": {
"match_phrase": {
"content": {
"query": "活动 后勤",
"slop": 20
}
}
}
}
GET /_analyze
{
"analyzer": "standard",
"text": "Text to analyze"
}
GET /news/test/_mapping
PUT /test/test/1
{
"bookId":1,
"bookName":"Java程序设计",
"publishDate":"2018-01-12"
}
PUT /test/
{
"mappings":{
"books": {
"properties": {
"bookId": {"type": "text"},
"bookName": {"type": "text"},
"publishDate": {"type": "date"}
}
}
}
}
GET /test/books/_mapping
GET _cat/indices
DELETE /books
#mapping可以新增 不能更改
PUT /test/books/_mapping
{
"books": {
"properties": {
"bookName2": {
"type": "text",
"analyzer": "english",
"search_analyzer": "standard"
}
}
}
}

浙公网安备 33010602011771号