Code前端首页关于Code前端联系我们

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

terry 2年前 (2023-09-25) 阅读数 48 #后端开发

Elasticsearch 是一个基于全文搜索引擎库 Apache Lucene™ 构建的开源搜索引擎。 Lucene 可能是当今最先进、高性能且功能齐全的搜索引擎库,无论是开源的还是专有的。但Lucene只是一个库。要利用它,您必须编写一个 Java 程序并将 Lucene 包直接集成到您的 java 程序中。更糟糕的情况是,您需要对信息检索有一定程度的了解才能了解 Lucene 的工作原理。 Lucene 非常复杂。

在上一篇文章中,我们介绍了ElasticSearch的简单使用。接下来,记录ElasticSearch查询:

查询所有数据

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

terms和terms

# 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

# 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

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

Bool查询组件

满足的查询必须有三种类型),应该be(满足其中之一)、must_not满足(不满足)

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)

切片查询

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

范围查询

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

前缀查询

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

通配符查询

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

排序‾thResponsfil过滤器

# 只需要获取_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

执行查询并获取类聚合查询的匹配数度量查询查询查询设置最小值etget最大值

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)

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门