Vespa.ai: How to make an array of floats handle null values? - java

I am trying to make a simple Vespa application, where one of my data fields are an Array. However the array contains some null values. For instance an array like: [2.0,1.4,null,5.6,...].
What can I use instead of float to represent elements in the array?

Seems like you want to use a sparse tensor field instead since some addresses does not have a value. x{} denotes a sparse tensor, x[128] is an example of a dense tensor. See https://docs.vespa.ai/documentation/tensor-user-guide.html for an intro to vespa tensor fields.
field stuff type tensor<float>(x{}) {
indexing: summary |attribute
}
[
{ "put": "id:example:example::0", "fields": {
"stuff" : {
"cells": [
{ "address" : { "x" : "0" }, "value": 2.0 },
{ "address" : { "x" : "1" }, "value": 1.4 },
{ "address" : { "x" : "3" }, "value": 5.6 },
]
}
}
}
]

Related

Issue when trying to query a MongoDB data

I have a document in a collection that has the following attributes:
nodeid : long
type: string
bagid: long
So, nodes can be on a bag, and be of different types.
I need to find,
all nodes of type A, or nodes of type B in a given list of nodes, or, nodes of type C in a given bag.
How can I design that query in MongoDB? I had all IN clauses but it is the works way to go performance wise. Could you please point me into the right direction? I could not find an aggregation or reduce that would help me make this simpler.
I tried also doing a text search, using the three elements, but the or in the text search, for instance "type: A \"type: B node:X\" \"type: B node: Y\" and so on, does not work.
Thanks
Edit, adding samples:
{ "_id" : BinData(3,"NJUuYHEBAAAdCda3V+kXvg=="),
"type" : "question", "bagid" : NumberLong(1067),
"topics" : [ NumberLong(33), NumberLong(67), NumberLong(203), NumberLong(217) ],
"nodeid" : NumberLong(15855),
"creationDate" : ISODate("2020-04-09T18:23:17.812Z"),
"_class" : "com.test.NodeEvent" }
{ "_id" : BinData(3,"NJUuYHEBAAAdCda3V+kXvg=="),
"type" : "comment", "bagid" : NumberLong(1067),
"topics" : [ NumberLong(33), NumberLong(67), NumberLong(203), NumberLong(217) ],
"nodeid" : NumberLong(15857),
"creationDate" : ISODate("2020-04-09T18:23:17.812Z"),
"_class" : "com.test.NodeEvent" }
{ "_id" : BinData(3,"NJUuYHEBAAAdCda3V+kXvg=="),
"type" : "question", "bagid" : NumberLong(1069),
"topics" : [ NumberLong(33), NumberLong(67) ],
"nodeid" : NumberLong(15859), "creationDate" : ISODate("2020-04-09T18:23:17.812Z"),
"_class" : "com.test.NodeEvent" }
You can build your queries using the MongoDB Query Language. The query is written using the db.collection.find method. It uses various query operators like, $or, $in, and $and.
I need to find, all nodes of type A, or nodes of type B in a given
list of nodes, or, nodes of type C in a given bag. How can I design
that query in mongo?
db.collection.find( { $or: [
{ type: "A" },
{ type: "B", node: { $in: [ "A", "C" ] },
{ type: "C", bag: 130 }
]
} );
In the above query the condition { type: "C", bag: 130 } is equivalent to { $and: [ { type: "C" }, { bag: 130 } ] } }. This is also the case with the condition { type: "B", node: { $in: [ "A", "C" ] }. But, using the $and is optional, in this case (see the documentation for details).
The query would be something like, given bag 1067 give me all nodes
with topics: 33 or 203
db.collection.find( { bag: 1067, topics: { $in: [ 33, 203 ] } } )
The output depends upon the data in the collection. The find method returns a cursor, and you can apply various cursor methods on the retrned documents (for example, you can sort them by a field).

Data extraction from the NVD data feed and cpe_match meaning

My IT product has CPE defined, for example:
cpe:/o:microsoft:windows_vista:6.0:sp1:~-~home_premium~-~x64~-
I am using NVD Data Feed to get all publicly known vulnerabilities.
CVEs are given in .json file and under each CVE item there is a configurations node.
If I want to check if my CPE exists in the current CVE item I guess I have to check configurations node, but I am not sure what is the purpose of the "operator" : "OR", "vulnerable" : false.
Can I just compare my CPE with cpe23Uri or I have to somehow consider operators and vulnerable nodes as well?
"configurations" : {
"CVE_data_version" : "4.0",
"nodes" : [ {
"operator" : "AND",
"children" : [ {
"operator" : "OR",
"cpe_match" : [ {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:adobe:flash_player:*:*:*:*:*:*:*:*",
"versionStartIncluding" : "10.3",
"versionEndExcluding" : "10.3.183.19"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:adobe:flash_player:*:*:*:*:*:*:*:*",
"versionStartIncluding" : "11.2",
"versionEndIncluding" : "11.2.202.233"
} ]
}, {
"operator" : "OR",
"cpe_match" : [ {
"vulnerable" : false,
"cpe23Uri" : "cpe:2.3:o:apple:mac_os_x:-:*:*:*:*:*:*:*"
}, {
"vulnerable" : false,
"cpe23Uri" : "cpe:2.3:o:linux:linux_kernel:-:*:*:*:*:*:*:*"
}, {
"vulnerable" : false,
"cpe23Uri" : "cpe:2.3:o:microsoft:windows:-:*:*:*:*:*:*:*"
} ]
} ]
}]
It depends what information you're trying to determine. Notice that the "OR" operator in the lower half of the node only applies between those three items, which together are "AND"ed with the top half. The skeletal structure of the node in question is:
"operator" : "AND",
"children" : [ {
"operator" : "OR",
"cpe_match" : [ {
...
} ]
}, {
"operator" : "OR",
"cpe_match" : [ {
...
} ]
} ]
(I have reindented because the "children" node is logically within "AND" even though they are structurally on the same level.)
In other words, two cpe23Uris need to be matched to meet the condition described by this node: any one from the top half AND any one from the bottom half. Your Windows example would appear to match only the latter, and it would not be matching the vulnerable component of your system. To determine whether your system is vulnerable you would need to look for a component that matches a vulnerable item as well.

Update the nested JSONObject using java

I am new to java programming, I am not getting the right example for my below nested json object scenario, Here is my Json object which I have, how can I update the "stackoverflow" values 1 with 100 and 2 with 200 etc.
also stackoveflow is incremental json object it can be {} or {...}, in below i given size of 2 elements . how can I update/replace this jsonobject faster and efficient way without effecting other objects. Thanks Much
{
"name": "sample",
"stackoverflow":
{
"one": {
"name": 1,
"type": "number",
"value":"onevalue"
},
"two": {
"name": 2,
"type": "number",
"value":"twovalue"
},
},
}

How to add different values to unique keys in Realm?

I'm unable to wrap my head around a problem I'm facing.
I have various json keys, let's say:
Name, grades, skills.
Now for each name, name, there can be multiple grades and skills, so the json could be like this:
[
{
"name": "name1"
"age": "age1"
"skills": "skills1"
},
{
"name": "name1"
"age": "age2"
"skills": "skills2"
},
{
"name": "name1"
"age": "age3"
"skills": "skills3"
}
]
Notice how name isn't changing, but skills and age are. In this case, how do I map all the different ages and skills to that one single name? I don't know the number of unique names in advance.
Let me know if I've missed out any details. Thank you.
Use JSON array like this:
[
{
"name" : "name1",
"age" : ["age1","age2","age3"],
"skills" : ["skills","skill2"]
},
{
"name" : "name2",
"age" : ["age1","age2","age3"],
"skills" : ["skills","skill2"]
}
]

Filter the returned elements in an array after a mongodb query

If I understood correctly this is called a projection in mongodb. I have seen that there are two types of useful projections operators: elemMatch and $.
I have a set of documents that each have a list of attributes(Object). I want to perform complex queries to match these attributes namely matching several of its fields(using regex, etc).
Right now when a document matches a query the Java driver is returning the whole document. I want to filter it to just return the attributes which matched the original query.
I have tried combining elemMatch but all I was able to accomplish was returning either all attributes or just one(normally the first match).
Mongo document structure as below :
{
"name": "MediaPlayer",
"attributes": [
{
"tag": "media",
"values": [
"mp3",
"mp4"
]
},
{
"tag": "teste",
"values": [
"teste"
]
}
]
}
and my query as :
{attributes : {$elemMatch: { 'tag' : 'media'},$elemMatch: { 'tag' : 'stream'}}}
and it return following results :
{
"name": "MediaStream",
"attributes": [
{
"tag": "media",
"values": [
"Edit"
]
},
{
"tag": "stream",
"values": [
"Edit"
]
},
{
"tag": "video",
"values": [
"Edit"
]
}
]
}
The example and returned are different documents. Although I matched two attributes I got the whole list returned.
I wanted the attributes array to contain only the matched elements.
How would I be able to do this?
EDIT:
From what I have seen the mongodb find operation does not support internal array filtering. There are some examples using the mongodb aggregation framework. Other than that doing the filtering manually seems like the only possible way.
Let's suppose you want to match tag:media and tag:stream, this case you entered your matching criteria in mongo $in aggregation query as below
db.collectionName.aggregate({
"$unwind": "$attributes" // first unwind attributes array
}, {
"$match": {
"attributes.tag": {
"$in": ["media", "stream"] // put all matching tags in array using $in
}
}
}, {
"$group": {
"_id": "$_id",
"name": {
"$first": "$name"
},
"attributes": {
"$push": "$attributes" // push all attributes to get only matching tags
}
}
}).pretty()
Found a possible solution : Solution A
EDIT: The aggregation found does not prove to offer a general solution to the problem since i am not controlling the queries. In this sense, if i want to unwind the attributes and then match it against the query it will only work on queries matching a single attribute.

Categories

Resources