1. 程式人生 > >【python Elasticsearch】python 操作Elasticsearch之dsl語句查詢

【python Elasticsearch】python 操作Elasticsearch之dsl語句查詢

在這裡插入圖片描述

python 實現上面的查詢語句:

# -*- coding:utf-8 -*-

from elasticsearch import Elasticsearch
import json
es = Elasticsearch()

dsl = {"query":{"bool":{"must":[],"must_not":[],"should":[{"query_string":{"default_field":"_all","query":"漁船"}}]}},"from":0,"size":10,"sort":[],"aggs":{}}

result = es.search(index='news2', doc_type='politics', body=dsl)
print(json.dumps(result, indent=1, ensure_ascii=False))

查詢結果:

{
  "_shards": {
    "successful": 5,
    "total": 5,
    "failed": 0
  },
  "took": 3,
  "timed_out": false,
  "hits": {
    "total": 1,
    "hits": [
      {
        "_type": "politics",
        "_score": 0.08184852,
        "_source": {
          "url": "https://news.qq.com/a/20111216/001044.htm",
          "title": "中韓漁警衝突調查:韓警平均每天扣1艘中國漁船",
          "date": "2011-12-17"
        },
        "_index": "news2",
        "_id": "AWcayRM2HuPv2Z-oKjDg"
      }
    ],
    "max_score": 0.08184852
  }
}

Process finished with exit code 0