Use one field to compare to another field and filter in oData - filter

Lets say I have data like this (lots of it)
{
"name" : "Coffee",
"quantity": 100,
"restock": 10
}
I want to use an odata $filter to show me ONLY items where the quantity is LESS than the restock number
Is it possible to do something like $filter=quantity lt restock
I know that specific example fails. Is there a way to do this? Or do I need to fetch everything and post process it?

That query should absolutely be possible in most (all?) versions of OData: see http://services.odata.org/V4/OData/OData.svc/Products?$filter=Rating lt Price for a working example.

Related

Oracle SODA - How to sort on created using REST?

I can't work out how to use the $orderby with SODA on an id field (such as created or lastModified. I'm using SODA for REST directly and not the other projects.
Sort syntax is:
{
$orderby: {
path: 'created',
datatype: 'date',
order: 'desc'
}
}
And I've also tried:
{
"$orderby": {
"$fields": [{
"path": "created",
"datatype": "date",
"order": "desc"
}],
"$scalarRequired": true
}
}
And replacing the path with $id: 'created' (as you can use that in a filter specification to access non-document metadata. But nothing works to order properly.
Short of putting the created field into my object when I create them (which defeats the purpose of having those fields) how can I use orderby on a metadata field?
Max here from the SODA dev team. I am not 100% sure what you mean by an "id field". Looks like you mean the "created on" and "last modified" document components automatically maintained by SODA, right? If so, we don't support orderbys on these (though it could be added as an enhancement).
As of now, as you mentioned in your post, best option is to create a field in your JSON documents' content and set it to ISO8601 format timestamp value (e.g. 2020-10-13T07:01:01). You can then do an orderby on such a field (with datatype "datetime"). Please let me know if more details on this are needed.
In SODA REST, when you're listing collection contents, you could specify since=timestamp and until=timestamp query parameters. That'll give you all documents with last modified timestamp greater than the "since" one, and less than or equal to the "until" one.
Example:
http://host:port/ords/scott/soda/latest/myColl?since=2020:01:01T00:00:00&until=2021:01:01T00:00:00
As part of this operation, SODA automatically adds an orderby on "last modified". Not sure if that's useful to you though, since that's just for listing all documents in the collection (i.e. you can't combine it with a QBE, for example). So if this doesn't meet your needs, best option right now is to explicitly add something like a "modified' field to the document content, and do an orderby on that.

Search by filters using views in CouchDB

I have a CouchDB database where I store models like this:
"_id": "id",
"_rev": "rev",
"field_1": "test",
"filed_2": 45,
"filed_3": 15,
"object_1": {
"field_1_1": 123,
"filed_1_2": 125
}
}
And I want to search for models by specific parameters in different ranges (filters).
For example, in one situation I need to find all the models with
field_2 from 10 to 50
field_3 from 10 to 20
object_1.field_1_1 from 100 to 150, object_1.field_1_2 from 120 to 130
In another case I need to find just all the models with field_2 from 10 to 50.
At the moment I wrote view like this:
function (doc) {
emit([doc.filed_2, doc.field_3, doc.object_1.field_1_1, doc.object_1.filed_1_2], 1);
}
So it generates that result:
{"id":"id","key":[45,15,123, 125],"value":1}
I can use this array-key to fetch necessary models and I can use "startkey" and "endkey" to generate ranges.
But Is there more efficient way to create search by different filters (some filters can be skipped, user selects the filters he wants to search by) in CouchDB? How Can I combine different parameters?
And How Can I skip parameters if they were not chosen for search (like in the second case)?
Thank you.
In CouchDB 2.x you can use the /db/_find endpoint with Mango expressions in order to query the database.
Please, check the expression syntax in order to check if it can cover your needs.

mapbox comparsion filter not working on numbers

i have tried to find anything about my problem online but unfortunately i had no luck finding anything that would help me.
i have a custom javascript map built with mapbox-gl-js.
the map shows real estate objects which can be filtered by country, city etc. using a custom built filter bar. now on this filter bar there are dropdown fields and two input fields. the filters i built for the dropdown fields work just fine. in the code use equal-to-comparsions for this task.
the two input fields are there to filter the maximum price and a minimum square meters - here i use the less or equal than and greater or equal than comparsion filters.
for some reason these two filter's won't work as desired. for example if i filter for objects with max price 1000000 objects with greater price still show.
this is how my filter would look like in JSON:
["all",["<=","preis",1000000]]
this is how the features look like:
feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [...]
},
"properties": {
[...],
"preis": 20000000,
[...]
}
}
i have also tried to reproduce this in a simple test map with simple objects - there the problem also exists.
does anyone have a clue why this is acting up on me or has anyone had or got an familiar issue?
thanks and br, John

Nested count queries

i'm looking to add a feature to an existing query. Basically, I run a query that returns say 1000 documents. Those documents all have the same structure, only the values of certain fields vary. What i'd like, is to not only get the full list as a result, but also count how many results have a field X with the value Y, how many results have the same field X with the value Z etc...
Basically get all the results + 4 or 5 "counts" that would act like the SQL "group by", in a way.
The point of this is to allow full text search over all the clients in our database (without filtering), while showing how many of those are active clients, past clients, active prospects etc...
Any way to do this without running additional / separate queries ?
EDIT WITH ANSWER :
Aggregations is the way to go. Here's how I did it, it's so straightforward that I expected much harder work !
{
"query": {
"term": {
"_type":"client"
}
},
"aggregations" : {
"agg1" : {
"terms" : {
"field" : "listType.typeRef.keyword"
}
}
}
}
Note that it's even in a list of terms and not a single field, that's just how easy it was !
I believe what you are looking for is the aggregation query.
The documentation should be clear enough, but if you struggle please give us your ES query and we will help you from there.

Getting all nested data object using facets with Elastic

Greatings,
I have done some searching around but I have not come across a solid answer. We are using elastic search and trying to use facets to group together nested objects. Here is what the data looks like:
{
id:1,
name:abc,
object:{
tag: 5,
name:'test1',
set: true
id
}
},
{
id:2,
name:def,
object:{
tag: 2,
name:'test2',
set: false
}
}
and I want to use facets to get a count of object nested. I can get one field from the object using something like this:
{"facets":{"tags":{"terms":{"field":"object.name"}}}}'
but that just gets me the name and a count from the parent object its in. I want all the properties within object. I want tag,name and set to come back within the facet.
Is this possible? All signs seem to point to no, or to use something like script, where i can create a composite field of all 3, but that would require post processing, which i was hoping to avoid doing.
Any help would surely be appreciated. Thank you in advance.

Resources