How can I query a dynamodb2 table with an index using boto? I'm not able to piece together enough information from the documentation or unit tests for boto.
I have a local index created as:
fields.KeysOnlyIndex('NameIndex', parts=[
fields.HashKey('account_id', data_type='S'),
fields.RangeKey('name', data_type='S'),
])
And would like to lookup an item using the account_id and name.
Attempting to make the call table.query( account_id__eq=account['id'], name__eq = name ) results in the error Query condition missed key schema element id.
Note: I would also prefer to avoid using the Table class and work directly with the connection.
with Table:
table = Table('accounts')
results = table.query(index='NameIndex', account_id__eq=account['id'], name__eq=name)
or with Connection:
results = conn.query(
table_name='accounts',
index_name='NameIndex',
select='ALL_PROJECTED_ATTRIBUTES',
key_conditions={
'account_id': {
'AttributeValueList': [
{'S': account['id']},
],
'ComparisonOperator': 'EQ',
},
'name': {
'AttributeValueList': [
{'S': name},
],
'ComparisonOperator': 'EQ',
},
}
)
Related
i am learning knexjs and this is my code:
await knex('vendor_message').insert(data).returning(['message_id',
'type',
'content',
'created_on',
'last_update_on',
'vendor_email']);
i was expecting it to return those fields from the db something like this:
[
{
'message_id': 'asdsd',
'type':2,
'content':'adddd',
'created_on':'xxxx',
'last_update_on':'xxxx',
'vendor_email':'xxx#gmail.com'
}
]
but it only returns the id: e.g.:
[ 687 ]
i don't want to return the arguments inserted, i want it from the db
returning is not supported by MySQL.
Utilized by PostgreSQL, MSSQL, and Oracle databases, the returning method specifies which column should be returned by the insert and update methods.
Just use the insert id to fetch the new row.
As the title states, im trying to make a query that doesnt return the entire document, but only certain fields, but with multiple exact terms.
Im using Guzzle from laravel to contruct my query:
$response = $client->post('cvr-permanent/_search', [
'auth' => ['USERNAME', 'PASSWORD'],
'json' => [
"_source" => ["Vrvirksomhed.attributter", "Vrvirksomhed.deltagerRelation.organisationer.medlemsData.attributter"],
"query" => [
"bool"=> [
"must"=> [
[
"term"=> [
"Vrvirksomhed.cvrNummer" => $vat
]
]],
"must_not"=> [ ],
"should"=> [ ]
]
],
"from"=> 0,
"size"=> 10,
"sort"=> [ ]
]
]);
I want the data from the Vrvirksomhed.cvrNummer and the data i want is where Vrvirksomhed.attributter.type => "KAPITAL" and Vrvirksomhed.deltagerRelation.deltager.navne and where Vrvirksomhed.deltagerRelation.organisation.attributter.type = "EJERANDEL_PROCENT"
Im very confused about how to make this query work because it is multiple terms but not really. Also very new to elasticsearch.
I tried the "terms" but couldnt really get it to work.
The query i have made above, return way too much data i dont need, and not all the data i DO need.
Hope you can help
**EDIT
Something like this maybe, but translated to elasticsearch
SELECT attributter.type": "KAPITAL" AND deltagerRelation.deltager.navne AND deltagerRelation.organisation.attributter.type": "EJERANDEL_PROCENT FROM Vrvirksomhed WHERE cvrNummer = $vat
***EDIT
Hopefully more clarification:
Okay, sorry ill try to make it clearer. The object i want is a company with a certain vat number. So Vrvirksomhed.cvrNummer is that, and that has to be the term. It returns a gigantic object with so many arrays in arrays. I do not want all of this data but only some of it. The data i need from this big object, is the object in the array Vrvirksomhed.attributter that has the type : "KAPITAL field, and not all of the attributter. Then i want Vrvirksomhed.deltagerRelation.deltager.navne which i can get by just putting it in the _source because i want all of these objects. But then i want Vrvirksomhed. deltagerRelation.organisation.attributter that again is a bunch of objects in the array attributter but i only want the ones with the type : "EJERANDEL_PROCENT
So i can´t really add them as additional "terms" because the only real term is the "cvrNummer", everything else is just filtering the response. I tried with filters etc, but to no avail
Heres a pastebin so you can see the clusterfuck i am dealing with. THis is what i have been able to sort it to so far, with putting the things in _source but without the extra "filtering" of "KAPITAL" and "EJERANDEL_PROCENT"
https://pastebin.com/b8hWWz1R
You want to get only documents which match several conditions, and you need only a subset of fields from those documents, correct?
In SQL (taking some liberties with the field names and structure), your query would be something like:
SELECT cvrNummer
FROM Vrvirksomhed
WHERE attributter_type = 'KAPITAL'
AND deltagerRelation_deltager_navne = 'you left this out in your question'
AND deltagerRelation_organisation_attributter_type = 'EJERANDEL_PROCENT'
As explained in the Elasticsearch Guide†, the equivalent to this in Elasticsearch is a query with a bool clause that contains all your conditions, and a _source parameter which says what fields you want to get back in the response. Something like the following:
{
"_source": ["cvrNummer"]
"query": {
"bool": {
"must": [
{ "term": "attributter.type": "KAPITAL" },
{ "term": "deltagerRelation.deltager.navne": "you left this out in your question" },
{ "term": "deltagerRelation.organisation.attributter.type": "EJERANDEL_PROCENT" }
]
}
}
}
† Do note that the syntax in this guide is for Elasticsearch 2.x. The current version is 7.x, and many things have changed since then!
See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html for how to construct a bool query using the new syntax;
see https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html for how to use the term-level queries, which you probably want;
also see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html and consider using filter context, since you probably don't care about the score of your query.
I have 5 db tables:
Template, Capability, Deployment, Level, Requirement
Template (PK) -> has many Capability (FK)
Capability (PK) -> has many Deployments (FK) && Capability has many Levels (FK)
Level (PK) -> has many Requirement (FK)
I want to retrieve all templates, with the related foreign keys all the way down to the Requirements.
However, I would like to limit the number of Levels to just the ones that are not deleted (isDeleted : false).
This works if there is DATA retrieved, BUT if there are no records returned from the Levels tables based on the Where (isDeleted), Sequelize will return an empty array for the Deployments table as well. This query correct data if records are found from the Levels table. However, shouldn't the records from the deployment table be returned regardless since it is a different table?
Template.hasMany(Capability, { foreignKey : {name : 'templateId'} })
Capability.hasMany(Deployment, { foreignKey : {name : 'capabilityId'} });
Capability.hasMany(Level, {foreignKey : {name : 'capabilityId'} });
Level.hasMany(Requirement, {foreignKey : {name : 'levelId'}});
const allModules =
await Template.findAll({
include : [ {
model: Capability,
as: 'capabilities',
include : [
{
model: Deployment,
as : 'deployments'
},{
model: Level,
as : 'levels',
where : { isDeleted : false }, // this is the failing part ( if no records are returned both the Deployment recordset and the Level recordset fail)
include : [{
model : Requirement,
as : 'requirements',
}],
},]
}],
})
You might be asking for something impossible. You said:
Capability.hasMany(Level, ...
Level.hasMany(Requirement ....
If there is no Level (or, if the level is marked as deleted), the link between Capability and Requirement is broken, so those Requirements cannot be retrieved. Under what capability would they appear?
OTOH, If you're looking for other outer join situations, try required : false. For example, you could add this to your query to retrieve Capabilities that have no deployments as follows:
...
include : [ {
model: Capability,
as: 'capabilities',
include : [
{
model: Deployment,
as : 'deployments',
required : false // outer join
}
...
I am trying to build a simple application using loopback.io as process of my learning. I have set up the project, created models and apis are working fine.
Now I am trying to create a custom api which can get the data from two different models by making a join query. So i have a two models
stories : id, title, noteId
notes : id , desc
i have stories.js file as
module.exports = function(Stories) {
Stories.list = function(cb) {
// make a join query
};
Stories.remoteMethod(
'list', {
http: {
path: '/list',
verb: 'get'
},
returns: {
arg: 'list',
type: 'array'
}
}
);
};
In general i will make a join in php api but here i am bit confused.Can i pass a raw query to database here or does loopback has some different way of achieving this. Any help would be appreciated.
You don't need to pass sql query. You can query data using PersistedModel find method by using include filter
In order to use include filter you have to create model relation.
For example:
Note relation:
"relations": {
"stories": {
"type": "hasMany",
"model": "Story",
"foreignKey": "noteId"
}
},
Query:
Note.find({include: ['stories']}, function(err, data) { ... });
I have a query I'm trying to perform based on a one to many relationship.
As an example there is a model called Users and one called Projects.
Users hasMany Projects
Projects have many types which are stored in a type (enum) column. There are 4 different types that potentially a user may have that I want to load. The catch is I want to include the most recent project record (createdAt column) for all networks that potentially will be there. I have not found a way to structure the query for it to work as an include. I have however found a way to do a raw query which does what I want.
I am looking for a way without having to do a raw query. By doing the raw query I have to map the returned results to users I've returned from the other method, or I have to do a simple include and then trim off all the results that are not the most recent. The latter is fine, but I see this getting slower as a user will have many projects and it will keep growing steadily.
This allow serialize a json for anywhere action about a model. Read it, very well
sequelize-virtual-fields
// define models
var Person = sequelize.define('Person', { name: Sequelize.STRING });
var Task = sequelize.define('Task', {
name: Sequelize.STRING,
nameWithPerson: {
type: Sequelize.VIRTUAL,
get: function() { return this.name + ' (' + this.Person.name + ')' }
attributes: [ 'name' ],
include: [ { model: Person, attributes: [ 'name' ] } ],
order: [ ['name'], [ Person, 'name' ] ]
}
});
// define associations
Task.belongsTo(Person);
Person.hasMany(Task);
// activate virtual fields functionality
sequelize.initVirtualFields();