Reputation: 6378
I am using a public API to display artworks on frontend. The public API url: https://api.artic.edu/api/v1/artworks/search
To filter artworks that has place_of_origin=France
, I can use this query (works 100% fine): https://api.artic.edu/api/v1/artworks/search?fields=id,api_link,title,description,thumbnail,image_id,place_of_origin&page=1&limit=10&query[match][place_of_origin]=france
To search artworks whose title includes night
, I can use this query (works 100% fine):
https://api.artic.edu/api/v1/artworks/search?fields=id,api_link,title,description,thumbnail,image_id,place_of_origin&page=1&limit=10&query[term][title]=night
Now, If I want to filter artworks that. has place_of_origin=France
as well as I want to search artworks whose title includes night
in the same api call. So, here is the url that I call GET request on: https://api.artic.edu/api/v1/artworks/search?fields=id,api_link,title,description,thumbnail,image_id,place_of_origin&page=1&limit=10&query[term][title]=night&query[match][place_of_origin]=france
It gives me 400 Bad request.
Here is the exact response that I get:
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[term] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
"line": 1,
"col": 416
}
],
"type": "x_content_parse_exception",
"reason": "[1:416] [bool] failed to parse field [must]",
"caused_by": {
"type": "x_content_parse_exception",
"reason": "[1:416] [bool] failed to parse field [should]",
"caused_by": {
"type": "x_content_parse_exception",
"reason": "[1:416] [bool] failed to parse field [must]",
"caused_by": {
"type": "x_content_parse_exception",
"reason": "[1:416] [bool] failed to parse field [must]",
"caused_by": {
"type": "parsing_exception",
"reason": "[term] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
"line": 1,
"col": 416
}
}
}
}
},
"status": 400
}
I think they use elastic search in backend for apis to work. Elastic search document gives me some JSON object. But I don't know how to convert those JSON objects to a url with query params.
Can someone explain me how can I use multiple query together?
Upvotes: 0
Views: 80
Reputation: 6378
I got a solution here
Posting that same code here, so that if link is broken in future, we do not loose access to that code:
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"terms": {
"color": [
"red",
"yellow"
]
}
}
],
"filter": [
{
"match": {
"availability": {
"query": "in stock"
}
}
}
]
}
}
}
But that solution was providing an object. I never knew: how to convert that object to query string params.
One of my friend suggested me to use this converter that can covert JSON to Query String params.
I used this input in the converter:
{
"query": {
"bool": {
"must": [
{
"term": {
"title": "night"
}
}
],
"filter": [
{
"match": {
"place_of_origin": "France"
}
}
]
}
}
}
The output that I received from converter:
query[bool][must][0][term][title]=night&query[bool][filter][0][match][place_of_origin]=France
This solution is working 100% fine, but I am not fully satisfied with the JSON that I got from another answer. So, will not mark my answer as accepted, in hope of someone else posting a better answer. :)
Upvotes: 0