What are some effective techniques for leveraging ElasticSearch's term queries in order to refine results by searching for a specific value within an array

I'm trying to make a POST request to an API in order to receive only the hits with the role "editor". The initial response, without any filters, appears as follows:

_source: {
  configuration: {
    roles : [
        {role : "lead"},
        {role : "editor"},
      ]
  }
}

For the desired response, I attempted to use the following query:

body: {
  size: 10,
  query: {
    bool: {
      must: { 
        term: {
          role : "editor"
        }
      }
    }
  }
}

However, even though there are indeed thousands of hits with the role "editor", I am not receiving any hits.

I've also tried different variations of this request, such as:

body: {
  size: 10,
  query: {
    bool: {
      must: { 
        term: {
          "roles.role" : "editor"
        }
      }
    }
  }
}

or

body: {
  size: 10,
  query: {
    bool: {
      must: { 
        term: {
          "roles" : "role.editor"
        }
      }
    }
  }
}

Does anyone have any insights on what might be incorrect here? It seems like the issue lies in correctly accessing the role value from the Roles array of objects.

Answer №1

If your field called "roles" is in nested format, you should utilize the Nested Query.

{
  "query": {
    "nested": {
      "path": "roles",
      "query": {
        "term": {
          "roles.role": {
            "value": "editor"
          }
        }
      }
    }
  }
}

If it is not in a "nested" format, give this a try:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "roles.role": {
              "value": "lead"
            }
          }
        }
      ]
    }
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The function json.stringify fails to invoke the toJson method on every object nested within the main object

When using IE 11, I encountered an issue where calling Stringify on my objects did not recursively call toJson on all objects in the tree. I have implemented a custom toJson function. Object.prototype.toJSON = function() { if (!(this.constructor.name == ...

Transfer data from SqLite to MySQL using PHP without the need for JSON

I'm currently attempting to transfer data from an Android SQLite database to a MySQL database. I have found several references, but my main concern is whether it's possible to achieve this without using JSON. JSON seems quite complex, and I have ...

Ways to split combined JSON data using jQuery?

I recently implemented a jQuery script that fetches data from a JSON server and stores it in an "output" variable. Here is how the code looks: $(function() { $.getJSON('http://127.0.0.1:8000/getData/', displayData); function displayData(da ...

This error message occurs when trying to access JSON keys from an object with an invalid operand in the 'in' operation

Check out the fiddle I created here: http://jsfiddle.net/kc11/h6nh1gvw/2/ I'm attempting to extract keys from a JSON string using this code: var keys = $.map(a, function(element,key) { return key; }); . But unfortunately, I keep encountering the er ...

How to store an imported JSON file in a variable using TypeScript

I am facing a challenge with a JSON file that stores crucial data in the following format { "login": { "email": "Email", "firstName": "First name", "lastName": "Last name", ...

Obtaining data from a CSV file and transforming it into JSON format results in an array

Currently, I am working on a function that takes a JSON object and prints it out as strings: GetAllArticles: (req, res) => { var allArticles = getAllArticles(); res.setHeader("Content-Type", 'application/json'); res.w ...

Decoding nested generic classes with JSONDecoder in Swift

As a newcomer to Swift, I am exploring the creation of a class tree structure to represent my data. My goal is to utilize JSONEncoder and JSONDecoder for object serialization and deserialization. One challenge I encountered is when decoding generic classes ...

The JSON string fails to deserialize and returns a null value

{ "apple": { "A": "xyz", "B": "abc", "C": "jkl" }, "banana": { "A": "lotus", "B": "oil", "C": &qu ...

Mistaken deserialization of JSON input into an incorrect object class

In my code, I have defined two classes that represent JSON responses for error and success scenarios. private class SuccessResponse { public string message { get; set; } public bool sent { get; set; } public string id { get; set ...

Having trouble retrieving JSON value in JSP using Spring MVC, as it always returns 0

Seeking assistance with JSON string retrieval in JSP after adding it to the modelAndView. Upon debugging, I discovered that it is indeed being added to the modelAndView instance. Here's the snippet of code: Controller: modelAndView.addObject("json ...

Exploring ways to iterate through an array in JSONATA

Here is an example of an array: { "tocontrol": [{"name": "john"},{"name": "doe"}] } The desired output should look like this: { "method": "OR", "match": [ { &quo ...

Showing information retrieved from an API and rendering it on an HTML page

My aim is to showcase a list of calculated results fetched from a local server. In the console, this data appears as an array of objects, but on the webpage, it is being displayed as separate string characters for each li element. How can I display the con ...

How can I establish a connection between my Android app and a PHP server using HTTPUrlConnection to send data in the output stream and ensure that the server receives it in the $_

When using HttpURLConnection to send a JSON query to a PHP server, I encountered issues with the communication not working as expected. Despite having a successful connection and receiving proper error responses from the server side, it seems that the PHP ...

What is the best way to perform an AJAX request in Typescript with JSON data?

Currently, I am delving into the realm of AJAX and encountering some hurdles when attempting to execute an AJAX request with parameters. Specifically, I am facing difficulties in sending JSON data: My approach involves utilizing Typescript in tandem with ...

Discovering the value of an item when the editItem function is triggered in jsGrid

Within my jsGrid setup, I have invoked the editItem function in this manner: editItem: function(item) { var $row = this.rowByItem(item); if ($row.length) { console.log('$row: '+JSON ...

The specified message formats required for the operation include 'XML' and 'JSON'

Creating a WCF REST service includes defining methods for adding, deleting, and editing news entities. Here is an example of such a service: [ServiceContract] public interface INewsRepository { [OperationContract] [WebInvoke(Me ...

Having difficulty rendering JSON data on an HTML webpage

Currently, I am working on an API App that utilizes the Foursquare API. With the help of my getRequest function, I am able to obtain results in JSON format, which are then displayed in my console.log. However, the challenge lies in parsing the data from J ...

Update an imageview within a listview by implementing a click listener

I have a unique ListView layout that showcases an outstanding ImageView and two exceptional buttons; the mighty primary setter and the astonishing image rotator. My vision for this masterpiece is simple. When a user clicks on the Set Primary button, it wi ...

Tips for Deserializing Complex JSON Objects in C#:

I have the ability to deserialize JSON and insert data into a database table, but I am struggling with inserting the correct "ParentID & ParentFullPath" within the table. Is there a way to achieve this using recursion or without recursion? Here is the met ...

What is the best way to change a date from the format DD/MM/YYYY to YYYY-MM-DD

Is there a way to use regular expressions (regex) to convert a date string from DD/MM/YYYY format to YYYY-MM-DD format? ...