elasticsearch学习笔i记(二十六)——Elasticsearch query DSL搜索实战

栏目: 后端 · 发布时间: 5年前

内容简介:下面先解释一下ES的GET+request body这种模式一般我们知道HTTP协议一般是不允许get请求带上request body,但是因为get更加适合描述查询数据的操作,因此还是这么用了。碰巧当前很多浏览器或是服务器也都支持GET+request body模式

下面先解释一下ES的GET+request body这种模式

一般我们知道HTTP协议一般是不允许get请求带上request body,但是因为get更加适合描述查询数据的操作,因此还是这么用了。

碰巧当前很多浏览器或是服务器也都支持GET+request body模式

如果遇到不支持的场景,也可以用POST+request body模式.

1、 一个例子让你明白什么是query DSL

GET /website/_search
{
  "query": {
    "match_all": {}
  }
}

2、query DSL的基本语法

GET /{index}/_search
{
    "各种条件"
}

示例:

GET /website/_search
{
  "query": {
    "match": {
      "title": "article"
    }
  }
}
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.13353139,
    "hits" : [
      {
        "_index" : "website",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.13353139,
        "_source" : {
          "post_date" : "2017-01-01",
          "title" : "my first article",
          "content" : "this is my first article in this website",
          "author_id" : 11400
        }
      },
      {
        "_index" : "website",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.13353139,
        "_source" : {
          "post_date" : "2017-01-02",
          "title" : "my second article",
          "content" : "this is my second article in this website",
          "author_id" : 11400
        }
      },
      {
        "_index" : "website",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.13353139,
        "_source" : {
          "post_date" : "2017-01-03",
          "title" : "my third article",
          "content" : "this is my third article in this website",
          "author_id" : 11400
        }
      }
    ]
  }
}

3、组合多个搜索条件

搜索需求:title必须包含first,content可以包含website也可以不包含,author_id必须不为111

GET /website/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "first"
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": "website"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "author_id": 111
          }
        }
      ]
    }
  }
}
{
  "took" : 21,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.1143606,
    "hits" : [
      {
        "_index" : "website",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.1143606,
        "_source" : {
          "post_date" : "2017-01-01",
          "title" : "my first article",
          "content" : "this is my first article in this website",
          "author_id" : 11400
        }
      }
    ]
  }
}

下面附上一个更复杂的组合查询,之后可以作为思考的模板:

GET /website/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "first"
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": "website"
          }
        },
        {
          "bool": {
            "must": [
              {
                "match": {
                  "content": "first"
                }
              }
            ],
            "must_not": [
              {
                "match": {
                  "title": "second"
                }
              }
            ]
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "author_id": 111
          }
        }
      ]
    }
  }
}

以上所述就是小编给大家介绍的《elasticsearch学习笔i记(二十六)——Elasticsearch query DSL搜索实战》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

游戏数据分析的艺术

游戏数据分析的艺术

于洋、余敏雄、吴娜、师胜柱 / 机械工业出版社 / 2015-7 / 79.00

《游戏数据分析的艺术》是中国游戏产业的开创性著作,具有里程碑意义,它首次系统讲解了如何对游戏行业的数据进行分析,在行业里竖起了一根标杆。作者是来自TalkingData等国内顶尖的数据分析机构和西山居这样的知名游戏公司的资深数据分析专家, 对游戏数据从不同的业务角度进行了诠释。本书详细剖析了游戏数据分析相关的指标、方法论、内容挖掘、数据挖掘、软件使用、游戏设计、运营策划、渠道推广、收入解读、用户分......一起来看看 《游戏数据分析的艺术》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具