ZBLOG

golang es 匹配查询,范围查询,多条件查询

在 Elasticsearch 中,可以使用各种查询来搜索和过滤文档。以下是一些常见的查询类型:

匹配查询

匹配查询是最简单和最基本的查询类型之一,用于查找一个或多个字段中包含指定字符串的文档。在 Go 中,可以使用 MatchQuery 来执行匹配查询。

  1. import (
  2. "context"
  3. "fmt"
  4. "github.com/elastic/go-elasticsearch/v7"
  5. "github.com/elastic/go-elasticsearch/v7/esapi"
  6. )
  7. func main() {
  8. es, _ := elasticsearch.NewDefaultClient()
  9. var buf bytes.Buffer
  10. query := map[string]interface{}{
  11. "query": map[string]interface{}{
  12. "match": map[string]interface{}{
  13. "name": "John",
  14. },
  15. },
  16. }
  17. if err := json.NewEncoder(&buf).Encode(query); err != nil {
  18. log.Fatalf("Error encoding query: %s", err)
  19. }
  20. res, err := es.Search(
  21. es.Search.WithContext(context.Background()),
  22. es.Search.WithIndex("my-index"),
  23. es.Search.WithBody(&buf),
  24. es.Search.WithTrackTotalHits(true),
  25. es.Search.WithPretty(),
  26. )
  27. if err != nil {
  28. log.Fatalf("Error getting response: %s", err)
  29. }
  30. }

范围查询

范围查询用于查找指定字段中符合指定范围条件的文档。在 Go 中,可以使用 RangeQuery 来执行范围查询。

  1. import (
  2. "context"
  3. "fmt"
  4. "github.com/elastic/go-elasticsearch/v7"
  5. "github.com/elastic/go-elasticsearch/v7/esapi"
  6. )
  7. func main() {
  8. es, _ := elasticsearch.NewDefaultClient()
  9. var buf bytes.Buffer
  10. query := map[string]interface{}{
  11. "query": map[string]interface{}{
  12. "range": map[string]interface{}{
  13. "age": map[string]interface{}{
  14. "gte": 30,
  15. "lte": 40,
  16. },
  17. },
  18. },
  19. }
  20. if err := json.NewEncoder(&buf).Encode(query); err != nil {
  21. log.Fatalf("Error encoding query: %s", err)
  22. }
  23. res, err := es.Search(
  24. es.Search.WithContext(context.Background()),
  25. es.Search.WithIndex("my-index"),
  26. es.Search.WithBody(&buf),
  27. es.Search.WithTrackTotalHits(true),
  28. es.Search.WithPretty(),
  29. )
  30. if err != nil {
  31. log.Fatalf("Error getting response: %s", err)
  32. }
  33. }

多条件查询

多条件查询允许同时使用多个查询条件来过滤文档。在 Go 中,可以使用 BoolQuery 来执行多条件查询。

  1. import (
  2. "context"
  3. "fmt"
  4. "github.com/elastic/go-elasticsearch/v7"
  5. "github.com/elastic/go-elasticsearch/v7/esapi"
  6. )
  7. func main() {
  8. es, _ := elasticsearch.NewDefaultClient()
  9. var buf bytes.Buffer
  10. query := map[string]interface{}{
  11. "query": map[string]interface{}{
  12. "bool": map[string]interface{}{
  13. "must": []map[string]interface{}{ // 所有子条件必须匹配
  14. {"match": {"name": "John"}},
  15. {"range": {"age": {"gte": 30}}},
  16. },
  17. "should": []map[string]interface{}{ // 至少有一个子条件匹配
  18. {"match_phrase_prefix":{"description":"quick brown fox"}},
  19. {"term":{"is_published":true}},
  20. },
  21. "must_not": []map[string]interface{}{ // 所有子条件都不匹配
  22. {"match": {"gender": "female"}},
  23. },
  24. },
  25. },
  26. }
  27. if err := json.NewEncoder(&buf).Encode(query); err != nil {
  28. log.Fatalf("Error encoding query: %s", err)
  29. }
  30. res, err := es.Search(
  31. es.Search.WithContext(context.Background()),
  32. es.Search.WithIndex("my-index"),
  33. es.Search.WithBody(&buf),
  34. es.Search.WithTrackTotalHits(true),
  35. es.Search.WithPretty(),
  36. )
  37. if err != nil {
  38. log.Fatalf("Error getting response: %s", err)
  39. }
  40. }

以上是 Elasticsearch 中常见的查询类型和 Go 客户端库中对应的使用方法。可以根据具体需求来选择合适的查询类型和选项来搜索和过滤文档。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=6768

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?