ElasticSearch系列 - SpringBoot整合ES:查询条件 query 和过滤条件 filter 的区别

这篇具有很好参考价值的文章主要介绍了ElasticSearch系列 - SpringBoot整合ES:查询条件 query 和过滤条件 filter 的区别。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

01. Elasticsearch 查询条件和过滤条件的区别?

Elasticsearch中的查询条件和过滤条件都是用于搜索和过滤文档的条件,但它们之间有一些区别。

查询条件是用于计算文档相关度得分的条件,它会将所有符合条件的文档按照相关度得分从高到低排序,并返回前N个文档。查询条件可以使用各种类型的查询,如match、term、range、bool等。查询条件会计算每个文档的相关度得分,因此查询条件可以用于搜索和排序。

过滤条件是用于过滤文档的条件,它会将所有符合条件的文档返回,但不会计算相关度得分。过滤条件可以使用各种类型的过滤器,如term、range、bool、geo_distance等。过滤条件不会计算相关度得分,因此过滤条件可以用于过滤和聚合。

查询条件和过滤条件的区别在于,查询条件会计算每个文档的相关度得分,而过滤条件不会计算得分。因此,如果只需要过滤文档而不需要计算得分,应该使用过滤条件。另外,过滤条件可以缓存结果,提高查询性能,而查询条件不能缓存结果。

需要注意的是,查询条件和过滤条件都可以使用bool查询和bool过滤器来组合多个条件。bool查询和bool过滤器都是用于组合多个查询或过滤器的逻辑运算符,可以使用must、should、must_not三个子句来组合多个查询或过滤器。

02. ElasticSearch 过滤器的作用和特性?

ElasticSearch中的过滤器(Filter)是一种用于限制查询结果范围的查询类型,它可以根据指定的条件来过滤掉不符合要求的文档,从而提高查询效率。与查询(Query)不同,过滤器不会影响文档的相关性得分,而只是根据指定的条件来判断文档是否符合要求。

过滤器的作用和特性包括:

① 提高查询效率:过滤器可以在查询过程中快速过滤掉不符合要求的文档,从而减少查询的文档数量,提高查询效率。

② 不影响相关性得分:过滤器不会影响文档的相关性得分,因此可以与查询一起使用,同时满足查询条件和过滤条件。

③ 可以缓存:由于过滤器不会影响相关性得分,因此可以缓存过滤器的结果,以提高查询效率。

④ 支持多种条件:ElasticSearch提供了多种不同类型的过滤器,可以根据具体的查询需求选择不同的过滤器类型,如Term过滤器、Range过滤器、Bool过滤器等。

⑤ 可以组合使用:多个过滤器可以组合使用,使用逻辑运算符(如AND、OR、NOT)来定义它们之间的关系,从而实现更复杂的查询需求。

需要注意的是,过滤器虽然可以提高查询效率,但是也会占用一定的内存和CPU资源。因此,在使用过滤器时,需要根据具体的查询需求和系统资源情况进行权衡,选择合适的过滤器类型和参数。

03. ElasticSearch 中常见的过滤器有哪些?

在ElasticSearch中,过滤器(Filter)是一种用于限制查询结果范围的查询类型,它可以根据指定的条件来过滤掉不符合要求的文档,从而提高查询效率。ElasticSearch提供了多种不同类型的过滤器,包括:

① Term过滤器:用于精确匹配某个字段的值,类似于SQL中的等于(=)操作符。

② Range过滤器:用于匹配某个字段的值在指定范围内的文档,可以指定大于、小于、大于等于、小于等于等多种条件。

③ Exists过滤器:用于匹配某个字段存在的文档。

④ Bool过滤器:用于组合多个过滤器,可以使用逻辑运算符(如AND、OR、NOT)来定义它们之间的关系。

⑤ Type过滤器:用于匹配某个文档类型的文档。

⑥ Ids过滤器:用于匹配指定ID的文档。

⑦ Prefix过滤器:用于匹配某个字段以指定前缀开头的文档。

⑧ Wildcard过滤器:用于匹配某个字段符合指定通配符模式的文档。

⑨ Regexp过滤器:用于匹配某个字段符合指定正则表达式的文档。

以上是ElasticSearch中常用的一些过滤器类型,根据具体的查询需求,可以选择不同的过滤器来限制查询结果范围。

04. ElasticSearch term 查询会不会计算评分?

