Combining JSON fields and searching based on the combined value within MarkLogic

In the JSON format provided below, I have stored a person's first name and last name along with their nickname.

{
  "Person": {
    "Records": [
      {
        "nameType": "Primary",
        "firstName": "Sagar",
        "lastName": "Dravid"
      },
      {
        "nameType": "known as",
        "firstName": "Bunny",
        "lastName": "Bhau"
      }
    ]
  }
}

For a typeahead search API I am developing, I need to combine both the first name and last names. I am currently able to search on individual fields such as either the first name or last name. Below is the code snippet I've tried:

.where(cts.jsonPropertyValueQuery("firstName", [str + "* *"], ["wildcarded","punctuation-insensitive","diacritic-insensitive","unstemmed","case-insensitive"]))
  .map(mapper)
  .withOptions({
    search: ['filtered']
  })
  .result();

How can I concatenate the JSON fields to achieve a complete name search So that when a user types "Sagar Dravid" or "Bunny Bhau", they should get results, but not if they type "Sagar Bhau" or "Bunny Dravid". Any suggestions?

Answer №1

If I comprehend your needs accurately, you might consider utilizing a cts.nearQuery along with a cts.orQuery for each attribute (lastName and firstName) that includes a cts.jsonPropertyValueQuery for each of the name values (breaking them up by space):

const searchString = "Bunny Bhau"
const query = 
  cts.nearQuery(
    searchString.split(" ").map(term => 
      cts.orQuery(["firstName", "lastName"]
        .map(property => 
          cts.jsonPropertyValueQuery(property, term + "*" , 
            ["wildcarded", "punctuation-insensitive", "diacritic-insensitive", "unstemmed", "case-insensitive"])
        )
      )
    ), 
    1)

This will produce the following outcome:

cts.nearQuery([
  cts.orQuery([
    cts.jsonPropertyValueQuery("firstName", "Bunny*",
        [ "case-insensitive", "diacritic-insensitive", "punctuation-insensitive", "unstemmed", "wildcarded", "lang=en"], 1), 
    cts.jsonPropertyValueQuery("lastName", "Bunny*",
        [ "case-insensitive", "diacritic-insensitive", "punctuation-insensitive", "unstemmed", "wildcarded", "lang=en"], 1)
  ],[]), 
  cts.orQuery([
    cts.jsonPropertyValueQuery("firstName", "Dravid*",
        [ "case-insensitive", "diacritic-insensitive", "punctuation-insensitive", "unstemmed", "wildcarded", "lang=en"], 1), 
    cts.jsonPropertyValueQuery("lastName", "Dravid*",
        [ "case-insensitive", "diacritic-insensitive", "punctuation-insensitive", "unstemmed", "wildcarded", "lang=en"], 1)
  ],[])],
  1,[], 1)

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

Best Practices for Handling Pre-State Setting Mutations in Flux Design Pattern

Currently, I am developing a Vue application utilizing Vuex and following the Flux design pattern. I have encountered what seems to be an inefficient practice of duplicating code in small increments. I am hopeful that this is just a misunderstanding on my ...

Tips on handling json files and constructing an lstm model?

I'm currently working on implementing LSTM on the HP news dataset, which is stored in JSON format (). I attempted to run this code and encountered errors. I'm unsure what's causing the issue with this code? from keras.layers import LSTM, Act ...

Anticipating the completion of loading the JSON API without the use of Docker

I need help retrieving JSON data from a government website using R and the jsonlite package. The issue I'm facing is that it seems to only grab 1000 rows of data, even though there should be around 32,000. My suspicion is that the webpage isn't f ...

When using <body onload=foo()>, the function foo will not be executed

Having trouble with this code - it seems like initialize() is not being called. Check out the code here Any insight into why this might be happening? ...

Craft a Flawlessly Repeating Sound Experience - Online

