Insertelements is not inserting the objects - drools

I am trying to insert objects to be validated by Drools using insertelements. Below is the JSON format used.
{
"commands": [ {
"insert-elements": {
"objects": [
{
"containedObject": {
"#class": "com.myspace.poc.check",
"sFirstName": "Person 1",
"fAppliedAmount" : 0,
"fLegalAmount" : 40
}
},
{
"containedObject": {
"#class": "com.myspace.poc.check",
"sFirstName": "Person 2",
"fAppliedAmount" : 0,
"fLegalAmount" : 0
}
}
],
"disconnected": false,
"out-identifier": "checks",
"return-object": true,
"entry-point": "DEFAULT"
}
},
{
"fire-all-rules":{}
}
]
}
Even I am getting the response as successful, not getting the suitable response. In this JSON, one object is valid and one is invalid as per the rule. Below is the output I am getting.
{
"type" : "SUCCESS",
"msg" : "Container POC_1.0.0-SNAPSHOT successfully called.",
"result" : {
"execution-results" : {
"results" : [ ],
"facts" : [ ]
}
}
}
Kindly help and let me know what am I missing. Will appreciate your help.

Related

ElasticSearch filter on array along with multiple match queries

I am trying to filter the documents based on some tags and subcategory.
Some documents:
[{
"_id" : "2oukjh8o9qy2ejhasdkqwe",
"productName" : "ASSORTED DOUGHNUT 1PC",
"productSubCategory" : [
{
"label" : "Veg",
"imageUrl" : "/catImg/fandA.svg"
}
],
"isVisible" : true,
"tags" : [
{
"tagImageUrl" : " ",
"label" : "Favorites",
"tag" : "favorites"
}
]
},
{
"_id" : "638daf9f42e6efc7f06641c2",
"isVisible" : true,
"productName" : "FILTER COFFEE",
"productSubCategory" : [
{
"_id" : ObjectId("638daf18ed445826c06a7328"),
"label" : "Veg"
}
],
"tags" : [
{
"tagImageUrl" : " ",
"label" : "Trending",
"tag" : "trending"
},
{
"tagImageUrl" : " ",
"label" : "Favorites",
"tag" : "favorites"
},
{
"tagImageUrl" : " ",
"label" : "Cabin Friendly",
"tag" : "cabinfriendly"
}
]
},
{
"_id" : "6389d7f942e6efc7f05d51e0",
"isVisible" : true,
"productName" : "ILLY DOPPIO",
"productSubCategory" : [
{
"_id" : ObjectId("638d97236612ca5d5ceb53b9"),
"label" : "Non-Veg"
}
],
"tags" : [
{
"tagImageUrl" : " ",
"label" : "Cabin Friendly",
"tag" : "cabinfriendly"
}
]
}]
Query I am trying
{
"query": {
"bool": {
"must": [
{
"match": {
"productSubCategory.label": {
"query": "Veg",
"operator": "and"
}
}
},
{
"match": {
"isVisible": true
}
}
],
"filter": {
"terms": {
"tags.tag": [
"favorites",
"cabinfriendly"
]
}
}
}
}
}
Requirement
The result documents must have any of the tags.tag provided in the query. Must have isVisible as true and productSubCategory.label as provided in the query.
With the above query I am getting Non-Veg items as well.
Because you are using match query which will do the free text search and it will match with the non-veg as well. You can use term query with keyword type insted of match query like shown below:
{
"query": {
"bool": {
"must": [
{
"term": {
"productSubCategory.label.keyword": {
"value": "Veg"
}
}
},
{
"match": {
"isVisible": true
}
}
],
"filter": {
"terms": {
"tags.tag": [
"favorites",
"cabinfriendly"
]
}
}
}
}
}
Please note that i have replace field name productSubCategory.label with productSubCategory.label.keyword

Return field of column in MongoDB