在ElasticSearch中,Term查询是一种精确匹配某个字段的值的查询类型,它不会计算文档的相关性得分(Relevance Score),因此不会影响查询结果的排序。

Term查询是一种非常快速和高效的查询类型,因为它只需要精确匹配某个字段的值,而不需要计算文档的相关性得分。因此,如果需要精确匹配某个字段的值,可以使用Term查询来提高查询效率。

需要注意的是,虽然Term查询不会计算文档的相关性得分,但是它仍然会返回所有匹配的文档,并按照相关性得分从高到低排序。这是因为,ElasticSearch中的查询结果总是会按照相关性得分从高到低排序,即使查询类型不会影响文档的相关性得分。

因此,虽然Term查询不会计算文档的相关性得分,但是它仍然会返回所有匹配的文档,并按照相关性得分从高到低排序。如果不需要排序,可以使用过滤器(Filter)来限制查询结果范围,以提高查询效率。

05. ElasticSearch bool 组合过滤器查询为什么有评分?

在ElasticSearch中,Bool查询可以组合多个查询条件和过滤条件,以实现更复杂的查询需求。当使用Bool查询时,查询结果会根据文档与查询条件的匹配程度计算相关性得分(Relevance Score),并按照相关性得分从高到低排序。

需要注意的是,虽然过滤器(Filter)不会影响文档的相关性得分,但是当过滤器与查询条件组合使用时,查询结果仍然会根据文档与查询条件的匹配程度计算相关性得分。这是因为,过滤器只是用来限制查询结果范围的,而查询条件则用来确定哪些文档匹配查询。因此,即使使用了过滤器,查询结果仍然需要根据文档与查询条件的匹配程度计算相关性得分。

在使用Bool查询时,如果只使用过滤器而不使用查询条件,那么查询结果不会有相关性得分,因为过滤器不会影响文档的相关性得分。但是,如果同时使用了查询条件和过滤器,那么查询结果仍然会有相关性得分,因为查询条件会影响文档的相关性得分。

因此,当使用Bool组合过滤器查询时,查询结果仍然会有相关性得分,这是因为查询条件的存在。如果不需要相关性得分,可以只使用过滤器来限制查询结果范围。

06. ElasticSearch term 查询 与term 过滤器?

term查询:会将所有符合条件的文档按照相关度得分从高到低排序,并返回前N个文档。term查询只能精确匹配某个字段的值,不支持模糊匹配和范围查询。

term过滤器:通常用于不需要计算文档相关度得分的场景,例如精确匹配、范围查询、聚合等。term过滤器会将所有符合条件的文档返回,但不会计算相关度得分。term过滤器只能精确匹配某个字段的值,不支持模糊匹配和范围查询。

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "price":10
}

PUT /my_index/_doc/2
{
  "price":20
}

PUT /my_index/_doc/3
{
  "price":30
}

② 查询字段 price 值包含10的文档,并按照相关度得分从高到低排序:

GET /my_index/_search
{
  "query": {
    "term": {
      "price": {
        "value": 10
      }
    }
  }
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "price" : 10
        }
      }
    ]
  }
}

③ 查询字段 price 值包含10的文档,查询结果不会按照相关度得分排序:

GET /my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "price": 10
        }
      }
    }
  }
}
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "price" : 10
        }
      }
    ]
  }
}

需要注意的是,term查询和term过滤器都是区分大小写的,因此如果字段值的大小写不一致,可能会导致查询结果不准确。另外,如果要查询或过滤的字段是一个text类型的字段,应该使用match查询或match_phrase查询,而不是term查询或term过滤器。因为text类型的字段会进行分词,term查询或term过滤器只能匹配整个词条,无法匹配分词后的单词。

07. ElasticSearch terms 查询和 terms 过滤器?

terms查询:通常用于需要计算文档相关度得分的场景,例如全文搜索、排序等。terms查询会将所有符合条件的文档按照相关度得分从高到低排序,并返回前N个文档。terms查询可以匹配某个字段的值在指定值列表中。

terms过滤器:通常用于不需要计算文档相关度得分的场景,例如精确匹配、范围查询、聚合等。terms过滤器会将所有符合条件的文档返回,但不会计算相关度得分。terms过滤器可以匹配某个字段的值在指定值列表中。

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "price":10
}

PUT /my_index/_doc/2
{
  "price":20
}

