第二阶段冲刺
经过五一这个小长假,我们团队对于这个团队项目更有精力了,我们在这个第二阶段要通过Esearch实现对医学文献的精准搜索。
代码如下:
from elasticsearch import Elasticsearch # 与 Elasticsearch 服务器建立连接 es = Elasticsearch(['http://localhost:9200/']) # 定义查询语句 query = { "query": { "bool": { "must": [ {"match": {"title": "heart disease"}}, {"match": {"author": "Smith"}} ], "must_not": [ {"match": {"abstract": "cancer"}} ], "filter": [ {"term": {"journal": "The New England Journal of Medicine"}}, {"range": {"date": {"gte": "2021-01-01", "lte": "2021-12-31"}}} ] } } } # 执行查询 result = es.search(index='articles', body=query) # 打印结果 for hit in result['hits']['hits']: print(hit['_id'], hit['_source']['title'], hit['_source']['author'], hit['_source']['date'])
工作照片: