I'm using couchDB in the fabric for state database in peers. I have a json data that is stored inside the ledger
JSON DATA
"Company": {
"Associate": {
"entity": {
"name": "foo",
"role": "admin"
},
"access": [
{
"create": "allowed"
}
]
}
}
}
I have to query the data based on access json object, like these have values like "create","readonly","delete" etc..
Currently I tried this but none of the records came up.
{
"selector": {
"Company": {
"Associate": {
"access": {
"$elemMatch": {
"$eq": "create"
}
}
}
}
}
}
How can I query the data's ?
I think you want (I use dot notation for simplicity):
{
"selector": {
"Company.Associate.access": {
"$elemMatch": {
"create": {
"$exists": true
}
}
}
}
}
...or maybe...
{
"selector": {
"Company.Associate.access": {
"$elemMatch": {
"create": "allowed"
}
}
}
}
Related
This might be a stupid question. I am building a server with Node JS that exposes an API to clients. I was wondering what is the recommended way to handle "ref" in the query results.
For example, I have a query like this -
q.Map(
q.Paginate(
q.Match(q.Index("network_by_creator"), q.Select("ref", q.Call(q.Function("getUserByUsername"), username))),
options
),
q.Lambda("x", q.Get(q.Var("x")))
)
This returns all the networks created by the user. An example result looks like this -
{
"before": [
{
"#ref": {
"id": "279699094284272133",
"collection": {
"#ref": {
"id": "networks",
"collection": {
"#ref": {
"id": "collections"
}
}
}
}
}
}
],
"data": [
{
"ref": {
"#ref": {
"id": "279699094284272133",
"collection": {
"#ref": {
"id": "networks",
"collection": {
"#ref": {
"id": "collections"
}
}
}
}
}
},
"ts": 1603000692605000,
"data": {
"creator": {
"#ref": {
"id": "279656553326313989",
"collection": {
"#ref": {
"id": "users",
"collection": {
"#ref": {
"id": "collections"
}
}
}
}
}
},
"name": "Hello2"
}
}
]
}
I was wondering should I remove the "ref" fields from the objects before sending them to the client (the "name" field can be used to search a network), or should I extract the "id" from the "ref" and send it along? In any case, can that be done through FQL, instead of querying the result and manually modifying?
A Reference is a compound value, comprised of the document's collection reference and a document ID.
You can certainly extract the document ID to send to the client, provided that when the client sends it to your API, you know which collection to use. If your API transits data for multiple collections, you should also return the collection reference name too.
Here is a query that demonstrates how to pull out the components of a Reference:
> Let(
{
ref: Ref(Collection("networks"), "279699094284272133")
},
{
collection: Select("collection", Var("ref")),
collection_Name: Select(["collection", "id"], Var("ref")),
documentID: Select("id", Var("ref"))
}
)
{
collection: Collection("networks"),
collection_Name: 'networks',
documentID: '279699094284272133'
}
Just use the appropriate Select expression to extract what you need.
I have documents like this.
{
"a":"test",
"b":"harry"
},
{
"a":""
"b":"jack"
}
I need to update docs with field a==""(empty string) to default value say null in all documents for a given index.
Any help is appreciated. Thanks
Use Update by query with ingest
_update_by_query can also use the Ingest Node feature by specifying a pipeline like this:
define the pipeline
PUT _ingest/pipeline/set-foo
{
"description" : "sets foo",
"processors" : [ {
"set" : {
"field": "a",
"value": null
}
} ]
}
then you can use it like:
POST myindex/_update_by_query?pipeline=set-foo
{
"query": {
"filtered": {
"filter": {
"script": {
"script": "_source._content.length() == 0"
}
}
}
}
}'
OR
POST myindex/_update_by_query?pipeline=set-foo
{
"query": {
"bool" : {
"must" : {
"script" : {
"script" : {
"inline": "doc['a'].empty",
"lang": "painless"
}
}
}
}
}
}
To query a documents with empty string field value, i.e = ''
I did,
"query": {
"bool": {
"must": [
{
"exists": {
"field": "a"
}
}
],
"must_not": [
{
"wildcard": {
"a": "*"
}
}
]
}
}
So overall query to update all docs with field a=="" is,
POST test11/_update_by_query
{
"script": {
"inline": "ctx._source.a=null",
"lang": "painless"
},
"query": {
"bool": {
"must": [
{
"exists": {
"field": "a"
}
}
],
"must_not": [
{
"wildcard": {
"a": "*"
}
}
]
}
}
}
I want build query for search matching with nested and not equal.
This is my elasticSearch query:
{
"from":0,"size":1000,
"query":{
"nested" : {
"path" : "data",
"query" : {
"match" : {
"data.city" : "california"
}
}
},
"filter":{
"not":{
"filter":{
"term":{
"_id":"01921asda01201"
}
}
}
}
}
}
But I got error, am I write something wrong ? thanks
You can use bool Filter too with must and must_not clause.
{
"from": 0,
"size": 1000,
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "data",
"query": {
"match": {
"data.city": "california"
}
}
}
}
],
"must_not": [
{
"term": {
"_id": "01921asda01201"
}
}
]
}
}
}
You need to use filtered query
GET _search
{
"query": {
"filtered": {
"query": {
"nested": {
"path" : "data",
"query" : {
"match" : {
"data.city" : "california"
}
}
}
},
"filter": {
"bool": {
"must_not": [
{
"term": {
"_id": "01921asda01201"
}
}
]
}
}
}
}
}
You should use a bool query for this, and put your two clauses in the must and must_not sections respectively.
If you don't care about scoring on the data.city field (from your example it's not clear), you might want to use the filter portion instead of the must portion.
{
"from": 0,
"size": 1000,
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "data",
"query": {
"match": {
"data.city": "california"
}
}
}
}
],
"must_not": [
{
"term": {
"_id": "01921asda01201"
}
}
]
}
}
}
I have a certain use case that I'm researching involving creating a query that returns events whenever a failed login occurs from two separate accounts on one machine.
I've created the following query, but I'm receiving errors whenever I try to run it.
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter" : {
"term": {
"EventID": "4625"
}
},
"filter" : {
"range" : {
"_timestamp" : {
"gt": "now-15m"
}
}
}
}
},
aggs: {
group_by_host: {
terms: {
field: 'hostname'
},
aggs: {
group_by_user: {
terms: {
field: 'username'
}
}
}
}
}
Any
You need to wrap your filters inside bool. You can refer to docs for more info.
Try this
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"term": {
"EventID": "4625"
}
},
{
"range": {
"_timestamp": {
"gt": "now-15m"
}
}
}
]
}
}
}
},
"aggs": {
"group_by_host": {
"terms": {
"field": "hostname"
},
"aggs": {
"group_by_user": {
"terms": {
"field": "username"
}
}
}
}
}
}
I have elasticsearch running and do all my requests with nodejs.
I have the following mapping applied for my index "mastert4":
{
"mappings": {
"mastert4": {
"properties": {
"s": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
I added exactly one document to the index which looks pretty much like this:
{
"master": {
"vi": "ff155d9696818dde0627e14c79ba5d344c3ef01d",
"s": "Anne Will"
}
}
Now doing any of the following search queries will not return any hits:
{
"index": "mastert4",
"body": {
"query": {
"filtered": {
"query": {
"match"/"term": {
"s": "anne will"/"Anne Will"
}
}
}
}
}
}
But the following query will return the exact document:
{
"index": "mastert4",
"body": {
"query": {
"filtered": {
"query": {
"constant_score": {
"filter": [
{
"missing": {
"field": "s"
}
}
]
}
}
}
}
}
}
And if I search for
{
"exists": {
"field": "s"
}
}
I will get no hits again.
When analyzing the field itsself I get:
{
"tokens": [
{
"token": "Anne Will",
"start_offset": 0,
"end_offset": 9,
"type": "word",
"position": 1
}
]
}
I'm really in a dead end here. Can someone tell me where I did wrong? Thx!!!!
You've enclosed the fields s and vi inside an outer field called master which is not declared in your mapping. That's the reason. If you query for master.s, you'll get results.
The second solution is to remove the enclosing master object in your document and that will work also:
{
"vi": "ff155d9696818dde0627e14c79ba5d344c3ef01d",
"s": "Anne Will"
}