PUT /my_index/_doc/3
{
  "price":30
}

② terms 查询:

GET /my_index/_search
{
  "query": {
    "terms": {
      "price": [
        "10",
        "20"
      ]
    }
  }
}
{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "createTime" : "2023-03-29 10:30:11",
          "tag" : "tag1",
          "price" : 10
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "createTime" : "2023-03-29 10:35:11",
          "tag" : "tag2",
          "price" : 20
        }
      }
    ]
  }
}

③ terms 过滤:

GET /my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "price": [
            "10",
            "20"
          ]
        }
      }
    }
  }
}
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "createTime" : "2023-03-29 10:30:11",
          "tag" : "tag1",
          "price" : 10
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "createTime" : "2023-03-29 10:35:11",
          "tag" : "tag2",
          "price" : 20
        }
      }
    ]
  }
}

08. ElasticSearch range 查询与 range 过滤器?

range查询:通常用于需要计算文档相关度得分的场景,例如全文搜索、排序等。range查询会将所有符合条件的文档按照相关度得分从高到低排序,并返回前N个文档。range查询可以匹配某个字段的值在指定范围内,支持数值、日期等类型的范围查询。

range过滤器:通常用于不需要计算文档相关度得分的场景,例如精确匹配、范围查询、聚合等。range过滤器会将所有符合条件的文档返回,但不会计算相关度得分。range过滤器可以匹配某个字段的值在指定范围内,支持数值、日期等类型的范围查询。

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "price":10
}

PUT /my_index/_doc/2
{
  "price":20
}

PUT /my_index/_doc/3
{
  "price":30
}

② 查询 price 字段的值在10到20之间的文档,并按照相关度得分从高到低排序:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "price" : 10
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "price" : 20
        }
      }
    ]
  }
}

③ 查询 price 字段的值在10到20之间的文档,不按照相关度得分排序:

GET /my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "price": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "price" : 10
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "price" : 20
        }
      }
    ]
  }
}

需要注意的是,range查询和range过滤器的语法略有不同,但它们的功能是相同的。另外,range查询和range过滤器都可以用于数值、日期等类型的范围查询,但对于text类型的字段,应该使用match查询或match_phrase查询。

09. ElasticSearch exists 查询与 exists 过滤器?

exists查询:通常用于需要计算文档相关度得分的场景,例如全文搜索、排序等。exists查询会将所有符合条件的文档按照相关度得分从高到低排序,并返回前N个文档。exists查询可以匹配某个字段不为空的文档。

exists过滤器:通常用于不需要计算文档相关度得分的场景,例如精确匹配、范围查询、聚合等。exists过滤器会将所有符合条件的文档返回,但不会计算相关度得分。exists查询可以匹配某个字段不为空的文档。

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "price":10
}

PUT /my_index/_doc/2
{
  "price":20
}

PUT /my_index/_doc/3
{
  "price":30
}

PUT /my_index/_doc/4
{
  "price":null
}

② 查询 price 字段不为空的文档,并按照相关度得分从高到低排序:

GET /my_index/_search
{
  "query": {
    "exists": {
      "field": "price"
    }
  }
}
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "price" : 10
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "price" : 20
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "price" : 30
        }
      }
    ]
  }
}

③ 查询 price 字段不为空的文档,不按照相关度得分排序:

GET /my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "exists": {
          "field": "price"
        }
      }
    }
  }
}
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "price" : 10
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "price" : 20
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "price" : 30
        }
      }
    ]
  }
}

需要注意的是,exists查询和exists过滤器的语法略有不同,但它们的功能是相同的。对于text类型的字段,应该使用match查询或match_phrase查询。

10. ElasticSearch ids 查询和 ids 过滤器?

ids查询:通常用于需要计算文档相关度得分的场景,例如全文搜索、排序等。ids查询会将所有符合条件的文档按照相关度得分从高到低排序,并返回前N个文档。ids查询可以匹配指定文档ID。

ids过滤器:通常用于不需要计算文档相关度得分的场景,例如精确匹配、范围查询、聚合等。ids过滤器会将所有符合条件的文档返回,但不会计算相关度得分。ids过滤器可以匹配指定文档ID。

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "price":10
}

PUT /my_index/_doc/2
{
  "price":20
}

PUT /my_index/_doc/3
{
  "price":30
}

② 查询 id=1或者 id=2的文档,并按照相关度得分从高到低排序:

GET /my_index/_search
{
  "query": {
    "ids": {
      "values": [1,2]
    }
  }
}
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "price" : 10
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "price" : 20
        }
      }
    ]
  }
}

③ 查询 id=1或者 id=2的文档,不按照相关度得分排序:

GET /my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "ids": {
          "values": [
            "1",
            "2"
          ]
        }
      }
    }
  }
}
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "price" : 10
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "price" : 20
        }
      }
    ]
  }
}

11. ElasticSearch bool 查询和 bool 过滤器?

bool查询:通常用于需要计算文档相关度得分的场景,例如全文搜索、排序等。bool查询会将所有符合条件的文档按照相关度得分从高到低排序,并返回前N个文档。bool查询可以组合多个查询,支持must、should、must_not三个子句。

bool过滤器:通常用于不需要计算文档相关度得分的场景,例如精确匹配、范围查询、聚合等。bool过滤器会将所有符合条件的文档返回,但不会计算相关度得分。bool过滤器可以组合多个过滤器,支持must、should、must_not三个子句。

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "createTime":{
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "tag":{
        "type": "keyword"
      },
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "createTime": "2023-03-29 10:30:11",
  "tag": "tag1",
  "price": 10
}

PUT /my_index/_doc/2
{
  "createTime": "2023-03-29 10:35:11",
  "tag": "tag2",
  "price": 20
}

PUT /my_index/_doc/3
{
  "createTime": "2023-03-29 10:38:11",
  "tag": "tag3",
  "price": 30
}

② bool 查询:

GET /my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "tag": {
              "value": "tag1"
            }
          }
        }
      ],
      "should": [
        {
          "range": {
            "price": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ],
      "must": [
        {
          "term": {
            "createTime": {
              "value": "2023-03-29 10:38:11"
            }
          }
        }
      ]
    }
  }
}
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "createTime" : "2023-03-29 10:38:11",
          "tag" : "tag3",
          "price" : 30
        }
      }
    ]
  }
}

③ boo 过滤器:文章来源地址https://www.toymoban.com/news/detail-627462.html

GET /my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "term": {
                "tag": {
                  "value": "tag1"
                }
              }
            }
          ],
          "should": [
            {
              "range": {
                "price": {
                  "gte": 10,
                  "lte": 20
                }
              }
            }
          ],
          "must": [
            {
              "term": {
                "createTime": {
                  "value": "2023-03-29 10:38:11"
                }
              }
            }
          ]
        }
      }
    }
  }
}
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "createTime" : "2023-03-29 10:38:11",
          "tag" : "tag3",
          "price" : 30
        }
      }
    ]
  }
}

12. SpringBoot整合ES实现bool过滤器

GET /my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "term": {
                "tag": {
                  "value": "tag1"
                }
              }
            }
          ],
          "should": [
            {
              "range": {
                "price": {
                  "gte": 10,
                  "lte": 20
                }
              }
            }
          ],
          "must": [
            {
              "term": {
                "createTime": {
                  "value": "2023-03-29 10:38:11"
                }
              }
            }
          ]
        }
      }
    }
  }
}
@Slf4j
@Service
public class ElasticSearchImpl {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public void searchUser() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
 
        // 内部 bool 过滤器
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        // must_not
        TermQueryBuilder termQueryBuilder = new TermQueryBuilder("tag","tag1");
        boolQueryBuilder.mustNot(termQueryBuilder);
        // should
        RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder("price");
        rangeQueryBuilder.gte(10);
        rangeQueryBuilder.lte(20);
        boolQueryBuilder.should(rangeQueryBuilder);
        // must
        termQueryBuilder = new TermQueryBuilder("createTime","2023-03-29 10:38:11");
        boolQueryBuilder.must(termQueryBuilder);

        // 外部 bool 过滤器
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        queryBuilder.filter(boolQueryBuilder);

        searchSourceBuilder.query(queryBuilder);

        SearchRequest searchRequest = new SearchRequest(new String[]{"my_index"},searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse);
    }
}

