Esper access array event propertie - esper

I want to process the following JSON message using Esper:
{
"firstname":"John",
"lastname":"Do",
"address":[{"street":"Maplestreet","number":100,"city":"New York"}]
}
When the schema look like:
create schema Address (street string, number int, city string);
create schema Person (firstname string, lastname string, address Address[]);
What is the correct way to select the event properties within the array?
I have tried:
select address[0].street from Person
select address.street from Person
select address[0] from Person
select {address.street} as street from Person
but I get null values. Is the schema wrong or am I missing something?
EDIT: When I try:
select * from Person
I get the initial JSON as result
EDIT2:
This is my Jruby code:
address_type = {
"street" => "string",
"number" => "int",
"city" => "string"
}
epService.getEPAdministrator.getConfiguration.addEventType("Address", address_type)
person_type = {
"firstname" => "string",
"lastname" => "string",
"address" => "Address[]"
"
}
epService.getEPAdministrator.getConfiguration.addEventType("Person", person_type)

Most likely the event object is not populated correctly by your application. I would suggest creates a Java class first in the same structure to test the statements. Once that works switch to the event object you used originally i.e. XML, object-array or Map or ...

Related

Selecting columns from eager-loaded laravel models

I'm trying to select columns from related models, but I'm not quite sure how to do it using Eloquent (I want to avoid the DB namespace).
This is what I have so far...
$users = User::with("role", "country") -> select(["first_name", "last_name", "email_address"]);
How would I go about modifying the select list so that it also includes role.title and country.name?
So essentially, my select list would look something like this:
-> select(["first_name", "last_name", "email_address", "role.title", "country.name"]);
Many thanks!
Did you try
User::with(['role' => function ($query) {
$query->select('title');
}])->with(['country' => function ($query) {
$query->select('name');
}])->get();
or
User::with([
'role' => function ($query) {
$query->select('title');
},
'country' => function ($query) {
$query->select('name');
}
])->get();

mongoid criteria result doesn't fill all field

I am new to Rails and MongoDB as well as MongoID..
class User
include Mongoid::Document
include Mongoid::Timestamps
field :fbid, type: String
field :facebookname, type: String
field :competitorFbid, type: String
field :createdAt, type: DateTime
field :updatedAt, type: DateTime
# building constructor the rails way: http://stackoverflow.com/a/3214293/474330
def initialize(options = {})
#fbid = options[:fbid]
#facebookname = options[:facebookname]
#competitorFbid = options[:competitorFbid]
end
def writeasjson
hash = { :fbid => #fbid,
:facebookname => #facebookname,
:competitorFbid => #competitorFbid,
:createdAt => #createdAt,
:updatedAt => #updatedAt
}
hash.to_json
end
attr_accessor :fbid, :facebookname, :competitorFbid, :createdAt, :updatedAt
end
I am using MongoID to query my mongodb database like this:
myuser = User.where(fbid: params[:fbid]).first
render :json => myuser.writesajson
However, the result is all the fields are "null"
If I print the criteria result like this,
render :json => myuser
it prints all the _id, authData and bcryptPassword field, however the rest of the field have null value,
Here is what I got from the MongoDB database in my app. If I query from MongoHub, all the null values will be filled
{
"_id": {
"$oid": "56d2872f00af597fa584e367"
},
"authData": {
"facebook": {
"access_token": "yEf8cZCs9uTkrOq0ZCHJJtgPFxPAig9yhW6DhBCLuJqPdMZBLPu",
"expiration_date": "2016-04-17T13:52:12.000Z",
"id": "9192631770"
}
},
"bcryptPassword": "$2a$10$9mUW3JWI51GxM1VilA",
"competitorFbid": null,
"createdAt": null,
"created_at": null,
"facebookname": null,
"fbid": null,
"objectId": "nLurZcAfBe",
"runCount": 2446,
"sessionToken": "0SwPDVDu",
"updatedAt": null,
"updated_at": null,
"username": "XgcWo4iUCK"
}
I have been debugging the whole day without any light, any help will be greatly appreciated...
EDIT:
adding the response
{"_id":{"$oid":"56d2872f00af597fa584e366"},"authData":{"facebook":{"access_token":"[ACCESS_TOKEN_REMOVED]","expiration_date":"2015-12-19T14:17:25.000Z","id":"[ID_REMOVED]"}},"bcryptPassword":"[PASSWORD_REMOVED]","competitorFbid":null,"createdAt":null,"created_at":null,"facebookname":null,"fbid":null,"objectId":"H5cEMtUzMo","runCount":790,"sessionToken":"[SESSION_TOKEN_REMOVED]","updatedAt":null,"updated_at":null,"username":"[USERNAME_REMOVED]"}
A field in the database is declared using the field method:
field :fbid, type: String
This also defines fbid and fbid= methods to work with the fbid attribute.
An instance variable with associated accessor and mutator methods is declared using the attr_accessor method:
attr_accessor :fbid
This will also add fbid and fbid= methods to work with the underlying instance variable.
They're not the same thing. Mongoid only knows about fields, those are the things that it will work with in the database so your query works; field also defines accessor and mutator methods for your fields.
But you have an attr_accessor call after your field calls so the methods that field creates (such as fbid and fbid=) are overwritten by those created by attr_accessor. The result is that all your attributes appear to be nil.
The solution is to drop the attr_accessor call from your class. You only need the field calls.

