Is it possible to query by array content? - fiware-orion

Using the FIWARE PointOfInterest data model I would like to filter by POI category which is an array. For instance
http://130.206.118.244:1027/v2/entities?type=PointOfInterest&options=keyValues&attrs=name,category&limit=100&q=category=="311"
having as entity instances something like
{
"id": "Museum-f85a8c66d617c23d33847f8110341a29",
"type": "PointOfInterest",
"name": "The Giant Squid Centre",
"category":
[
"311"
]
},
{
"id": "Museum-611f228f42c7fbfa4bd58bad94455055",
"type": "PointOfInterest",
"name": "Museo Extremeño e Iberoamericano de Arte Contemporáneo",
"category":
[
"311"
]
},

Looking to the NGSIv2 specification it seems that works in the way you mention:
Single element, e.g. temperature==40. For an entity to match, it must contain the target property (temperature) and the target property value must be the query value (40) (or include the value, in case the target property value is an array).
I mean, in particular the part that says:
...or include the value, in case the target property value is an array.

Related

Orion Context Broker How to filter an object by name contains a keyword

We are using Orion Context Broker as our database.
Recently, We have meet an requirement that user want to find a City by name of this City container a keyword.
For example:
We have city name likes this. Hanoi, Madrid, London, Barcelona, Paris, Lyon.
If user type: "on", we should show Lyon, London.
The city object like this.
{
"type": "City",
"isPattern": "false",
"id": "city1",
"attributes": [
{
"name": "name",
"type": "string",
"value": "London"
}
]
}
So I am wondering if any queryContext filtering can help us to sort out this case.
I have done some research and there is no good sounds on this.
Many thanks.
You can use idPattern in GET /v2/entities which value is a regular expression. Thus the following query:
GET /v2/entities?idPattern=on
should return any City with the "on" substring in its id.
EDIT: if you want to apply a pattern to the value of some attribute, then you have to use q query parameter and the ~= NGSIv2 Simple Query Operator. Something like this:
GET /v2/entities?q=colour~=or
I have figured out that we use ~= operation to do that.
Please see the follow quote.
Match pattern: ~=. The value matches a given pattern, expressed as a regular expression, e.g. color~=ow. For an entity to match, it must contain the target property (color) and the target property value must match the string in the right-hand side, 'ow' in this example (brown and yellow would match, black and white would not). This operation is only valid for target properties of type string.
http://telefonicaid.github.io/fiware-orion/api/v2/stable/
Section: Simple Query Language
Many thanks.

Orion context broker update some attributes

Lets say we have an entity with attributes A,B,C,D. Can we update only some of them, for example only B and D, or we have to update all of them, or just only one? And if it's possible to update some of them, how the target url must be constructed for the request to work?
You can update only a subset of attributes if you want. For instance:
PATCH /v2/entities/E1/attrs
{
"B": {
"value": "foo",
"type": "Text"
},
"D": {
"value": "bar",
"type": "Text"
}
}
which updates B and D but leave A and C untouched. You can use POST instead of PATCH: the former does update or create if the attribute doesn't exist in the entity, the later is for strict update (if the attribute to update doesn't exist, you will get an error response).
This and more detail can be found in the NGSIv2 specification document.

MongoDB - Document with different type of value

I'm very new to MongoDB, i tell you sorry for this question but i have a problem to understand how to create a document that can contain a value with different "type:
My document can contain data like this:
// Example ONE
{
"customer" : "aCustomer",
"type": "TYPE_ONE",
"value": "Value here"
}
// Example TWO
{
"customer": "aCustomer",
"type": "TYPE_TWO",
"value": {
"parameter1": "value for parameter one",
"parameter2": "value for parameter two"
}
}
// Example THREE
{
"customer": "aCustomer",
"type": "TYPE_THREE",
"value": {
"anotherParameter": "another value",
{
"someParameter": "value for some parameter",
...
}
}
}
Customer field will be even present, the type can be different (TYPE_ONE, TYPE_TWO and so on), based on the TYPE the value can be a string, an object, an array etc.
Looking this example, i should create three kind of collections (one for type) or the same collection (for example, a collection named "measurements") can contain differend kind of value on the field "value" ?
Trying some insert in my DB instance i dont get any error (i'm able to insert object, string and array on property value), but i would like to know if is the correct way...
I come from RDBMS, i'm a bit confused right now.. thanks a lot for your support.
You can find the answer here https://docs.mongodb.com/drivers/use-cases/product-catalog
MongoDB's dynamic schema means that each need not conform to the same schema.

How to search a collection for every instance of a single keyword

I'm trying to figure out how to search a collection for every instance of a keyword. For example, I'd like to search a collection for every instance of the word "authenticated". Below is what one of the documents looks like in the collection that contains the keyword:
{
"_id": {
"$oid": "55945938d28f9f3809002afc"
},
"TAGS": ".source.s_net",
"SOURCEIP": "10.10.0.5",
"SEQNUM": "11004",
"PROGRAM": "Core",
"PRIORITY": "info",
"MESSAGE": "<pppoe-jjcutter>: authenticated",
"LEGACY_MSGHDR": "Core ",
"HOST_FROM": "10.10.0.5",
"HOST": "10.10.0.5",
"FACILITY": "syslog",
"DATE": "Jul 1 14:18:48"
}
Check my github https://github.com/parthaindia/CustomMongo,
getByCondition(String tableName, Map condition) ,call this method where you tableName is your collection name ,Map is the key-value.In your case map would be
Map condition =new HashMap();
condition.put("MESSAGE",": authenticated");//or whatever object
You would receive an Array List in Json format.Use Type Token to convert into Array List.
You can use a wildcard text index.
Test data
db.people.insert({name: "Martin", remarks: "great architector"})
db.people.insert({name: "John", remarks: "awesome SO user, great programmer"})
db.people.insert({name: "Tony The Great", remarks: "inventor"})
Wildcard Index
db.people.createIndex({"$**" : "text"})
Query
db.people.find({$text: {$search: "great"}})
You will get all 3 records.
MongoDB documentation on wildcard indexes
Use $regex in find as below :
db.messages.find({"MESSAGE":{"$regex":"(\\s|^)authenticated(\\s|$)"}})
Or if you want to find out exact match of authenticated values then used below query :
db.messages.find({"MESSAGE":"authenticated"})

Is it possible to run a query using Orion where the searching criteria is given by attributes value?

As I create entities in an Orion server, I can search by ID, as flat or using regular expressions:
http://<localhost>:1026/v1/queryContext
Content:
{
"entities": [
{
"type": "Sensor",
"isPattern": "true",
"id": "sensor_1.*"
}
],
"attributes": ["temperature","humidity"]
}
In the above example I'd get all objects of type "Sensor" whose ID starts with "sensor_1", and their attributes "temperature" and "humidity". I wonder if there is any way that would allow me to search by specific attribute value, for example to get those sensors whose humidity is over "60.2", or this selection must be done over the retrieved data queried by ID.
Not in the current Orion version (0.19.0) but it will be possible in the future. Have a look to the attribute::<name> filter with the = operator in this document.