欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Elasticsearch 实战Query DSL搜索语法

程序员文章站 2022-07-05 14:26:53
...

Elasticsearch实战

本课时主要介绍以下三种语法

must 必须满足;

should 如果包含了must或者filter查询,那么should的查询语句就不是或者的意思了,而是有或者没有都行的含义;使用minimum_should_match,至少匹配一项should子句;

must_not 必须不满足;

step1

准备三条介绍技术的文档

POST /technology/type/_bulk
{"index":{"_id":1}}
{"title":"ES搜索引擎","content":"Elasticsearch是一个基于Lucene的搜索服务器。","creator":"Lucene","cost":60}
{"index":{"_id":2}}
{"title":"Solr搜索引擎","content":"Solr是一个高性能,采用Java开发,基于Lucene的全文搜索服务器","creator":"Lucene","cost":20}
{"index":{"_id":3}}
{"title":"RocketMQ","content":"消息队列 RocketMQ 版(原ONS)是阿里云基于 Apache RocketMQ构建的低延迟、高并发、高可用、高可靠的分布式消息中间件。","creator":"Alibaba","cost":30}

step2

需求查询‘’搜索引擎“,内容可以包含“消息队列”也可以不包含,不可以来自Alibaba

GET /technology/type/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "搜索引擎"
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": "消息队列"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "creator": "Alibaba"
          }
        }
      ]
    }
  }
}

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 530,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.6365592,
    "hits" : [
      {
        "_index" : "technology",
        "_type" : "type",
        "_id" : "1",
        "_score" : 1.6365592,
        "_source" : {
          "title" : "ES搜索引擎",
          "content" : "Elasticsearch是一个基于Lucene的搜索服务器。",
          "creator" : "Lucene",
          "cost" : 60
        }
      },
      {
        "_index" : "technology",
        "_type" : "type",
        "_id" : "2",
        "_score" : 1.6365592,
        "_source" : {
          "title" : "Solr搜索引擎",
          "content" : "Solr是一个高性能,采用Java开发,基于Lucene的全文搜索服务器",
          "creator" : "Lucene",
          "cost" : 20
        }
      }
    ]
  }
}
 

step3

使用"minimum_should_match": 1

GET /technology/type/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "搜索引擎"
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": "消息队列"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "creator": "Alibaba"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

step4

没有must或者filter查询表示或

GET /technology/type/_search
{
  "query": {
    "bool": {
      
      "should": [
        {
          "match": {
            "creator": "Lucene"
          }
        },
        {
          "match": {
            "title": "RocketMQ"
          }
        }
      ]
    }
  }
}

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.3962393,
    "hits" : [
      {
        "_index" : "technology",
        "_type" : "type",
        "_id" : "3",
        "_score" : 1.3962393,
        "_source" : {
          "title" : "RocketMQ",
          "content" : "消息队列 RocketMQ 版(原ONS)是阿里云基于 Apache RocketMQ构建的低延迟、高并发、高可用、高可靠的分布式消息中间件。",
          "creator" : "Alibaba",
          "cost" : 30
        }
      },
      {
        "_index" : "technology",
        "_type" : "type",
        "_id" : "1",
        "_score" : 0.4700036,
        "_source" : {
          "title" : "ES搜索引擎",
          "content" : "Elasticsearch是一个基于Lucene的搜索服务器。",
          "creator" : "Lucene",
          "cost" : 60
        }
      },
      {
        "_index" : "technology",
        "_type" : "type",
        "_id" : "2",
        "_score" : 0.4700036,
        "_source" : {
          "title" : "Solr搜索引擎",
          "content" : "Solr是一个高性能,采用Java开发,基于Lucene的全文搜索服务器",
          "creator" : "Lucene",
          "cost" : 20
        }
      }
    ]
  }
}
 

step5

GET /technology/type/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "creator": "Lucene"
          }
        },
        {
          "match": {
            "title": "RocketMQ"
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
 

 

欢迎关注公众号《小马JAVA》

Elasticsearch 实战Query DSL搜索语法