美文网首页
elk 5:es 检索

elk 5:es 检索

作者: _Rondo | 来源:发表于2020-10-28 17:54 被阅读0次

一、前言

ELasticsearch使用Javascript对象符号(JavaScript Object Notation),也就是JSON,作为
文档序列化格式。JSON现在已经被大多语言所支持,而且已经成为NoSQL领域的标准格
式。它简洁、简单且容易阅读。
首先要做的是存储员工数据,每个文档代表一个员工。在Elasticsearch中存储数据的行
为就叫做索引(indexing),不过在索引之前,我们需要明确数据应该存储在哪里:

  • es中的index 相当于关系型数据库的 库
  • es中的type 相当于关系型数据库的 表
  • es中的document 相当于关系数据库表中的 一条记录相当于表的行
  • es中的field 相当于关系型数据库表的字段的 Column 相当于列

二、简单 query

首先使用postman创建员工,遵循http restful

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" ]
}

简单搜索

GET /megacorp/employee/_search
image.png

查询字符串(query string)搜索

GET  /megacorp/employee/_search?q=last_name:Smith

DSL查询就是请求体是json

GET /megacorp/employee/_search
{
  "query" : {
    "match" : {
      "last_name" : "Smith"
    }
  }
}

term查询,完全匹配,即不进行分词器分析,terms 匹配多个

GET /megacorp/employee/_search
{
    "query" : {
        "term" : {
            "first_name" : "Jane"
        }
    }
}
{
    "query" : {
        "terms" : {
            "first_name" : ["Jane"]
        }
    }
}

match_all:查询所有文档

GET /megacorp/employee/_search
{
    "query" : {
        "match_all" : {
        }
    }
}

multi_match:可以指定多个字段

GET /megacorp/employee/_search
{
    "query" : {
        "multi_match" : {
            "query" : "Jane",
            "fields":  ["first_name","last_name"] 
        }
    }
}

三、聚合

这里pdf是6.0版本,本地是7.8.0 遇到了两个问题:
1.Fielddata is disabled on text fields by default. Set fielddata=true on [interests] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead
默认fielddata在text字段上默认不起作用的,为了能够在内存中解析出倒排索引的字段数据,需要在interes的字段上开启fielddata
2.Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to
这个是因为elasticsearch7.0 之后不支持type导致的…高版本要求传入一个include_type_name参数,值为true。所以加上一个参数即可。
解决上面两个问题:

PUT /megacorp/_mapping/employee?include_type_name=true
{
  "properties": {
    "interests": { 
      "type":     "text",
      "fielddata": true
    }
  }
}

所有姓"Smith"的人最大的共同点(兴趣爱好)

GET /megacorp/employee/_search
{
    "query": {
        "match": {
            "last_name": "smith"
            }
    },
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "interests"
            }
        }
    }
}

统计每种兴趣下职员的平均年龄

GET /megacorp/employee/_search
{
    "aggs" : {
        "all_interests" : {
            "terms" : { "field" : "interests" },
            "aggs" : {
                "avg_age" : {
                    "avg" : { "field" : "age" }
                }
            }
        }
    }
}

四、写在最后

《ES权威指南.pdf》,已上传度盘,需要自取链接: https://pan.baidu.com/s/1EV1dJ0WDXumWJFUgU7yGGg 提取码: eih1

相关文章

网友评论

      本文标题:elk 5:es 检索

      本文链接:https://www.haomeiwen.com/subject/rubtvktx.html