到了这里,关于ElasticSearch系列 - SpringBoot整合ES:查询条件 query 和过滤条件 filter 的区别的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ElasticSearch系列 - SpringBoot整合ES:多字段查询 multi_match

    1. 什么是 ElasticSearch 的 multi_match 查询? 有时用户需要在多个字段中查询,除了使用布尔查询封装多个match查询之外,可替代的方案是使用multi_match。可以在multi_match的query子句中组织数据匹配规则,并在fields子句中指定需要搜索的字段列表。 以下是一个示例multi-match查询

    2023年04月21日
    浏览(50)
  • ElasticSearch系列 - SpringBoot整合ES:查询字段不为空的文档 exists

    1. ElasticSearch exists 查询是什么 在某些场景下,我们希望找到某个字段不为空的文档,则可以用exists搜索。字段不为空的条件有: 值存在且不是 null; 值不是空数组; 值是数组,但不是 [null] 例如,查询在字段中至少有一个非空值的文档: 这些文档都将匹配上面的查询: ①

    2024年02月06日
    浏览(54)
  • springboot整合elasticsearch8组合条件查询

    整合过程见上一篇文章 springboot整合elasticsearch8 1.es8多条件组合查询 2.使用scroll进行大数据量查询

    2024年02月16日
    浏览(35)
  • 【ElasticSearch-基础篇】ES高级查询Query DSL术语级别查询并结合springboot使用

    Elasticsearch 提供了基于 JSON 的完整 Query DSL(Domain Specific Language)来定义查询。 因Query DSL是利用Rest API传递JSON格式的请求体(RequestBody)数据与ES进行交互,所以我们在使用springboot的时候也可以很方便的进行集成,本文主要讲述的就是使用springboot实现各类DSL的语法查询。 Elastics

    2024年02月01日
    浏览(36)
  • SpringBoot整合Elasticsearch实现分页条件查询及注意事项

    项目环境: springboot 2.3.7.RELEASE es 6.8.3 这里需要注意es中日期格式,ES默认是不支持yyyy-MM-dd HH:mm:ss格式的,需要通过 @Field(type = FieldType.Date, format = DateFormat.custom,pattern = \\\"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_second\\\") 来指定日期格式。 直接看业务层实现分页条件查询: 范围查询: es en

    2023年04月16日
    浏览(41)
  • ElasticSearch序列 - SpringBoot整合ES:范围查询 range

    01. ElasticSearch range查询是什么? Elasticsearch 中的 range 查询可以用于查询某个字段在一定范围内的文档。 range 查询可同时提供包含和不包含这两种范围表达式,可供组合的选项如下: gt : 大于(greater than) lt : 小于(less than) gte : = 大于或等于(greater than or equal to) lte : = 小于

    2024年02月09日
    浏览(30)
  • ElasticSearch序列 - SpringBoot整合ES:根据指定的 ids 查询

    1. ElasticSearch 根据 ids 查询文档 ① 索引文档,构造数据 ② 查询文档 id 为 1 或者 2 的文档: 我们索引文档时,文档的id为整型,为什么查询出来的文档 id为字符串类型呢?如果我们使用字符串类型的文档id查询呢? 可以看到仍然可以查询到匹配的文档。 在Elasticsearch中,文档

    2024年02月11日
    浏览(42)
  • ElasticSearch系列 - SpringBoot整合ES:ElasticSearch分析器

    1. ElasticSearch match 文本搜索的过程? Elasticsearch 的 match 查询是一种基于文本匹配的查询方式,它的搜索过程如下: ① 将查询字符串分词:Elasticsearch 会将查询字符串分成一个个词项(term),并去除停用词(如“的”、“是”等常用词汇)和标点符号等无意义的字符。 ② 构建

    2023年04月18日
    浏览(106)
  • ElasticSearch系列 - SpringBoot整合ES:分析器

    1. ElasticSearch match 文本搜索的过程? Elasticsearch 的 match 查询是一种基于文本匹配的查询方式,它的搜索过程如下: ① 将查询字符串分词:Elasticsearch 会将查询字符串分成一个个词项(term),并去除停用词(如“的”、“是”等常用词汇)和标点符号等无意义的字符。 ② 构建

    2024年02月06日
    浏览(46)
  • Elasticsearch 基本使用(五)查询条件匹配方式(query & query_string)

    ES中常用的查询类型往大了分可以分为简单查询,复合查询,聚合查询等; 而复合查询及聚合查询都是基于简单查询的;简单查询里面对条件的匹配方式又分为不同类型。term[s],match,match_all,match_phrase 等等 term 单词查询,在字段的倒排索引(发生分词)或者直接在字段值(

    2024年02月09日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包