Python-ElasticSearch搜索查询实例:搜索用法

2019-02-2519:39:25后端程序开发Comments2,853 views字数 3038阅读模式

Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上。 Lucene 可能是目前存在的,不论开源还是私有的,拥有最先进,高性能和全功能搜索引擎功能的库。但是 Lucene 仅仅只是一个库。为了利用它,你需要编写 Java 程序,并在你的 java 程序里面直接集成 Lucene 包。 更坏的情况是,你需要对信息检索有一定程度的理解才能明白 Lucene 是怎么工作的。Lucene 是 很 复杂的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

在上一篇文章中介绍了ElasticSearch的简单使用,接下来记录一下ElasticSearch的查询:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

查询所有数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

# 搜索所有数据
es.search(index="my_index",doc_type="test_type")
# 或者
body = {
  "query":{
    "match_all":{}
  }
}
es.search(index="my_index",doc_type="test_type",body=body)

term与terms文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

# term
body = {
  "query":{
    "term":{
      "name":"python"
    }
  }
}
# 查询name="python"的所有数据
es.search(index="my_index",doc_type="test_type",body=body)
# terms
body = {
  "query":{
    "terms":{
      "name":[
        "python","android"
      ]
    }
  }
}
# 搜索出name="python"或name="android"的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

match与multi_match文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

# match:匹配name包含python关键字的数据
body = {
  "query":{
    "match":{
      "name":"python"
    }
  }
}
# 查询name包含python关键字的数据
es.search(index="my_index",doc_type="test_type",body=body)
# multi_match:在name和addr里匹配包含深圳关键字的数据
body = {
  "query":{
    "multi_match":{
      "query":"深圳",
      "fields":["name","addr"]
    }
  }
}
# 查询name和addr包含"深圳"关键字的数据
es.search(index="my_index",doc_type="test_type",body=body)

ids文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

body = {
  "query":{
    "ids":{
      "type":"test_type",
      "values":[
        "1","2"
      ]
    }
  }
}
# 搜索出id为1或2d的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

复合查询bool文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

bool有3类查询关系,must(都满足),should(其中一个满足),must_not(都不满足)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

body = {
  "query":{
    "bool":{
      "must":[
        {
          "term":{
            "name":"python"
          }
        },
        {
          "term":{
            "age":18
          }
        }
      ]
    }
  }
}
# 获取name="python"并且age=18的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

切片式查询文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

body = {
  "query":{
    "match_all":{}
  }
  "from":2  # 从第二条数据开始
  "size":4  # 获取4条数据
}
# 从第2条数据开始,获取4条数据
es.search(index="my_index",doc_type="test_type",body=body)

范围查询文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

body = {
  "query":{
    "range":{
      "age":{
        "gte":18,    # >=18
        "lte":30    # <=30
      }
    }
  }
}
# 查询18<=age<=30的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

前缀查询文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

body = {
  "query":{
    "prefix":{
      "name":"p"
    }
  }
}
# 查询前缀为"赵"的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

通配符查询文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

body = {
  "query":{
    "wildcard":{
      "name":"*id"
    }
  }
}
# 查询name以id为后缀的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

排序文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

body = {
  "query":{
    "match_all":{}
  }
  "sort":{
    "age":{         # 根据age字段升序排序
      "order":"asc"    # asc升序,desc降序
    }
  }
}

filter_path文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

响应过滤文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

# 只需要获取_id数据,多个条件用逗号隔开
es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._id"])
# 获取所有数据
es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._*"])

count文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

执行查询并获取该查询的匹配数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

# 获取数据量
es.count(index="my_index",doc_type="test_type")

度量类聚合文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html

  • 获取最小值
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查询
    "min_age":{         # 最小值的key
      "min":{         # 最小
        "field":"age"    # 查询"age"的最小值
      }
    }
  }
}
# 搜索所有数据,并获取age最小的值
es.search(index="my_index",doc_type="test_type",body=body)
  • 获取最大值
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查询
    "max_age":{         # 最大值的key
      "max":{         # 最大
        "field":"age"    # 查询"age"的最大值
      }
    }
  }
}
# 搜索所有数据,并获取age最大的值
es.search(index="my_index",doc_type="test_type",body=body)
  • 获取和
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查询
    "sum_age":{         # 和的key
      "sum":{         # 和
        "field":"age"    # 获取所有age的和
      }
    }
  }
}
# 搜索所有数据,并获取所有age的和
es.search(index="my_index",doc_type="test_type",body=body)
  • 获取平均值
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查询
    "avg_age":{         # 平均值的key
      "sum":{         # 平均值
        "field":"age"    # 获取所有age的平均值
      }
    }
  }
}
# 搜索所有数据,获取所有age的平均值
es.search(index="my_index",doc_type="test_type",body=body)
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9697.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/9697.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定