Python ES查询

 

数据检索功能

1
2
# rs = es.search(index="index", doc_type="type", filter_path=["hits.hits._*"])
rs = es.search(index='shang', filter_path=['hits.hits._*'], size=30, from_=3)<br>print(json.dumps(rs, indent=2, ensure_ascii=False))

常用参数

  • index - 索引名
  • q - 查询指定匹配 使用Lucene查询语法
  • from_ - 查询起始点  默认0
  • doc_type - 文档类型
  • size - 指定查询条数 默认10
  • field - 指定字段 逗号分隔
  • sort - 排序  字段:asc/desc
  • body - 使用Query DSL
  • scroll - 滚动查询

 

1、range 范围查询

  • gt: > 大于
  • lt: < 小于
  • gte: >= 大于或等于
  • lte: <= 小于或等于
  • include_lower: 是否包含范围的左边界,默认是true
  • include_upper: 是否包含范围的右边界,默认是true
复制代码
# 查询年龄1岁到17岁
query = {
    'query': {
        'range': {
            'age': {
                "from": "1",
                "to": "17",
                "include_lower": 'true',    # 意思是包括1
                "include_upper": 'false'    # 不包括17
            }
        }
    }
}

# 查询年龄1岁到17岁

query = {
    'query': {
        'range': {
            'age': {
                'lt': 17,
                'gte': 1
            }
        }
    }
}

# 匹配的所有文档
allDoc = es.search(index='index', doc_type='type', body=query)
print(json.dumps(allDoc['hits']['hits'], indent=2, ensure_ascii=False))
View Code
复制代码

2、match 查询

  • match query:知道分词器的存在, 会对field进行分词操作,然后再查询
  • match_all:查询所有文档
  • multi_match:可以指定多个字段
  • match_phrase:短语匹配查询

 

复制代码
# match查询
# 匹配name包含"python"关键字的数据
query = {
    'query': {
        'match': {
            "name": "python",
        }
    }
}


# match_all 查找所有文档
query = {
    'query': {
        'match_all': {}
    }
}


# 切片查询
query = {
    'query': {
        'match_all': {
              'age': 18            
    }
    },
    "from": 2,    # 从第二条数据开始
    "size": 4    # 获取4条数据
}    


# multi_match 多字段搜索
# 查询name和addr包含"深圳"关键字的数据
query = {
    'query': {
        "multi_match": {
            "query": "深圳",
            "fields": ["name", "addr"]
        }
    }
}

# 查询name中包含’深圳‘的
query = {
    'query': {
        "match_phrase": {
            "name": "深圳",
        }
    }
}    


# match_phrase和match的区别是这里是真正包含'深圳',而不是只要满足其中一个字就会被模糊命中
View Code
复制代码

 

3、term 查询

  • term query:不知道分词器的存在, 会确切寻找
  • term:查询满足条件的文档(相等关系而不是包含关系)
  • terms:查询多个满足条件的文档
复制代码
# term 匹配一个    查询 xx = “xx”
# 查询name="python"的所有数据
query1 = {
    'query': {
        'term': {
            "name": "python"
        }
    }
}


# terms 多个条件   # terms: 查询 xx = “xx” 或 xx = “yy”
# 查询出name="c"或name="ios"的所有数据
query2 = {
    'query': {
        'terms': {
            "name": [
                'c',
                'ios'
            ]
        }
    }
}


allDoc = es.search(index='index', doc_type='type', body=query)
print(json.dumps(allDoc['hits']['hits'], indent=2, ensure_ascii=False))
View Code
复制代码

 4、bool 查询

  • must  and
  • must_not  not
  • should  or  
复制代码
query = {
    'query': {
        "bool": {
            "must": [
                {
                    "term": {
                        "name": "python"
                    }
                },
                {
                    "term": {
                        "age": 18
                    }
                }
            ]
        }
    },
    "sort": {
        "time": {
            "order": "desc"
        }
    }
}

# 获取name="python"并且age=18的所有数据
# allDoc = es.search(index='index', doc_type='type', body=query)
# print(json.dumps(allDoc['hits']['hits'], indent=2, ensure_ascii=False))
View Code
复制代码

 5、prefix 前缀匹配查询

1
2
3
4
5
6
7
8
9
{
    "query":{
        "prefix":{
        "title":{
        "value":"r"      # 匹配title 字段 r开头的
        }
    }
}
}
posted @   长乐未央丫  阅读(1131)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示