
Elasticsearch基础教程
1.搜索引擎Elasticsearch
1.关系数据库和搜索引擎
关系数据库 搜索引擎 持久存储 可以重建 索引部分列 索引所有列 不支持全文搜索 支持全文搜索 数据量有限,千万级 支持集群,支持海量数据 数据最新 可能有延时 结构化数据搜索 - 关系数据库 + 索引
非结构化数据搜索,地理位置搜索 - 搜索引擎
然而,搜索引擎也支持结构化数据搜索
数据量增大,关系数据库需要分库分表之后,搜索产生局限
2.ES特点
- 使用简单
- 支持集群
- 应用广泛
2.Elasticsearch基础使用
创建一个雇员目录
我们受雇于 Megacorp 公司,作为 HR 部门新的 “热爱无人机” ("We love our drones!")激励项目的一部分,我们的任务是为此创建一个员工目录。该目录应当能培养员工认同感及支持实时、高效、动态协作,因此有一些业务需求:
- 支持包含多值标签、数值、以及全文本的数据
- 检索任一员工的完整信息
- 允许结构化搜索,比如查询 30 岁以上的员工
- 允许简单的全文搜索以及较复杂的短语搜索
3.创建员工的索引
index和type,type只保留一个词,一个type可能对于一组table
PUT /megacorp/employee/1 { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] } PUT /megacorp/employee/2 { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests": [ "music" ] } PUT /megacorp/employee/3 { "first_name" : "Douglas", "last_name" : "Fir", "age" : 35, "about": "I like to build cabinets", "interests": [ "forestry" ] }
4.检索文档
按docid检索
GET /megacorp/employee/1 { "_index" : "megacorp", "_type" : "employee", "_id" : "1", "_version" : 1, "_seq_no" : 0, "_primary_term" : 2, "found" : true, "_source" : { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests" : [ "sports", "music" ] } }
5.轻量搜索
GET /megacorp/employee/_search { "took" : 11, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 3, "max_score" : 1.0, "hits" : [ { "_index" : "megacorp", "_type" : "employee", "_id" : "2", "_score" : 1.0, "_source" : { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests" : [ "music" ] } }, { "_index" : "megacorp", "_type" : "employee", "_id" : "1", "_score" : 1.0, "_source" : { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests" : [ "sports", "music" ] } }, { "_index" : "megacorp", "_type" : "employee", "_id" : "3", "_score" : 1.0, "_source" : { "first_name" : "Douglas", "last_name" : "Fir", "age" : 35, "about" : "I like to build cabinets", "interests" : [ "forestry" ] } } ] } }
6.使用查询表达式
GET /megacorp/employee/_search { "query" : { "match" : { "last_name" : "Smith" } } }{ "took" : 10, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 0.2876821, "hits" : [ { "_index" : "megacorp", "_type" : "employee", "_id" : "2", "_score" : 0.2876821, "_source" : { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests" : [ "music" ] } }, { "_index" : "megacorp", "_type" : "employee", "_id" : "1", "_score" : 0.2876821, "_source" : { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests" : [ "sports", "music" ] } } ] } }
7.精确查找,使用term. 不评分,提高性能
GET /megacorp/employee/_search { "query" : { "constant_score" : { "filter" : { "term" : { "age" : 32 } } } } }{ "took" : 11, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "megacorp", "_type" : "employee", "_id" : "2", "_score" : 1.0, "_source" : { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests" : [ "music" ] } } ] } }
8.分词把大写转成小写
查找名称
"term" : { "last_name": "Smith" }无结果
"term" : { "last_name": "smith" }有结果,想想为什么
分词把大写转成小写,输入条件经过分词也转成了小写
多个term呢,试试
GET /megacorp/employee/_search { "query" : { "constant_score" : { "filter" : { "terms" : { "age" : [32, 35] } } } } }
9.更复杂的搜索
复杂的搜索
GET /megacorp/employee/_search { "query" : { "bool": { "must": { "match" : { "last_name" : "smith" } }, "filter": { "range" : { "age" : { "gt" : 30 } } } } } }bool表示一个组合,must需要全部满足,filter用于范围查询
{ "took" : 9, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.2876821, "hits" : [ { "_index" : "megacorp", "_type" : "employee", "_id" : "2", "_score" : 0.2876821, "_source" : { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests" : [ "music" ] } } ] } }bool过滤器完整语法
{ "bool" : { "must" : [], "should" : [], "must_not" : [], } }bool可以嵌套在should里面
"should" : [ "match": {}, "bool": { "must" : [], "should" : [], "must_not" : [], } ],
10.全文搜索
观察评分
GET /megacorp/employee/_search { "query" : { "match" : { "about" : "rock climbing" } } }{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 0.5753642, "hits" : [ { "_index" : "megacorp", "_type" : "employee", "_id" : "1", "_score" : 0.5753642, "_source" : { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests" : [ "sports", "music" ] } }, { "_index" : "megacorp", "_type" : "employee", "_id" : "2", "_score" : 0.2876821, "_source" : { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests" : [ "music" ] } } ] } }
11.短语搜索
GET /megacorp/employee/_search { "query" : { "match_phrase" : { "about" : "rock climbing" } } }{ "took" : 8, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.5753642, "hits" : [ { "_index" : "megacorp", "_type" : "employee", "_id" : "1", "_score" : 0.5753642, "_source" : { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests" : [ "sports", "music" ] } } ] } }
12.更新文档(修改年龄)
PUT /megacorp/employee/3 { "first_name" : "Douglas", "last_name" : "Fir", "age" : 38, "about": "I like to build cabinets", "interests": [ "forestry", "tennis" ] }GET /megacorp/employee/_search
13.删除文档
DELETE /megacorp/employee/3GET /megacorp/employee/_search{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 1.0, "hits" : [ { "_index" : "megacorp", "_type" : "employee", "_id" : "2", "_score" : 1.0, "_source" : { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests" : [ "music" ] } }, { "_index" : "megacorp", "_type" : "employee", "_id" : "1", "_score" : 1.0, "_source" : { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests" : [ "sports", "music" ] } } ] } }
14.删除整个索引
DELETE /megacorp
15.查看映射(mapping)
GET /megacorp/_mapping/employee{ "megacorp" : { "mappings" : { "employee" : { "properties" : { "about" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "age" : { "type" : "long" }, "first_name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "interests" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "last_name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } } } }
16.指定索引文档的字段类型
测试:
- 指定中文分词
- 有些字段不需要全文检索,只作为关键词(keyword)检索