简体   繁体   English

如何使用 Elasticsearch 查询 DSL 来使用 Python 查询 API?

[英]How do I use the Elasticsearch Query DSL to query an API using Python?

I'm trying to query the public Art Institute of Chicago API to only show me results that match certain criteria.我正在尝试查询芝加哥公共艺术学院 API ,只显示符合特定条件的结果。 For example:例如:

  • classification_title = "painting" classification_title = "绘画"
  • colorfulness <= 13 colorfulness <= 13
  • material_titles includes "paper (fiber product)" material_titles包括“纸(纤维产品)”

The API documentation states: API 文档指出:

Behind the scenes, our search is powered by Elasticsearch. You can use its Query DSL to interact with our API.在幕后,我们的搜索由 Elasticsearch 提供支持。您可以使用其查询 DSL 与我们的 API 进行交互。

I can't figure out how to take an Elasticsearch DSL JSON-like object and pass it into the API URL, beyond a single criteria.我无法弄清楚如何获取 Elasticsearch DSL JSON-like object 并将其传递到 API URL,超出了单一标准。

Here are some working single-criteria examples specific to this API:以下是特定于此 API 的一些有效的单一标准示例:

requests.get("https://api.artic.edu/api/v1/artworks/search?q=woodblock[classification_title]").json()
requests.get("https://api.artic.edu/api/v1/artworks/search?q=monet[artist_title]").json()

And here are some of my failed attempts to have return only items that pass 2+ criteria items:以下是我尝试仅返回通过 2+ 个标准项目的项目的一些失败尝试:

requests.get("https://api.artic.edu/api/v1/artworks/search?q=woodblock[classification_title]monet[artist_title]")
requests.get("https://api.artic.edu/api/v1/artworks/search?q=woodblock[classification_title],monet[artist_title]")
requests.get("https://api.artic.edu/api/v1/artworks/search?q=%2Bclassification_title%3A(woodblock)+%2Bartist_title%3A(monet)")

And lastly some of my failed attempts to return more complex criteria, like range:最后,我尝试返回更复杂的标准的一些失败尝试,例如范围:

requests.get("https://api.artic.edu/api/v1/artworks/search?q={range:lte:10}&query[colorfulness]").json()
requests.get("https://api.artic.edu/api/v1/artworks/search?q=<10&query[colorfulness]").json()
requests.get("https://api.artic.edu/api/v1/artworks/search?q=%2Bdate_display%3A%3C1900").json()

All of these failed attempts return data but not within my passed criteria.所有这些失败的尝试都会返回数据,但不在我通过的标准范围内。 For example, woodblock[classification_title]m.net[artist_title] should return no results.例如, woodblock[classification_title]m.net[artist_title]不应返回任何结果。

How could I query all of these criteria, only returning results (if any) that match all these conditions?我如何查询所有这些条件,只返回符合所有这些条件的结果(如果有的话)? The JSON-like Query DSL does not seem compatible with a requests.get .类似 JSON 的查询 DSL似乎与requests.get不兼容。

Solved.解决了。 I was lacking knowledge on GET and POST .我缺乏关于GETPOST的知识。 I can indeed use the JSON-like Query DSL .我确实可以使用类似 JSON 的查询 DSL It just needs to be sent as part of a requests.post instead of a requests.get , like so:它只需要作为requests.post的一部分而不是requests.get发送,如下所示:

import requests

fields = "id,image_id,title,artist_id,classification_title,colorfulness,material_titles"
url = f"https://api.artic.edu/api/v1/artworks/search?&fields={fields}"

criteria = {
    "query": {
        "bool": {
            "must": [
                {"match": {"classification_title": "painting"}},
                {"range": {"colorfulness": {"lte": 13}}},
                {"match": {"material_titles": "paper (fiber product)"}},
            ],
        }
    }
}


r = requests.post(url, json=criteria)
art = r.json()
print(art)

Notice within the requests.post that the desired criteria query is passed through as a json argument, separate from the url .请注意,在requests.post中,所需的条件查询作为json参数传递,与url分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM