JSONSchema for "array of objects, they can have any keys, but they all have to have the same keys" - validation

Is there a way to specify in a JSON schema that you want an array of objects, you don't care what keys the objects have, as long as they all have the same ones.
Basically, I want a schema that constrains you to describe a valid 2D table of data.
So this would be valid:
[
{"foo": "a", "bar": "b"},
{"foo": "c", "bar": "d"}
]
But this would not:
[
{"foo": "a", "bar": "b"},
{"baz": "c", "bar": "d"}
]
... because the two objects in the array don't share the same keys.

No. You can't do this with the standard JSON Schema specification.

Related

How can we reverse the keys and values of hash with an efficient algorithm like this?

Think like you are implementing Like function of chat app.
You wanna store the information all people's name who liked a specific comment in db.
for memory reason, you don't wanna store the information which comments a specific person liked so far in db.
So what you wanna do is
# commend-id1: (user1, user2, user3)
# comment-id2: (user2]
# comment-id3: (user1)
# You wanna convert above into below by some codes
# user1: (comment-id1, comment-id3)
# user2: (comment-id2)
# user3: (comment-id1)
Is there any efficient way to achieve this?
EDIT::
someone commented the above example is impossible with efficient way.
how about this?
data = {
"field_a": {
"name": "index_a",
"used_fields": ["a", "b", "c"]
},
"field_b": {
"name": "index_b",
"used_fields": ["d", "b", "d"]
},
"field_c": {
"name": "index_c",
"used_fields": ["a"]
}
}
# you wanna convert above into below
# a. index_a, index_c
# b. index_a, index_b
# c. index_a

Is there some way to not fold/enclose identical lines with go-cmp?

I'm using go-cmp to compare and show the diff between two structures.
It works great, but I would like to show the entire struct instead of only the changed lines.
For example: https://play.golang.org/p/ynqUE2jPC-7
What I want
main.MyType{
Field1: "A",
Field2: "B",
Field3: "C",
Field4: "D",
Field5: "E",
- Field6: "F",
+ Field6: "G",
}
What I have
main.MyType{
... // 3 identical fields
Field4: "D",
Field5: "E",
- Field6: "F",
+ Field6: "G",
}
In the documentation I could not find such option, I also tried to check the code but it is too complex for my current Go knowledge.

Elasticsearch filter by whitelist

I have a list of string values which used as a whitelist.
For example:
{
"whitelist": ["a", "b", "c"]
}
In Elastic Search I have many documents, each one contains an array of strings, like:
doc_1
{
"values": ["a", "y", "b"]
}
doc_2
{
"values": ["a", "c"]
}
I want to filter out all the documents that contain at least one value that is not contained in the whitelist.
In the example above, the requested result is doc_2, as it doesn't contain any value which is not in the whitelist, while doc_1 does contain value "y".
Is there any way to do such a thing without an external code outside of ElasticSearch?

Elastic Search - querying documents where intersection of two arrays is nonempty

I have a document structure as follows:
{
"documentId": 123,
"someOtherInfo": {...}
"permissions": ["a", "b, ..., "g"]
}
Users themselves have a permission set ["x", "y", "z"]. Business Rule: User A is allowed to view document X if and only if at least one of the user permissions matches documents permissions. Or put mathematically, if intersection is nonempty -
["a", "b, ..., "g"] ∩ ["x", "y", "z"] ≠ ∅
I am building a search engine that needs to find all documents user has access to. I want to store it in Elastic Search for all the great querying capabilities it provides, but how do I add a restriction for permissions using ES DSL? Many thanks.
You need a terms query where an array whose element is to be matched can be passed. This match documents containing any of the provided terms. As an example , the following will match the document containing permissions = ["a", "b", "c"] but not permissions = ["a", "t", "c"]
{
"query": {
"terms": {
"permissions": [
"x",
"y",
"z",
"b"
]
}
}
}

Boost elastic [MoreLikeThis] search query for begining of array

I have elastic search documents with structure like this:
{
"name": "item1",
"storages": [
{"items": ["a", "b", "c", "d", "e", "f"]},
{"items": ["a 1", "b 2", "c 3", "d 4", "e 5", "f 6"]}]
}
{
"name": "item2",
"storages": [
{"items": ["d", "e", "f", "g", "h", "i", "j"]},
{"items": ["d 4", "e 5", "f 6", "g 7", "h 8", "i 9", "j 10"]}
]
}
and I want to search for sequence of strings, for example ["d 4","e 5"].
For this I use MoreLikeThis query:
{
"query": {
"more_like_this" : {
"fields" : ["storages.items"],
"like" : ["d 4","e 5"],
"min_term_freq": 1,
"min_doc_freq": 1
}
}
}
and it works almost fine, but it returns "_score": 0.1620518 for first document and "_score": 0.13890153 for second.
I want to boost score for terms from the begining of array ('items'), so because "d 4", "e 5" appears on the begining of array it should be ranked higher.
Is there way to create such query in elasticsearch? May be it should be not more like this query?
Tricky part is that query could be something like ["d 4","e 5", "xxx"] (xxx not present in document, but it's ok)
as you can see in this answer to a related question,
arrays are indexed—made searchable—as multivalue fields, which are
unordered
so you can't count on the order when you search.
Even worse, the array of objects is not stored as you think.
Arrays of objects do not work as you would expect: you cannot query each object independently of the other objects in the array. If you need to be able to do this then you should use the nested datatype instead of the object datatype.

Resources