四十七、Elasticsearch初识搜索引擎-Query DSL

1、Query DSL基础语法

(1)查询全部index下的全部type的全部document

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

(2)查询test_index下的全部type的全部document

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

(3)查询test_index下的test_type下document的test_field字段包含test的

GET /test_index/test_type/_search
{
  "query": {
    "match": {
      "test_field": "test"
    }
  }
}

语法不在重复说明,之前的文章已经说的很明白的。

2、如何组合多个搜索条件?

关键字:bool

需求:搜索title必须包含elasticsearch,content可以包含elasticsearch也可以不包含,author_id必须不为111的document

(1)数据准备

PUT /website/article/1
{
  "title" : "my elasticsearch article",
  "content" : "es is very good",
  "author_id" : 110
}

PUT /website/article/2
{
  "title" : "my hadoop article",
  "content" : "es is very good",
  "author_id" : 111
}

PUT /website/article/3
{
  "title" : "my elasticsearch article",
  "content" : "elasticsearch is very bad",
  "author_id" : 111
}

进行搜索

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

返回结果:

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.25316024,
    "hits": [
      {
        "_index": "website",
        "_type": "article",
        "_id": "1",
        "_score": 0.25316024,
        "_source": {
          "title": "my elasticsearch article",
          "content": "es is very good",
          "author_id": 110
        }
      }
    ]
  }
}

假设should里面又来个bool{must:{match}}这种的,那么代表这个must也是可有可无的,因为他最外层是should

若有兴趣,欢迎来加入群,【Java初学者学习交流群】:458430385,此群有Java开发人员、UI设计人员和前端工程师。有问必答,共同探讨学习,一起进步!
欢迎关注我的微信公众号【Java码农社区】,会定时推送各种干货:


qrcode_for_gh_577b64e73701_258.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,049评论 19 139
  • 人们常说父爱如山,可我从小到大从未得到过父爱,长大后我甚至都不愿提起他,因为父亲是个酒鬼。 从我记事起,父亲就是家...
    张家二YY阅读 1,472评论 17 11
  • 真是怪事!寝室里竟然会有蚊子,而且是这立冬后的节气。合租的小伙笑话我少见多怪,我惊诧不已——他说之前蚊子是在咬他,...
    Michael_Lau阅读 376评论 0 1
  • 1他是一个很干净明朗的男生,衣服鞋子不多但很精致,穿着都很帅气。 2他的头发怎么理都帅,光头也帅,真的很神奇,大概...
    火星小喵阅读 484评论 0 0