I'm facing a challenge in creating a flawless loop of an audio file. However, all the methods I've tried so far have resulted in a noticeable gap between the end and the start. Here are the approaches I experimented with: The first approach inv ...

After the state loads in Ui-router, how can I execute a function?

Transitioning from jQuery to Angular, I am struggling with ui-router and states. In jQuery, I can make an AJAX call and then run another function on success. How can this be achieved in Angular? For instance, consider the code snippet below: var ivApp = ...

What is the best way to save inputted names as properties of an object and assign the corresponding input values as the values of those properties?

I am searching for a way to dynamically build an object where each property corresponds to the name of an input field and the value of that property is the input's value. Here is the HTML structure: <form> <fieldset> ...

Why do style assignments lose their motion when executed right after being made?

If you take a look at this specific fiddle in Webkit, you will see exactly what I am referring to. Is there a way to define the style of an element when it is first specified, and then its final state? I would like to be able to fully define a single-ste ...

Dealing with Grails command objects and encountering a 404 error due to spaces within JSON data sent

Recently, I came into possession of a controller. Interestingly, when a post request is sent with a precisely formatted JSON document, everything runs smoothly. However, the situation changes drastically when the JSON includes a space in one of its field ...

Executing the ES6 import syntax within a Node child process

After many attempts, I have come to the conclusion that I am ready to throw in the towel. My goal was to run a node es6 project that employs es6 import syntax; however, it seems that the child processes are not cooperating. The issue stems from the fact th ...

Guide on redirecting to a specific Vue-app page using Flask

I am currently working on an application that includes a page that ends with '@' and provides meta information for the page without '@'. For example, if the page '/user/aabb' contains information about the user 'aabb&apos ...

Create a list using ReactJS

I've been working on rendering a dynamic list in JSX, but I'm facing issues displaying the items. Below is my code snippet where I attempted to use useState const [orderList, setOrderList] = useState([]) and setOrderList(prev => [...prev, chil ...

Tips for customizing Material-UI Switch button appearance

Is there a way to remove the box-shadow on the thumb of the Switch button when it's disabled? I've tried various methods like adding the box-shadow in the switchBase and removing it from the thumb, but it affects the parent element as well. Anoth ...

How to handle duplicate keys in JSON parsing using Python3

Excuse my lack of experience in this area, but there's a json response that looks like this; import json jsonObj = json.loads(""" { "data": [ { "name_space": "name", "value": &q ...

Converting "require" to ES6 "import/export" syntax for Node modules

Looking to utilize the pokedex-promise for a pokemonapi, however, the documentation only provides examples on how to require it in vanilla JavaScript: npm install pokedex-promise-v2 --save var Pokedex = require('pokedex-promise-v2'); var P = new ...

The dynamically created array length is indicating as null

Although this question has been asked numerous times, most answers are in plain JavaScript. I have a function that generates an array with product data. This array is then utilized in another function to retrieve new data via JSON. $(function(){ var p ...

Steps to include a border around text and center align it:

As a beginner in Html and CSS, I am trying to create a heading with the text "Women safety" and wrap it with a border. However, when I apply the border to the text, it covers the entire width, extending both left and right. I only want the border to be a ...

Every time a row is selected, React and material-ui cause all TableRows to be re-rendered anew

Recently, I came across a code snippet that looks like this: <Table selectable onRowSelection={this.onRecordSelected} bodyStyle={tableBodyStyle}> <TableBody deselectOnClickaway={false} showRowHover displayRowCheckbox={false}> ...

Node.js is having trouble locating a module that has been installed

Upon transferring my node.js application to a different PC (where it functioned flawlessly on the development machine) and manually installing all the necessary dependencies, I encountered an error when attempting to run it: C:\Users\myself>n ...

What is the best way to encode only a specific section of a JavaScript object into JSON format?

Currently, I am in the process of developing a 2D gravity simulation game and I am faced with the challenge of implementing save/load functionality. The game involves storing all current planets in an array format. Each planet is depicted by a Body object ...