I have this table, and I want to filter some fields of it. The table looks like this:
/* 1 */
{
"_id" : ObjectId("5ce7db93db1ec10d08941d44"),
"name" : "john",
"age" : "22",
"group" : "A",
"nodes" : [
{
"name1" : "some_name1",
"status" : "completed"
}
]
}
/* 2 */
{
"_id" : ObjectId("5ce7e2fd726ed9434c32aaba"),
"name" : "mike",
"age" : "23",
"group" : "B",
"nodes" : [
{
"dev_name" : "some_name_dev1",
"status" : "not completed"
},
{
"dev_name" : "some_name_dev2",
"status" : "completed"
}
]
}
/* 3 */
{
"_id" : ObjectId("5ce7e36c726ed9434c32aabc"),
"name" : "anne",
"age" : "24",
"group" : "C",
"status" : "pending"
}
/* 4 */
{
"_id" : ObjectId("5ce7f05e726ed9434c32aabe"),
"name" : "jane",
"age" : "27",
"group" : "D",
"nodes" : [
{
"dev_name" : "some_name_dev6",
"status" : "not completed"
},
{
"dev_name" : "some_name_dev7"
}
]
}
And what I actually want to return is this (a simple object with "status" if nodes does not exist, and an array if nodes exists:
/* 1 */
[{
"status" : "completed"
}]
/* 2 */
[{
"status" : "not completed"
},
{
"status" : "completed"
}]
/* 3 */
{
"status" : "pending"
}
/* 4 */
[{
"status" : "not completed"
}]
I did this:
db.getCollection('user').find(
{
$or: [
{
"status": { $ne:null }
},
{
"nodes.status":{ $exists: true }
}
]
},
{
'status': 1,
'nodes.status': 1,
'_id': 0
}
)
and the result is this, and I am a little bit stuck:
/* 1 */
{
"nodes" : [
{
"status" : "completed"
}
]
}
/* 2 */
{
"nodes" : [
{
"status" : "not completed"
},
{
"status" : "completed"
}
]
}
/* 3 */
{
"status" : "pending"
}
/* 4 */
{
"nodes" : [
{
"status" : "not completed"
},
{}
]
}
How can I obtain what I want (getting rid of nodes field)? Thank you for your time!
EDIT:
db.getCollection('user').find(
{
$or: [
{
"status": { $ne:null }
},
{
"nodes.status":{ $exists: true }
}
]
},
{
'status': 1,
'nodes.status': 1,
'_id': 0
},
[ { "$project": { "status": { "$ifNull" : ["$nodes.status", ""] } } } ]
)
You are going to the right direction using {$project: <expression>} to change the format of your return document. But I don't think the $ifNull has been used correctly. According to the doc, the input of the $ifNull is
{ $ifNull: [ <expression>, <replacement-expression-if-null> ] }
Full Query:
db.getCollection('user').find(
{
$or: [
{
"status": { $ne:null }
},
{
"nodes.status":{ $exists: true }
}
]
},
{
'status': { "$ifNull" : ["$status", "$nodes.status"] },
'_id': 0
})
Edit: Using aggregation instead of simple find(), as $ifNull operator is only available using aggregation.
db.collection.aggregate([
{
$match: { #behaves like the find operator
$or: [
{
"status": {
$ne: null
}
},
{
"nodes.status": {
$exists: true
}
}
]
}
},
{
$project: {
"status": {
"$ifNull": [
"$status",
"$nodes.status"
]
},
"_id": 0
}
}
])
output:
[
{
"status": [
"completed"
]
},
{
"status": [
"not completed",
"completed"
]
},
{
"status": "pending"
},
{
"status": [
"not completed"
]
}
]

Elasticsearch - Range query doesn't work

To try this error I have tried with Elasticsearch 2.x and 5.x but doesn't work in any of these.
I have lots of logs saved in my Elasticsearch instance. They have a field called timestamp whose format is "YYYY-MM-dd HH-mm-ss.SSS" (for example, "2017-11-02 00:00:00.000"). When I try to send a query via POSTMAN which is this:
{
"query": {
"range": {
"timestamp": {
"gte": "2017-10-21 00:00:00.000",
"lte": "2017-10-27 00:00:00.000"
}
}
}
}
I receive nothing and I there are more than 500 logs in that range. What am I doing wrong?
EDIT:
My index (loganalyzer):
{
"loganalyzer" : {
"aliases" : { },
"mappings" : {
"logs" : {
"properties" : {
"entireLog" : {
"type" : "string"
},
"formattedMessage" : {
"type" : "string"
},
"id" : {
"type" : "string"
},
"level" : {
"type" : "string"
},
"loggerName" : {
"type" : "string"
},
"testNo" : {
"type" : "string"
},
"threadName" : {
"type" : "string"
},
"timestamp" : {
"type" : "string"
}
}
}
},
"settings" : {
"index" : {
"refresh_interval" : "1s",
"number_of_shards" : "5",
"creation_date" : "1507415366223",
"store" : {
"type" : "fs"
},
"number_of_replicas" : "1",
"uuid" : "9w3QQQc0S0K0NcKtOERtTw",
"version" : {
"created" : "2040699"
}
}
},
"warmers" : { }
}
}
What I receive sending the request:
{
"took": 429,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
And status 200 (OK).
Your edit with the mappings indicates the problem. The reason you aren't getting any result is because it's attempting to find a "range" for the string you're providing against the values of the field in your index, which are also treated as a string.
"timestamp" : {
"type" : "string"
}
Here's the elastic documentation on that mapping type
You need to apply a date mapping to that field before indexing, or reindex to a new index that has that mapping applied prior to ingestion.
Here is what the mapping request could look like, conforming to your timestamp format:
PUT loganalyzer
{
"mappings": {
"logs": {
"properties": {
"timestamp": {
"type": "date",
"format": "YYYY-MM-dd HH-mm-ss.SSS"
}
}
}
}
}

Distinct list of values from embedded document in mongodb

I'm going crazy... I come from the sql world an this is my first real experience with mongodb.
I have a given json/object structure (I know, this is not perfect but it has to be so because of existing data and other applications), stored in a mongodb (v3.4) with Restheart as the http frontend.
The documents look like this
{
"_id" : ObjectId("5855cbc9fc3baea81e937261"),
"_etag" : ObjectId("5855cbc99b971700050d8adc"),
"log" : [
"858489b087f7472dbb8d1012dee4cd5d.d",
NumberLong("12345678901234"),
{
"ext" : {
"text" : "someone did something at somewhere (some street 123) +38 USD",
"markup" : [
[
"user",
{
"plain" : "someone",
"team" : "master"
}
], [
"TEXT",
{
"plain" : " did something at "
}
], [
"location",
{
"name" : "somewhere",
"plain" : "some street 123",
"team" : "master"
}
], [
"TEXT",
{
"plain" : "38"
}
], [
"TEXT",
{
"plain" : "USD"
}
]
],
"category" : 1,
"team" : "master"
}
}
]
}
And I want to get a distinct list of the usern.plain names. Theoretically db.logs.distinct("log.2.ext.markup.0.1.plain") would do exactly what I need. But as far as I understand, there is no way to use db.distinct with Restheart. I tried this using views but it seems, I can not use db.distinct in views too.
This are my experiments...
{ "aggrs" : [
{ "stages" : [
{ "_$project" : { "user" : "$log.2.ext.markup.0.1.plain"}},
{ "_$unwind" : "$user"},
{ "_$unwind" : "$user"},
{ "_$unwind" : "$user"},
{ "_$unwind" : "$user"},
{ "_$unwind" : "$user"},
{ "_$unwind" : "$user"},
{ "_$group" : { "_id" : "$user"}}
],
"type" : "pipeline",
"uri" : "unique_users1"
},
{ "stages" : [
{ "_$match": { "log": { "_$exists": true, "_$ne": null }}},
{ "_$unwind" : "$log"},
{ "_$unwind" : "$log.2"},
{ "_$unwind" : "$log.2.ext"},
{ "_$unwind" : "$log.2.ext.markup"},
{ "_$unwind" : "$log.2.ext.markup.0"},
{ "_$unwind" : "$log.2.ext.markup.0.1"},
{ "_$group" : { "_id" : "$log.2.ext.markup.0.1.plain"}}
],
"type" : "pipeline",
"uri" : "unique_users2"
},
{ "stages" : [
{ "_$match" : {"log" : { "_$exists" : true }}},
{ "_$replaceRoot" : { "newRoot" : { "user": "$log.2.ext.markup.0.1.plain"}}}
],
"type" : "pipeline",
"uri" : "unique_users3"
},
{ "stages" : [
{ "_$group" : { "_id" : 1 , "users" : { "_$addToSet" : "$log.2.ext.markup.0.1.plain"}}}
],
"type" : "pipeline",
"uri" : "unique_users4"
}
]}
But results are... nothing or nearly nothing
{
"_embedded": {
"rh:result": [
{
"_id": 1,
"users": [
[]
]
}
]
},
"_returned": 1,
"_size": 1,
"_total_pages": 1
}

How to get sum of elements using mongo?

I have following json structure-
[{
cName:"A",
"vms" : [
{
"status":"off",
"name":"ds0",
"capacity":5
},
{
"name" : "ds1",
"status":"on",
"capacity":5
},
{
"name" : "ds2",
"status":"off",
"capacity":5
},
{
"name" : "ds3",
"status":"off",
"capacity":10
}
],
},
{
cName:"B",
"vms" : [
{
"name" : "ds4",
"status":"on",
"capacity":52
},
{
"name" : "ds3",
"status":"off",
"capacity":50
},
{
"name" : "ds5",
"status":"off",
"capacity":15
}
],
}
]
My expected output is following -
[{
"cName":"A",
"capacity":20,
},[{
"cName":"B",
"capacity":65,
}
]
I am using mongo aggregation to get output.
I am able to get cname and capacity using it but unable to get sum of capacity.
How do I get sum of capacity of vms using mongo aggregation??
If I understood you correctly
db.f.aggregate([
{
$unwind:"$vms"
},
{
$group:{
_id:"$cName",
capacity:{$sum:"$vms.capacity"}
}
},
{
$sort:{_id:1}
},{
$project:{
_id:0,
"cName":"$_id",
"capacity":1
}
}
])
Result:
{
"result" : [
{
"capacity" : 25,
"cName" : "A"
},
{
"capacity" : 117,
"cName" : "B"
}
],
"ok" : 1
}
I don't understand why in your example cName:A capacity = 20 (5+5+5+10) even cName:B...Maybe it's only the mock