Skip to main content
1-Visitor
November 20, 2015
Solved

Can't get ThingTemplate.QueryImplementingThingsWithData(...) to work with Query filter

  • November 20, 2015
  • 7 replies
  • 2734 views

Greetings,

I'm trying to query for implementing things on a template which have certain property values.

I'm doing this in Java.

Here is an example:

ThingTemplate template = // get thing template

String property1Value = "someValue";

String property2Value = "anotherValue";

JSONArray a = new JSONArray();

  a.push(newFilterJSONObject("EQ", "property1", property1Value));

  a.push(newFilterJSONObject("EQ", "property2", property2Value));

  JSONObject filters = new JSONObject();

  filters.put("type", "AND");

  filters.put("filters", a);

InfoTable results = template.QueryImplementingThingsWithData(500.00, null, null, filters);

This will return all implementing things as if it is ignoring the filter.

I've reviewed the Query docs and am using the composite query example:

var query = {
filters: {
type: "And",
filters: [{
type: "GT",
fieldName: "Duration",
value: "12"
},{
type: "TaggedWith",
fieldName: "tags",
tags: "MaintenanceIssues:PowerOutage"
} ]
}

};

This is the resulting JSON from the Java JSONObject above:

{

"filters":[

  1. {},
    • "fieldName":"property1",
    • "type":"EQ",
    • "value":"1"
  2. {}
    • "fieldName":"property2",
    • "type":"EQ",
    • "value":"1"

],

"type":"AND"

}

There are no matches in my data so i should be getting back an empty InfoTable, but i'm not.

What am i doing wrong?

    Best answer by qn_01

    You need another "filters" outside:

    {

         filters:

         {

              "filters":

              [

                   {"fieldName":"property1","type":"EQ","value":"1"},

                   {"fieldName":"property2","type":"EQ","value":"1"}

              ],

              "type":"AND"

         }

    };

    7 replies

    1-Visitor
    November 25, 2015

    The resulting JSON is not "normal", the 2 filters are outside of the {}. Or is it just a problem of copy-paste ?

    Have a look on this discussion How to populate query when testing the QueryStreamEntriesWithData service?​.

    a badly formatted query is ignored and you get all the results

    rtaylor-31-VisitorAuthor
    1-Visitor
    November 25, 2015

    Sorry....copy-n-paste error, below is the generated output:

    {

    "filters":[

         {"fieldName":"property1","type":"EQ","value":"1"},

         {"fieldName":"property2","type":"EQ","value":"1"}

      ],

    "type":"AND"

    }

    And it did review that discussion before posting.

    1-Visitor
    November 25, 2015

    Could you try "type":"And" instead of "type":"AND"​, just in case of syntax.

    qn_011-VisitorAnswer
    1-Visitor
    November 25, 2015

    You need another "filters" outside:

    {

         filters:

         {

              "filters":

              [

                   {"fieldName":"property1","type":"EQ","value":"1"},

                   {"fieldName":"property2","type":"EQ","value":"1"}

              ],

              "type":"AND"

         }

    };

    rtaylor-31-VisitorAuthor
    1-Visitor
    November 25, 2015

    Boom! That did it. I should have seen that....just glossed over it. Another set of eyes is always good. Thanks so much!

    1-Visitor
    November 25, 2015

    You're welcome!