I am trying to pass an array of integers to ElasticSearch template using the below mustache template.
{{#filter5_terms}}
"terms": {
"{{filter5_name}}": [
"{{#filter5_lt}}",
"{{.}}",
"{{/filter5_lt}}" ]
}
{{/filter5_terms}}
Above works, If I pass a string array (Ex: ["A","B"]. But the same is failing with the int array [1,2] with Nested: NumberFormatException[For input string: ""]; error.
Reference: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html#_passing_an_array_of_strings
Can you please let me know if I am missing anything?
Thanks
Anil
You really shouldn't rely on that, as the format is an inner implementation of Mustache and thus, subject to change. For example, if you try to emulate that using mustache.js, you'll get something like:
"terms: {
"property": 3,4
}
To workaround this problem, you should add square brackets to the templated values. So, your example becomes:
"terms": {
"{{filter5_name}}": [{{filter5_lt}}]
}
And that will get you what you want.
At least, this is true in mustache.js 2.2.1
I did fix this.
We can use the below to replace the integer array into ElasticSearch query.
"terms": {
"{{filter5_name}}": {{filter5_lt}}
}
ElasticSearch documentation has an example to replace string arrays and I tried to use the same for integer arrays and it did not work.
So I had to use the above which is provided in Mustache templating examples.
Thanks
Anil
Related
I have the output of a SalesForce SOQL snap that is a JSON in this format.
[
{
"QualifiedApiName": "Accelerator_Pack__c"
},
{
"QualifiedApiName": "Access_Certifications__c"
},
{
"QualifiedApiName": "Access_Requests__c"
},
{
"QualifiedApiName": "Account_Cleansed__c"
},
{
"QualifiedApiName": "Account_Contract_Status__c"
}
]
I am attempting to take those values and turn them into a string with the values separated by commas, like this, so that I can use that in the SELECT clause of another query.
Accelerator_Pack__c, Access_Certifications__c, Access_Requests__c, Account_Cleansed__c, Account_Contract_Status__c
From the documentation, my understanding was that .toString() would convert the array into a comma-separated string, but as shown in the attached image, it isn't doing anything. Does anyone have experience with this?
You need to aggregate the incoming documents.
Use the Aggregate snap with the function CONCAT. This will give you a | delimited concatenated string as the output like as follows.
Accelerator_Pack__c|Access_Certifications__c|Access_Requests__c|Account_Cleansed__c|Account_Contract_Status__c
You can then replace the | with , like $concatenated_fields.split('|').join(',') or $concatenated_fields.replace(/\|/g, ',').
Following is a detailed explanation of the configuration.
Sample Pipeline:
Sample Input:
I set the sample JSON you provided in a JSON Generator for testing.
Aggregation:
Result of Aggregation:
You get a | delimited concatenated string.
Mapper Expression:
Output:
Both expressions give the same result.
You can also use the array functions directly to achieve this. see the below pipeline that can be used to concat the values:
I have used the JSONGenerator for taking your sample data as input.
Then I have used the GroupByN snap with '0' as the group size to formulate the array.
Finally in the mapper you can use the below expression to concat:
jsonPath($, "$arrayAccom[*].QualifiedApiName").join(",")
I need to filter an array of strings in loopback. I have the following model.
{
...
"type": ["string"],
...
}
The filters I'm using are {"where":{"type":["filter string"]}} and {"type":["filter string"]}, but both are not working and return all the entries.
Please help. Thanks in advance.
Please use {"where":{"type":"filter string"}} without square brackets. Since type is defined as an array of strings, this query will return entries with filter string as an element at any index.
In the question JSON-LD framing single object arrays was asked on how to make arrays from single objects while framing JSON-LD for all arrays (by adding an compactArrays option).
Is there any way to do it selectively? Starting from always having "#graph", and after that for some "major" nodes. Maybe some parameter can be given in the frame to indicate exception of the general option?
In my case arrays are actually sets.
Yep, you can do it per-property by setting the #collection attribute of the property to #set:
{
"#context": {
"arrayProperty": {
"#id": "http://example.com/something",
"#container": "#set"
}
},
...
"arrayProperty": []
}
Hello everyone and thanks in advance for any ideas, suggestions or answers.
First, the environment: I am using CouchDB (currently developing on 1.0.2) and couchdb-lucene 0.7. Obviously, I am using couchdb-lucene ("c-l" hereafter) to provide full-text searching within couchdb.
Second, let me provide everyone with an example couchdb document:
{
"_id": "5580c781345e4c65b0e75a220232acf5",
"_rev": "2-bf2921c3173163a18dc1797d9a0c8364",
"$type": "resource",
"$versionids": [
"5580c781345e4c65b0e75a220232acf5-0",
"5580c781345e4c65b0e75a220232acf5-1"
],
"$usagerights": [
{
"group-administrators": 31
},
{
"group-users": 3
}
],
"$currentversionid": "5580c781345e4c65b0e75a220232acf5-1",
"$tags": [
"Tag1",
"Tag2"
],
"$created": "/Date(1314973405895-0500)/",
"$creator": "administrator",
"$modified": "/Date(1314973405895-0500)/",
"$modifier": "administrator",
"$checkedoutat": "/Date(1314975155766-0500)/",
"$checkedoutto": "administrator",
"$lastcommit": "/Date(1314973405895-0500)/",
"$lastcommitter": "administrator",
"$title": "Test resource"
}
Third, let me explain what I want to do. I am trying to figure out how to index the '$usagerights' property. I am using the word index very loosely because I really do not care about being able to search it, I simply want to 'store' it so that it is returned with the search results. Anyway, the property is an array of json objects. Now, these json objects that compose the array will always have a single json property.
Based on my understanding of couchdb-lucene, I need to reduce this array to a comma separated string. I would expect something like "group-administrators:31,group-users:3" to be a final output.
Thus, my question is essentially: How can I reduce the $usagerights json array above to a comma separated string of key:value pairs within the couchdb design document as used by couchdb-lucene?
A previous question I posted regarding indexing of tagging in a similar situation, provided for reference: How-to index arrays (tags) in CouchDB using couchdb-lucene
Finally, if you need any additional details, please just post a comment and I will provide it.
Maybe I am missing something, but the only difference I see from your previous question, is that you should iterate on the objects. Then the code should be:
function(doc) {
var result = new Document(), usage, right;
for(var i in doc.$usagerights) {
usage = doc.$usagerights[i];
for(right in usage) {
result.add(right + ":" + usage[right]);
}
}
return result;
}
There's no requirement to convert to a comma-separated list of values (I'd be intrigued to know where you picked up that idea).
If you simply want the $usagerights item returned with your results, do this;
ret.add(JSON.stringify(doc.$usagerights),
{"index":"no", "store":"yes", "field":"usagerights"});
Lucene stores strings, not JSON, so you'll need to JSON.parse the string on query.
i have a list of lat/long objects on my server.
eg.
public class LatitudeLongitude
{
public float Latitude;
public float Longitude;
}
simple.
now, can i return a collection of these, in json format .. BUT ... i do not want to list the key, just the values.
This means the normal result would be something like ...
{ { lat: 111, long : 222 }, { lat: 333, long : 444 } } ..
but i'm hoping for...
{ {111, 222}, {333, 444} ..... }
{ {obj1.Lat, obj1.Long}, {obj2.Lat, obj2.Long} ... etc. ... }
Is this possible? I mean, i can make that string on the server side easily. But is that a correct JSON output format?
Can someone please confirm, etc.
cheers :)
PS. I hardly know any Json, so please don't hesitate to correct my poor examples above.
Use JSON arrays:
[ [111, 222], [333, 444], ... ]
Ayman's proposal isn't quite correct JSON syntax. As an object {..} is always composed of fields having a name. So we again would have object with 2 fields of type 'array' that don't have a name -> not valid json for an object.
There is a very good description of how a json string can look like at www.json.org. Looking at this description you'll see an object is not allowed without a name String for a field so this {111,222} is not a valid object in json, but this is valid [111,222] array. This is not a valid object {[111,222]} but this is {array1 : [111,222]}.
I guess what you actually want is most likely an array of arrays like this not encapsulated as an object.
[[111,222],[333,444]].
This approach would give you the choice to add as much arrays of lat/long as you like to the enclosing array and at the same time you can process the inner arrays no matter what size the outer array is with an javascript loop.
Hope that helped.
cheers
Michael
If you need to access it using the keys in JSON, you'll need to specify it (or use a JS to convert arrays to objects on the client side). Otherwise, arrays should work:
var json = [ [123, 456], [234, 567] ];
var convertedJson = [];
for (i = 0; i < json.length; ++i) {
var thisObj = new Object();
thisObj.latitude = json[i][0];
thisObj.longitude = json[i][1];
convertedJson[i] = thisObj;
}
But is that a correct JSON output
format?
You can paste in JSON at jslint.com to validate your JSON
The ASP.NET MVC JSON serializer should convert a IList<LatitudeLongitude> to something like this:
[{"Latitude":111,"Longitude":222},{"Latitude":333,"Longitude":444} .. ]
I believe that under the ASP.NET MVC JSON serializer is just a hot-wired .NET core Javascript serializer.
I dont think it's possible from .NET using a serializer. Prob need to render the string manually.