elasticsearch看这一篇就够了---入门(2)Elasticsearch检索文档,DSL语句查询,复杂查询,全文搜索

在这里插入图片描述

1.检索文档

(没有数据的看上一篇就可以elasticsearch看这一篇就够了—入门(1)Elasticsearch&Kibana概念详解,下载安装使用

现在Elasticsearch中已经存储了一些数据,我们可以根据业务需求开始工作了。

(1)第一个需求是能够检索单个员工的信息。 这对于Elasticsearch来说非常简单。我们只要执行HTTP GET请求并指出文档的“地址”——索引、类型和ID既可。根据这三部 分信息,我们就可以返回原始JSON文档:

GET /albb/employee/1

响应的内容中包含一些文档的元信息,John Smith的原始JSON文档包含在 _source 字段中。还有的字段后面再说明,每一个都是很重要的字段。

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

2.简单搜索

GET 请求非常简单——你能轻松获取你想要的文档。

让我们来进一步尝试一些东西,比如简单的搜索! 我们尝试一个最简单的搜索全部员工的请求:

GET /albb/employee/_search

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

{
  "took": 41,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "albb",
        "_type": "employee",
        "_id": "2",
        "_score": 1,
        "_source": {
          "first_name": "Jane",
          "last_name": "Smith",
          "age": 32,
          "about": "I like to collect rock albums",
          "interests": [
            "music"
          ]
        }
      },
      {
        "_index": "albb",
        "_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": "albb",
        "_type": "employee",
        "_id": "3",
        "_score": 1,
        "_source": {
          "first_name": "Douglas",
          "last_name": "Fir",
          "age": 35,
          "about": "I like to build cabinets",
          "interests": [
            "forestry"
          ]
        }
      }
    ]
  }
}

接下来,让我们搜索姓氏中包含“Smith”的员工。要做到这一点,我们将在命令行中使用轻量级的搜索方法。这种方法常被 称作查询字符串(query string)搜索,因为我们像传递URL参数一样去传递查询语句:

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

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

{
  "took": 26,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "albb",
        "_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": "albb",
        "_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"
          ]
        }
      }
    ]
  }
}

3.使用DSL语句查询

查询字符串搜索便于通过命令行完成特定(ad hoc)的搜索,但是它也有局限性(参阅简单搜索章节)。Elasticsearch提供丰 富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂、强大的查询。

DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。我们可以这样表示之前关于“Smith”的查询:

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

这会返回与之前查询相同的结果。你可以看到有些东西改变了,我们不再使用查询字符串(query string)做为参数,而是使用 请求体代替。这个请求体使用JSON表示,其中使用了 match 语句(查询类型之一,具体我们以后会学到)。

4.更复杂的搜索

我们让搜索稍微再变的复杂一些。我们依旧想要找到姓氏为“Smith”的员工,但是我们只想得到年龄大于30岁的员工。我们的 语句将添加过滤器(filter),它使得我们高效率的执行一个结构化搜索:

GET /albb/employee/_search 
{ "query" :
    { "bool" : 
       { 
         "filter" : 
          {
               "range" : 
              { 
                 "age" : { "gt" : 30 } <1>
              } 
           },
        "must" : 
          { 
                "match" : { "last_name" : "smith"  } <2>
          } 
       } 
   } 
}
  • <1> 这部分查询属于区间过滤器(range filter),它用于查找所有年龄大于30岁的数据—— gt 为"greater than"的缩写。
  • <2> 这部分查询与之前的 match 语句(query)一致。

现在不要担心语法太多,我们将会在以后详细的讨论。你只要知道我们添加了一个过滤器(filter)用于执行区间搜索,然后重 复利用了之前的 match 语句。现在我们的搜索结果只显示了一个32岁且名字是“Jane Smith”的员工:

{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "albb",
        "_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"
          ]
        }
      }
    ]
  }
}

5.全文搜索

到目前为止搜索都很简单:搜索特定的名字,通过年龄筛选。

让我们尝试一种更高级的搜索,全文搜索——一种传统数据库 很难实现的功能。 我们将会搜索所有喜欢“rock climbing”的员工:

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

你可以看到我们使用了之前的 match 查询,从 about 字段中搜索"rock climbing",我们得到了两个匹配文档:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "albb",
        "_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": "albb",
        "_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"
          ]
        }
      }
    ]
  }
}

这里稍微解释一下 “_score” 这个字段,这个字段的意思是,文档结果与搜索内容相关性得分,也就是这个结果与搜索内容的相关程度,关于如何计算的这就后面再说,暂不讨论!

默认情况下,Elasticsearch根据结果相关性评分来对结果集进行排序,所谓的「结果相关性评分」就是文档与查询条件的匹 配程度。

很显然,排名第一的 John Smithabout 字段明确的写到“rock climbing”。 但是为什么 Jane Smith 也会出现在结果里呢?

原因是“rock”在她的 abuot 字段中被提及了。

因为只有“rock”被提及 而“climbing”没有,所以她的 _score 要低于John

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

结语,是不是感觉很简单呢?

写作不易,喜欢的话关注,点赞,收藏,一波,你们的支持是我继续创作的动力!
在这里插入图片描述

发布了28 篇原创文章 · 获赞 12 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_42002367/article/details/103984466
今日推荐