Search in mongoid/mongo console not work

I have some fields in my model category, tags which contains lots of text
Take the following data for example,
It should be found by these search keywords Iron, Pig, Academic Data
{
"_id" : "M0130AUSM561NNBR",
"name" : "Pig Iron Production for United StatesMonthly, Not Seasonally Adjusted, ",
"categories" : [
"Production of Commodities",
"NBER Macrohistory Database",
"Academic Data"
],
"tags" : "[\"iron\", \"metals\", \"nber\", \"production\", \"monthly\", \"nation\", \"usa\", \"nsa\"]",
"updated_at" : ISODate("2014-12-30T03:38:13.954Z"),
"created_at" : ISODate("2014-12-30T03:38:13.954Z")
}
I tried to query by Indicator.text_search("Pig")
But I got nothing
irb(main):005:0> Indicator.text_search("Pig")
=> #<Mongoid::Contextual::TextSearch
selector: {}
class: Indicator
search: Pig
filter: {}
project: N/A
limit: N/A
language: default>
Tried to search with Indicator.any_of({ :text => /.*Production.*/ })
I still got nothing.
=> #<Mongoid::Criteria
selector: {"$or"=>[{"text"=>/.*Production.*/}]}
options: {}
class: Indicator
embedded: false>
Tried to search in mongo console, still not works (nothing in the result)
> db.indicators.find({"title": /.*Production.*/})
>
Model : Indicator.rb
class Indicator
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Attributes::Dynamic
# include Mongoid::Search
field :id, type: String
field :name, type: String
field :category, type: String
field :tags, type: String
# search_in :tags, :category
end
The shell query
db.indicators.find({ "title" : /.*Production.*/ })
won't find any documents because there is no title field, according to your sample documents. Is a text index defined?
db.indicators.ensureIndex({ "name" : "text", "categories" : "text", "tags" : "text" })
Additionally, tags is a string that resembles an array of strings - is that what you want? Or do you want n array of strings? I'd think you want
"tags" : ["iron", "metals", "nber", "production", "monthly", "nation", "usa", "nsa"]
rather than
"tags" : "[\"iron\", \"metals\", \"nber\", \"production\", \"monthly\", \"nation\", \"usa\", \"nsa\"]"

Rails update multiple records find based on other id

Using Rails 3.2. As shown in the doc on update method, the update finds based on id:
update(id, attributes)
# id - This should be the id or an array of ids to be updated.
# Updates multiple records
people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
Person.update(people.keys, people.values)
What if I want to update an array found based on other columns? For example:
people = { 'cook' => { "first_name" => "David" }, 'server' => { "first_name" => "Jeremy" } }
Find people with role = cook, then update first_name = David; find people with role = server, then update first_name = jeremy.
I want it to be done in 1 query if possible, and not by SQL. Thanks.
You can Achieve this with #update_all
people = { 'cook' => { "first_name" => "David" }, 'server' => { "first_name" => "Jeremy" } }
Person.update_all(people.keys, people.values)
In that case I would write my own sql statement. I depends on which database backend you are using.
http://www.postgresql.org/docs/9.1/static/plpgsql-control-structures.html
https://dev.mysql.com/doc/refman/5.0/en/case.html
The update method doesn't execute 1 SQL query when passed an array of ids and values. If you view the source code for update, you will see it loops through the array and executes 2 queries for each record (a find and an update query) to then return an array of updated objects.
If you're happy accepting that you will need to make 2 queries per row, then you can use the following code for finding people by role.
people = { 'cook' => { "first_name" => "David" }, 'server' => { "first_name" => "Jeremy" } }
updated_people = people.keys.map.with_index do |role, index|
object = Person.where(role: role).first!
object.update(people.values[index])
object
end
Note: This code only updates the first record it finds per role because I've assumed there will only be one cook with the first name 'David'.
If you want to only use 1 SQL statement, you should look at doing it in SQL like devanand suggested.

Date in Mongoid queries

I have a model:
class SimpleAction
include Mongoid::Document
field :set_date, :type => Date
and I have some data in collection:
{ "_id" : ObjectId("4f6dd2e83a698b2518000006"), "name" : "lost",
"notes" : "", "set_date(1i)" : "2012", "set_date(2i)" : "3",
"set_date(3i)" : "25", "set_date(4i)" : "13", "set_date(5i)" : "57",
"duration" : 15, "todo" : "4" }
You can see that mongoid store date in the five fields - set_date(ni).
I have two question:
How can I filter data by set_date field in the mongo console client? Something like this:
db.simple_actions.find({ set_date : { "$lte" : new Date() } })
My query didn't return any data.
How can I filter data by set_date field in my Rails controller? Something like this:
#simple_actions = SimpleAction.where(:set_date => { '$lte' => Date.today })
I would recommend not using Date, but instead DateTime:
field :set_date, :type => DateTime
Now not only will it be stored in 1 field, like so:
"set_date" : ISODate("2012-03-14T17:42:27Z")
But Mongoid will correctly handle various conversions for queries like you want:
SimpleAction.where( :set_date => { :$lte => Date.today } )
You can use it with class Time.
Rails is so good with Api for mobile (such as Android),when you sent date from mobile to Api like: 1482723520. (last_created=1482723520)
You can use it like that:
time = Time.at(params[:last_created].to_i)
And in Rails you can query like that:
Number.where(created_at: { :$gte => time })

Resources