Elasticsearch DSL查询

在Elasticsearch中,通过使用基于JSON的查询进行搜索。
查询由两个子句组成 -

  • 叶查询子句 - 这些子句是匹配,项或范围的,它们在特定字段中查找特定值。
  • 复合查询子句 - 这些查询是叶查询子句和其他复合查询的组合,用于提取所需的信息。

Elasticsearch支持大量查询。 查询从查询关键字开始,然后以JSON对象的形式在其中包含条件和过滤器。以下描述了不同类型的查询 -

_search端点

现在已经把一些信息放入了索引,可以通过搜索看看是否可找到它们。 为了使用ElasticSearch进行搜索,我们使用_search端点,可选择使用索引和类型。也就是说,按照以下模式向URL发出请求://_search。其中,index和type都是可选的。换句话说,为了搜索相关信息,可以对以下任一URL进行POST请求:
http://localhost:9200/_search - 搜索所有索引和所有类型。http://localhost:9200/accounts/_search - 在索引中搜索所有类型http://localhost:9200/accounts/person/_search - 在索引中显式搜索电影类型的文档。

因为我们只有一个单一的索引和单一的类型,所以怎么使用都不会有什么问题。为了简洁起见使用第一个URL。

查询语法

如果只是发送一个请求到上面的URL,我们会得到所有的电影信息。为了创建更有用的搜索请求,还需要向请求正文中提供查询。 请求正文是一个JSON对象,除了其它属性以外,它还要包含一个名称为“query”的属性,这就可使用ElasticSearch的查询DSL。

{
    "query": {
        //Query DSL here
    }
}

JSON你可能想知道查询DSL是什么。它是ElasticSearch自己基于JSON的域特定语言,可以在其中表达查询和过滤器。想象ElasticSearch它像关系数据库的SQL

匹配所有查询

这是最基本的查询; 它返回所有内容,并为每个对象的分数为1.0。 例如,

GET users*/_search?filter_path=hits.hits
{
   "query":{
      "match_all":{}
   }
}
{
  "hits" : {
    "hits" : [
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "user" : "王五",
          "title" : "运营",
          "desc" : "运营人员",
          "age" : 25
        }
      },
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "user" : "张三",
          "title" : "工程师",
          "desc" : "数据库库存"
        }
      },
      {
        "_index" : "users-profile",
        "_type" : "person",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "user" : "李四",
          "title" : "工程师",
          "desc" : "数据库库存"
        }
      }
    ]
  }
}

全文查询

这些查询用于搜索整个文本,如章节或新闻文章。 此查询根据与特定索引或文档相关联的分析器一起工作。 在本节中,我们将讨论不同类型的全文查询。

  1. 匹配查询
    此查询将文本或短语与一个或多个字段的值匹配。 例如,
GET users/_search?filter_path=hits.hits
{
   "query":{
      "match" : {
         "user":"张三"
      }
   }
}
{
  "hits" : {
    "hits" : [
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "user" : "张三",
          "title" : "工程师",
          "desc" : "数据库库存"
        }
      }
    ]
  }
}
  1. multi_match查询
    此查询将文本或短语与多个字段匹配。 例如,
GET users/_search?filter_path=hits.hits
{
   "query":{
      "multi_match" : {
         "query": "张",
         "fields": [ "user", "desc" ]
      }
   }
}
{
  "hits" : {
    "hits" : [
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "user" : "张三",
          "title" : "工程师",
          "desc" : "数据库库存"
        }
      }
    ]
  }
}
  1. 字符串查询

常规查询

GET users/_search?filter_path=hits.hits
{
   "query":{
      "query_string":{
         "query":"师"
      }
   }
}
{
  "hits" : {
    "hits" : [
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "user" : "张三",
          "title" : "工程师",
          "desc" : "数据库库存"
        }
      }
    ]
  }
}

匹配属性

GET users*/_search?filter_path=hits.hits
{
    "query":{
        "query_string":{
            "query":"库",
            "fields":[
                "desc"
            ]
        }
    }
}
{
  "hits" : {
    "hits" : [
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "1",
        "_score" : 0.39556286,
        "_source" : {
          "user" : "张三",
          "title" : "工程师",
          "desc" : "数据库库存"
        }
      },
      {
        "_index" : "users-profile",
        "_type" : "person",
        "_id" : "1",
        "_score" : 0.39556286,
        "_source" : {
          "user" : "李四",
          "title" : "工程师",
          "desc" : "数据库库存"
        }
      }
    ]
  }
}
  1. 期限等级查询
    这些查询主要处理结构化数据,如数字,日期和枚举。 例如,
GET users*/_search?filter_path=hits.hits
{
   "query":{
      "term":{"age":25}
   }
}
{
  "hits" : {
    "hits" : [
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "user" : "王五",
          "title" : "运营",
          "desc" : "运营人员",
          "age" : 25
        }
      }
    ]
  }
}
  1. 范围查询
    此查询用于查找值的范围之间的值的对象。 为此,需要使用类似
    gte − 大于和等于
    gt − 大于
    lte − 小于和等于
    lt − 小于
GET users*/_search?filter_path=hits.hits
{
   "query":{
      "range":{
         "age":{
            "gte":"23"
         }
      }
   }
}
{
  "hits" : {
    "hits" : [
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "user" : "王五",
          "title" : "运营",
          "desc" : "运营人员",
          "age" : 25
        }
      }
    ]
  }
}
  1. 复合查询
    这些查询是通过使用如和,或,非和或等,用于不同索引或具有函数调用等的布尔运算符彼此合并的不同查询的集合。例如,
GET users*/_search?filter_path=hits.hits
{
    "query":{
        "bool":{
            "must":{
                "multi_match":{
                    "fields":[
                        "desc"
                    ],
                    "query":"库"
                }
            },
            "filter":{
                "terms":{
                    "user":[
                        "三"
                    ]
                }
            }
        }
    }
}
{
  "hits" : {
    "hits" : [
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "1",
        "_score" : 0.39556286,
        "_source" : {
          "user" : "张三",
          "title" : "工程师",
          "desc" : "数据库库存"
        }
      }
    ]
  }
}
  1. 类型查询
    用于特定类型(type)
GET users*/_search?filter_path=hits.hits
{
   "query":{
      "type" : {
         "value" : "person"
      }
   }
}
{
  "hits" : {
    "hits" : [
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "user" : "王五",
          "title" : "运营",
          "desc" : "运营人员",
          "age" : 25
        }
      },
      {
        "_index" : "users",
        "_type" : "person",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "user" : "张三",
          "title" : "工程师",
          "desc" : "数据库库存"
        }
      },
      {
        "_index" : "users-profile",
        "_type" : "person",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "user" : "李四",
          "title" : "工程师",
          "desc" : "数据库库存"
        }
      }
    ]
  }
}

在这里插入图片描述


上一篇文章:Elasticsearch API约定


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值