Elasticsearch 7.2 学习笔记

 

1、工具

kibana-7.2.0-windows-x86_64(汉化:kibana-7.2.0-windows-x86_64\config\kibana.yml  最后一行i18n.locale: "zh-CN"

elasticsearch-7.2.0

2、中文api

https://es.xiaoleilu.com/010_Intro/30_Tutorial_Search.html

es 索引 、type 含义用(db)数据库结构解释

Elasticsearch集群可以包含多个索引(indices)(数据库),每一个索引可以包含多个类型(types)(表),每一个类型包含多个文档(documents)(行),然后每个文档包含多个字段(Fields)(列)。

Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices   -> Types  -> Documents -> Fields

3、插入数据

PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}

4、查询

a、简单搜索

”——索引(megacorp)、类型(employee)和ID(1)既可。根据这三部分信息,我们就可以返回原始JSON文档:

GET /megacorp/employee/1

结果:

{
  "_index" : "megacorp",
  "_type" : "employee",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "first_name" : "John",
    "last_name" : "Smith",
    "age" : 25,
    "about" : "I love to go rock climbing",
    "interests" : [
      "sports",
      "music"
    ]
  }
}

b、搜索全部

使用megacorp索引employee类型,但是我们在结尾使用关键字_search来取代原来的文档ID。响应内容的hits数组中包含了我们所有的三个文档。默认情况下搜索会返回前10个结果

GET /megacorp/employee/_search

 

c、根据某字段搜索

我们在请求中依旧使用_search关键字,然后将查询语句传递给参数q=。这样就可以得到所有姓氏为Smith的结果:

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

d、使用DSL语句查询(结果同上)

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

e、复杂的搜索(加过滤器(filter)

大于30岁且名字为Smith的员工

GET /megacorp/employee/_search
{
    "query" : {
        "filtered" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 } <1>
                }
            },
            "query" : {
                "match" : {
                    "last_name" : "smith" <2>
                }
            }
        }
    }
}

出现错误:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "no [query] registered for [filtered]",
        "line": 3,
        "col": 22
      }
    ],
    "type": "parsing_exception",
    "reason": "no [query] registered for [filtered]",
    "line": 3,
    "col": 22
  },
  "status": 400
}

原因和解决

原因: 过滤查询已被弃用,并在ES 5.0中删除。 
解决: 使用bool / must / filter查询

 

GET /megacorp/employee/_search
{
    "query" : {
        "bool" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 }
                }
            },
            "must" : {
                "match" : {
                    "last_name" : "smith"
                }
            }
        }
    }
}

结果: 

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.35667494,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : 0.35667494,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        }
      }
    ]
  }
}

f、全文搜索

 match查询,从about字段中搜索"rock climbing"

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

 结果:

{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0314757,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 1.0314757,
        "_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.35044178,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        }
      }
    ]
  }
}

 

默认情况下,Elasticsearch根据结果相关性评分来对结果集进行排序,所谓的「结果相关性评分」就是文档与查询条件的匹配程度。很显然,排名第一的John Smithabout字段明确的写到“rock climbing”。

但是为什么Jane Smith也会出现在结果里呢?原因是“rock”在她的abuot字段中被提及了。因为只有“rock”被提及而“climbing”没有,所以她的_score要低于John。

这个例子很好的解释了Elasticsearch如何在各种文本字段中进行全文搜索,并且返回相关性最大的结果集。相关性(relevance)的概念在Elasticsearch中非常重要,而这个概念在传统关系型数据库中是不可想象的,因为传统数据库对记录的查询只有匹配或者不匹配。

End-to-end Search and Analytics About This Book Solve your data analytics problems with the Elastic Stack Improve your user search experience with Elasticsearch and develop your own Elasticsearch plugins Design your index, configure it, and distribute it — you'll also learn how it works Who This Book Is For This course is for anyone who wants to build efficient search and analytics applications. Some development experience is expected. What You Will Learn Install and configure Elasticsearch, Logstash, and Kibana Write CRUDE operations and other search functionalities using the Elasticsearch Python and Java Clients Build analytics using aggregations Set up and scale Elasticsearch clusters using best practices Master document relationships and geospatial data Build your own data pipeline using Elastic Stack Choose the appropriate amount of shards and replicas for your deployment Become familiar with the Elasticsearch APIs In Detail Elasticsearch is a modern, fast, distributed, scalable, fault tolerant, open source search and analytics engine. It provides a new level of control over how you can index and search even huge sets of data. This course will take you from the basics of Elasticsearch to using Elasticsearch in the Elastic Stack and in production. You'll start with the very basics: Elasticsearch terminology, installation, and configuring Elasticsearch. After this, you'll take a look at analytics and indexing, search, and querying. You'll learn how to create maps and visualizations. You'll also be briefed on cluster scaling, search and bulk operations, backups, and security. Then you'll be ready to get into Elasticsearch's internal functionalities including caches, Apache Lucene library, and its monitoring capabilities. You'll learn about the practical usage of Elasticsearch configuration parameters and how to use the monitoring API. You'll discover how to improve the user search experience, index distribution, segment statistics, merging, and more. Once you have mastered this, you'll dive into end-to-end visualize-analyze-log techniques with Elastic Stack (also known as the ELK stack). You'll explore Elasticsearch, Logstash, and Kibana and see how to make them work together to build fresh insights and business metrics out of data. You'll be able to use Elasticsearch with other de facto components in order to get the most out of Elasticsearch. By the end of this course, you'll have developed a full-fledged data pipeline. This Learning Path combines some of the best that Packt has to offer in one complete, curated package. It includes content from the following Packt products: Elasticsearch Essentials Mastering Elasticsearch, Second Edition Learning ELK Stack Style and approach This course aims to create a smooth learning path that will teach you how to effectively use Elasticsearch with other de facto components and get the most out of Elasticsearch. Through this comprehensive course, you'll learn the basics of Elasticsearch and progress to using Elasticsearch in the Elastic stack and in production. Table of Contents Chapter 1. Module 1 Chapter 2. Understanding Document Analysis and Creating Mappings Chapter 3. Putting Elasticsearch into Action Chapter 4. Aggregations for Analytics Chapter 5. Data Looks Better on Maps: Master Geo-Spatiality Chapter 6. Document Relationships in NoSQL World Chapter 7. Different Methods of Search and Bulk Operations Chapter 8. Controlling Relevancy Chapter 9. Cluster Scaling in Production Deployments Chapter 10. Backups and Security
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值