1.添加
curl -u elastic:123456 -H "Content-Type:application/json" -XPUT 'http://192.168.0.103:9200/megacorp/employee/3' -d '
{
"first_name":"Douglas",
"last_name":"Fir",
"age":35,
"about":"I love to cabinets",
"interest":["forestry"]
}'
2.获取单个
curl -u elastic:123456 -XGET 'http://192.168.0.103:9200/megacorp/employee/1'
3.轻量搜索
curl -u elastic:123456 -XGET 'http://192.168.0.103:9200/megacorp/employee/_search'
{
"took": 6,
"timed_out": false,
"_shards": { ... },
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "megacorp",
"_type": "employee",
"_id": "3",
"_score": 1,
"_source": {
"first_name": "Douglas",
"last_name": "Fir",
"age": 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_score": 1,
"_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": 1,
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [ "music" ]
}
}
]
}
}
可以看到,我们仍然使用索引库 megacorp 以及类型 employee`,但与指定一个文档 ID 不同,这次使用 `_search 。
返回结果包括了所有三个文档,放在数组 hits 中。一个搜索默认返回十条结果。
curl -u elastic:123456 -XGET 'http://192.168.0.103:9200/megacorp/employee/_search?q=last_name:Fir'
我们仍然在请求路径中使用 _search 端点,并将查询本身赋值给参数 q= 。返回结果给出了所有的 Smith:
4.使用查询表达式搜索
curl -H "Content-Type:application/json" -u elastic:123456 -XGET 'http://192.168.0.103:9200/megacorp/employee/_search' -d '
{
"query":{
"match":{
"last_name":"Fir"
}
}
}‘
5.更复杂的搜索
现在尝试下更复杂的搜索。 同样搜索姓氏为 Smith 的雇员,但这次我们只需要年龄大于 30 的。
查询需要稍作调整,使用过滤器 filter ,它支持高效地执行一个结构化查询。
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
6.全文搜索
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
Elasticsearch 默认按照相关性得分排序,即每个文档跟查询的匹配程度。第一个最高得分的结果很明显:John Smith 的 about 属性清楚地写着 “rock climbing” 。
但为什么 Jane Smith 也作为结果返回了呢?原因是她的 about 属性里提到了 “rock” 。因为只有 “rock” 而没有 “climbing” ,所以她的相关性得分低于 John 的。
7.短语搜索
找出一个属性中的独立单词是没有问题的,但有时候想要精确匹配一系列单词或者短语 。
比如, 我们想执行这样一个查询,仅匹配同时包含 “rock” 和 “climbing” ,
并且 二者以短语 “rock climbing” 的形式紧挨着的雇员记录。
为此对 match 查询稍作调整,使用一个叫做 match_phrase 的查询:
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
8.高亮搜索
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
9.分析
终于到了最后一个业务需求:支持管理者对雇员目录做分析。 Elasticsearch 有一个功能叫聚合(aggregations),
允许我们基于数据生成一些精细的分析结果。聚合与 SQL 中的 GROUP BY 类似但更强大。
举个例子,挖掘出雇员中最受欢迎的兴趣爱好:
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
暂时忽略掉语法,直接看看结果:
{
...
"hits": { ... },
"aggregations": {
"all_interests": {
"buckets": [
{
"key": "music",
"doc_count": 2
},
{
"key": "forestry",
"doc_count": 1
},
{
"key": "sports",
"doc_count": 1
}
]
}
}
}
可以看到,两位员工对音乐感兴趣,一位对林地感兴趣,一位对运动感兴趣。这些聚合并非预先统计,
而是从匹配当前查询的文档中即时生成。如果想知道叫 Smith 的雇员中最受欢迎的兴趣爱好,可以直接添加适当的查询来组合查询:
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests"
}
}
}
}
all_interests 聚合已经变为只包含匹配查询的文档:
...
"all_interests": {
"buckets": [
{
"key": "music",
"doc_count": 2
},
{
"key": "sports",
"doc_count": 1
}
]
}
聚合还支持分级汇总 。比如,查询特定兴趣爱好员工的平均年龄:
GET /megacorp/employee/_search
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
得到的聚合结果有点儿复杂,但理解起来还是很简单的:
...
"all_interests": {
"buckets": [
{
"key": "music",
"doc_count": 2,
"avg_age": {
"value": 28.5
}
},
{
"key": "forestry",
"doc_count": 1,
"avg_age": {
"value": 35
}
},
{
"key": "sports",
"doc_count": 1,
"avg_age": {
"value": 25
}
}
]
}
输出基本是第一次聚合的加强版。依然有一个兴趣及数量的列表,只不过每个兴趣都有了一个附加的 avg_age 属性,代表有这个兴趣爱好的所有员工的平均年龄。
创建 ik 索引 :
{
"settings" : {
"refresh_interval" : "5s", // 新建索引,5s后刷新。
"number_of_shares" : 1 // 索引分片
"nunber_of_replicas" : 0//副本
},
"mappings" : { // 对索引字段的配置
"_default_" : {
"_all" : { //所有字段都生效
"enable" : true
}
},
"products" : { //指定索引类型
"dynamic" : false //动态映射,如果出现新的不认识的字段,false关闭
"properties" : { // 指定拥有哪些字段
"productid" : {
"type" : "long" // 长整形
},
"title" : {
"type" : "string",
"index" : "analyzed", // 创建索引
"analyzer" : "ik" //分词器
},
"descr" : {
"type" : "string",
"index" : "analyzed",
"analyzer" : "ik",
}
}
}
}
}
curl -XPUT "http://192.168.0.105:9200/im_shop" -d'@文件名'
curl -XPUT "http://192.168.0.105:9200/im_shop" -H 'Content-Type: application/json' -d'@test.json'
https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_analytics.html