Search code examples
marklogicmarklogic-8

MarkLogic 8 JavaScript Arrays


I'm trying to work with arrays. This is some working code that explains what I am doing.

// query

var a = ["1000", "2000", "3000"];
var b = ["2000"];    

for (i in b) {
  var index = a.indexOf(b[i]);
};

if (index > -1) {
    a.splice(index, 1);
};

a

Now when I use this same logic for my query results, it does not work anymore.

// query

queryDo = cts.andQuery([
      cts.jsonPropertyValueQuery("displayable", "true"),
      cts.jsonPropertyValueQuery("section", "dikw Track Events"),
      cts.jsonPropertyValueQuery("name", 'dikw_default'),
      cts.collectionQuery(["reference/application"])
    ]);

queryDont = cts.andQuery([
      cts.jsonPropertyValueQuery("displayable", "false"),
      cts.jsonPropertyValueQuery("section", "dikw Track Events"),
      cts.jsonPropertyValueQuery("name", 'Helpdesk'),
      cts.collectionQuery(["reference/application"])
    ]);

var qDo = cts.jsonPropertyWords("code", null, "document", queryDo).toArray();
var qDont = cts.jsonPropertyWords("code", null, "document", queryDont).toArray();

for (i in qDont) {
  var index = qDo.indexOf(qDont[i]);
};

if (index > -1) {
    qDo.splice(index, 1);
};

qDo

I've verified that the result of both queries is an array. The arrays consist of values just like the example code; 1000, 2000, etc. Also, when I use a notAndQuery to exclude results from the 2nd query from the first, this has no effect.

When I look at qDo[2] for example, the right value is returned.

My andNotQuery:

queryDo = cts.andQuery([
      cts.jsonPropertyValueQuery("displayable", "true"),
      cts.jsonPropertyValueQuery("section", "dikw Track Events"),
      cts.jsonPropertyValueQuery("name", 'dikw_default'),
      cts.collectionQuery(["reference/application"])
    ]);

queryDont = cts.andQuery([
      cts.jsonPropertyValueQuery("displayable", "false"),
      cts.jsonPropertyValueQuery("section", "dikw Track Events"),
      cts.jsonPropertyValueQuery("name", 'Helpdesk'),
      cts.collectionQuery(["reference/application"])
    ]);

andnot = cts.andNotQuery(queryDo, queryDont);

result = cts.jsonPropertyWords("code", null, "document", andnot);

Solution

  • After filing a bug at MarkLogic I got this repsonse from the engineering team:

    "The return from the lexicon call is a StringWithFrequency (not just a String) so the JS layer wraps it"

    You can create an Array from the call to cts.jsonPropertyWords as you did before, but you'll need to iterate through and get the string values for each item in turn first. Something like this:

    1. Create the Array from cts.jsonPropertyWords as before
    2. Create a new Array
    3. Iterate through the original Array and push each items's string value into the new Array. The call to indexOf will work on the new Array.

    Work around

    var trackCodes = cts.jsonPropertyWords("code", null, "document", cts.andQuery([])).toArray();
    var tc2 = new Array();
    
    for (var i = 0; i < trackCodes.length; i++){
      tc2.push(trackCodes[i].toString())
    }
    
    tc2.indexOf(["your value"])