Query Results in Rule - jboss

I've made query in JBoss:
query "Cars"
c : Car()
end;
and I want to use in this rule:
rule "Ca"
salience -100
when
c : Cars()
then
end;
but anything what I'm doing occurs errors. Are there any possibilities to get query results in rule? I know about this method:
org.drools.runtime.rule.QueryResults results = ksession.getQueryResults( "Cars" );
but I need to get results in rule. Can anybody help?

Why do you want to do that? you can do an accumulate to get the list of all your cars something like this:
when
$cars: List() from accumulate(Car())
then
You don't need a query for that.

Related

Mongo text search does not seem to use the initial condition to reduce the number of documents to search

I have a collection with text search enabled and and index of the FName column. If I perform a search like this:
db.items.find({ "FName" : /^customer\\rainfall\\geometry\ types\\customer\ 1\ manhole\ monitors\\region\ 10\\area\ 100\\catchment\ 1000\\/ }).count()
Then this is very fast and returns a value of 300 as there are 300 matching documents.
If, however, I run this query:
db.items.find([{ "FName" : /^customer\\rainfall\\geometry\ types\\customer\ 1\ manhole\ monitors\\region\ 10\\area\ 100\\catchment\ 1000\\/ }, "$text" : { "$search" : "\"\\average rainfall\"" }]).count(), it can take up to 20 seconds to complete. If I run the initial query, get all the documents and carry out the search client side it is much faster - a second or two.
How can this be faster given that the second query is actually being run on the database? It almost looks like the FName filter is not being used first but I cannot prove this.
Any ideas?
Thanks
Ian

Mongodb sort on conditional clause

I want to do something like this:-
db.pub_pairs.find({status:'active'})
.sort( { if (pcat="abc") then 0 else 1 end })
.limit(10)
In other words, I want it to prefer records where the field pcat is "abc".
I also need it to be fast, but I can add indexes as necessary.
Any suggestions on how to do this?
Thanks
A decently efficient way of doing that would be to query the DB twice. Once for documents having pcat="abc" and once (if needed) for documents having pcat!="abc"
Something along the lines of (pseudo-code):
var data = db.pub_pairs.find({status:'active', pcat="abc"})
.limit(10)
if (data.length < 10)
data.concat(db.pub_pairs.find({status:'active', pcat!="abc"})
.limit(10-data.length))
For this to perform well, I would suggest a compound index on {status: 1, pcat: 1}
Please note, like any other "find" with MongoDB, you have to be prepared to retrieve the same document twice in the result set, notably here if the pcat field was concurrently modified by an other client during execution.

1 dimension indexes or multiple index

I have a query where the criteria is as follow (PHP):
...
->findBy(['userId'=>new \MongoId($me),'active'=>1],array('date' => 'DESC'),10);
I'm wondering whether it would be more efficient to use a triple index than using a simple index on userId for example.
My other question is : the field active can change; does it still make sense to create an index on this field ? if yes , how do indexes update their values ?
You can use the mongodb profiler, to find which of your queries are slow & then index the fields that have a 'where' clause, example
db.system.profile.find( { millis : { $gt : 5 } } ).pretty()
http://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/

How to remove datatypes for Integer in MongoDB

I am new to MongoDB.
I have inserted some rows in a collection which contains integers and strings.
Previously, they look like normal integer values.
Ex : age : 30 , work_status : 1
But now, They are looking like
Ex : age : NumberLong(30) , work_status : NumberLong(1) .
I dont know why they had changed like this.
I need the rows to get revert like old fashion . what should I do?
Please help me in solving the issue.
Thanks in advance!
You could basically loop over all the documents and update the values.
Try something like below
var cursor = db.<collection-name>.find()
while (cursor.hasNext()) { var doc = cursor.next(); db.<collection-name>.update({_id:doc._id}, {$set: {age: parseInt(doc.age.toString())}}) }
You can execute this directly in your mongo console.
PS. If you want to repeat the while block for testing then you might also have to execute the "var cursor = db..." command everytime.

Need help on this query

I want to have a output count as 2 for the user_mail logged as test1#gmail.com for a query like this,
SELECT Count(user_refemai) from Table_users where userref_mail = user_mail
but, I'm getting the output as 0! What am I doing wrong?
My table_users looks like:
user_id user_mail user_refemail
1 test1#gmail.com NULL
2 test2#gmail.com test1#gmail.com
3 test3#gmail.com test1#gmail.com
you are getting 0 as in your query the condition is false all the time
for this purpose you have to use the cursor or inner queries and then get the count
or pass the parameter to the query for which you want to get the counts.
Shafqat is correct. To build on that, you'd pass in the query parameter like this:
SELECT count(*)
FROM
table_users
WHERE
userref_email = ?
If you need a reporting query instead of for a particular email address, you could use a self join.