ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python Elasticsearch DSL 搜索

2021-12-18 22:31:13  阅读:209  来源: 互联网

标签:Search title Python python DSL Elasticsearch query match dict


  使用ElasticSearch DSL进行搜索

  Search主要包括:

  查询(queries)过滤器(filters)聚合(aggreations)排序(sort)分页(pagination)额外的参数(additional parameters)相关性(associated)

  创建一个查询对象

  from elasticsearch import Elasticsearch

  from elasticsearch_dsl import Search

  client=Elasticsearch()

  s=Search(using=client)

  初始化测试数据

  # 创建一个查询语句s=Search().using(client).query("match", title="python")# 查看查询语句对应的字典结构print(s.to_dict())# {'query': {'match': {'title': 'python'}}}# 发送查询请求到Elasticsearchresponse=s.execute()# 打印查询结果for hit in s: print(hit.title)# Out:Python is good!Python very quickly# 删除查询s.delete()

  第一个查询语句

  # 创建一个查询语句

  s=Search().using(client).query("match", title="python")

  # 查看查询语句对应的字典结构

  print(s.to_dict())

  # {'query': {'match': {'title': 'python'}}}

  # 发送查询请求到Elasticsearch

  response=s.execute()

  # 打印查询结果

  for hit in s:

  print(hit.title)

  # Out:

  Python is good!

  Python very quickly

  # 删除查询

  s.delete()

  1、Queries

  # 创建一个多字段查询

  multi_match=MultiMatch(query='python', fields=['title', 'body'])

  s=Search().query(multi_match)

  print(s.to_dict())

  # {'query': {'multi_match': {'fields': ['title', 'body'], 'query': 'python'}}}

  # 使用Q语句

  q=Q("multi_match", query='python', fields=['title', 'body'])

  # 或者

  q=Q({"multi_match": {"query": "python", "fields": ["title", "body"]}})

  s=Search().query(q)

  print(s.to_dict())

  # If you already have a query object, or a dict

  # representing one, you can just override the query used

  # in the Search object:

  s.query=Q('bool', must=[Q('match', title='python'), Q('match', body='best')])

  print(s.to_dict())

  # 查询组合

  q=Q("match", title='python') | Q("match", title='django')

  s=Search().query(q)

  print(s.to_dict())

  # {"bool": {"should": [...]}}

  q=Q("match", title='python') & Q("match", title='django')

  s=Search().query(q)

  print(s.to_dict())

  # {"bool": {"must": [...]}}

  q=~Q("match", title="python")

  s=Search().query(q)

  print(s.to_dict())

  # {"bool": {"must_not": [...]}}2、Filters

  s=Search()

  s=s.filter('terms', tags=['search', 'python'])

  print(s.to_dict())

  # {'query': {'bool': {'filter': [{'terms': {'tags': ['search', 'python']}}]}}}

  s=s.query('bool', filter=[Q('terms', tags=['search', 'python'])])

  print(s.to_dict())

  # {'query': {'bool': {'filter': [{'terms': {'tags': ['search', 'python']}}]}}}

  s=s.exclude('terms', tags=['search', 'python'])

  # 或者

  s=s.query('bool', filter=[~Q('terms', tags=['search', 'python'])])

  print(s.to_dict())

  # {'query': {'bool': {'filter': [{'bool': {'must_not': [{'terms': {'tags': ['search', 'python']}}]}}]}}}3、Aggregations

  s=Search()

  a=A('terms', filed='title')

  s.aggs.bucket('title_terms', a)

  print(s.to_dict())

  # {

  # 'query': {

  # 'match_all': {}

  # },

  # 'aggs': {

  # 'title_terms': {

  # 'terms': {'filed': 'title'}

  # }

  # }

  # }

  # 或者

  s=Search()

  s.aggs.bucket('articles_per_day', 'date_histogram', field='publish_date', interval='day') \

  .metric('clicks_per_day', 'sum', field='clicks') \

  .pipeline('moving_click_average', 'moving_avg', buckets_path='clicks_per_day') \

  .bucket('tags_per_day', 'terms', field='tags')

  s.to_dict()

  # {

  # "aggs": {

  # "articles_per_day": {

  # "date_histogram": { "interval": "day", "field": "publish_date" },

  # "aggs": {

  # "clicks_per_day": { "sum": { "field": "clicks" } },

  # "moving_click_average": { "moving_avg": { "buckets_path": "clicks_per_day" } },

  # "tags_per_day": { "terms": { "field": "tags" } }

  # }

  # }

  # }

  # }4、Sorting

  s=Search().sort(

  'category',

  '-title',

  {"lines" : {"order" : "asc", "mode" : "avg"}}

  )5、Pagination

  s=s[10:20]

  # {"from": 10, "size": 10}6、Extra Properties and parameters

  s=Search()

  # 设置扩展属性使用`.extra()`方法

  s=s.extra(explain=True)

  # 设置参数使用`.params()`

  s=s.params(search_type="count")

  # 如要要限制返回字段,可以使用`source()`方法

  # only return the selected fields

  s=s.source(['title', 'body'])

  # don't return any fields, just the metadata

  s=s.source(False)

  # explicitly include/exclude fields

  s=s.source(include=["title"], exclude=["user.*"])

  # reset the field selection

  s=s.source(None)

  # 使用dict序列化一个查询

  s=Search.from_dict({"query": {"match": {"title": "python"}}})

  # 修改已经存在的查询

  s.update_from_dict({"query": {"match": {"title": "python"}}, "size": 42})

标签:Search,title,Python,python,DSL,Elasticsearch,query,match,dict
来源: https://www.cnblogs.com/linjingyg/p/15706268.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有