简体   繁体   中英

Concatenate json fields and search on concatenated value in MarkLogic

I have below json format for storing person's firstname and lastname. I also store person's nickname as well.

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

I am developing typeahead search API for that I need to combine both first name and last name. I am able to search on single individual field like only on either first name or on last name. tried below code

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

How I can concatenate json fields to achieve complete name search so, if user types "Sagar Dravid" or "Bunny Bhau" I should get result, but I should not get result if user types "Sagar Bhau" or "Bunny Dravid". Any suggestions?

If I understand your requirement correctly, you could use a cts.andQuery with a cts.orQuery for each property (lastName and firstName) that has a cts.jsonPropertyValueQuery for each of the name values (splitting the on space):

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

Which would generate the following:

cts.andQuery([
  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", "Bhau*",[ "case-insensitive", "diacritic-insensitive", "punctuation-insensitive", "unstemmed", "wildcarded", "lang=en"], 1), 
    cts.jsonPropertyValueQuery("lastName", "Bhau*",[ "case-insensitive", "diacritic-insensitive", "punctuation-insensitive", "unstemmed", "wildcarded", "lang=en"], 1)
  ],[])],[])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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