RethinkDB index query with several .contains() - rethinkdb

I have the following query that works fine but is slow, however I can't figure out how to index it properly:
r.db('my_db')
.table('messages')
.filter({ community_id : community.id})
.filter(function(row){
return row('mentions').contains(user.id);
})
.filter(function(row){
return row('channels').contains(channel.id);
})
.orderBy(r.desc('created_at'))
.skip(0)
.limit(50);
I tried with the following index (using Thinky.js):
Model.ensureIndex("user_mentions", function(message){
return message("mentions").map(function(user_id){
return message("channels").map(function(channel_id){
return [
message("community_id"),
message("mentions").contains(user_id),
message("channels").contains(channel_id),
message('created_at')
];
});
});
}, {multi: true});
And then to query it I've tried this:
r.db('my_db')
.table('messages')
.between(
[community.id, user.id, channel.id, r.minval],
[community.id, data.user.id, channel.id, r.maxval],
{ index : 'user_mentions' }
)
.orderBy({index:r.desc("user_mentions")})
.skip(0)
.limit(50);
The messages table looks like:
id | community_id | mentions (array of user_ids) | channels (array of channel_ids) | created_at
But I end up getting zero results.
I greatly appreciate any suggestions!

I think this index will make the between query you wrote above work:
.indexCreate(function(message) {
return message('channels').concatMap(function(channel) {
return message('mentions').map(function(mention) {
return [
message('community_id'),
mention,
channel,
message('created_at')
];
});
});
}, {multi: true});

Related

How to modify just a property from a dexie store without deleting the rest?

I'm having the dexie stores showed in the print screen below:
Dexie stores print screen
My goal is to update a dexie field row from a store without losing the rest of the data.
For example: when I edit and save the field "com_name" from the second row (key={2}) I want to update "com_name" only and not lose the rest of the properties, see first and the third row.
I already tried with collection.modify and table.update but both deleted the rest of the properties when used the code below:
dexieDB.table('company').where('dexieKey').equals('{1}')
//USING table.update
//.update(dexieRecord.dexiekey, {
// company: {
// com_name: "TOP SERVE 2"
// }
//})
.modify(
{
company:
{
com_name: TOP SERVE 2
}
}
)
.then(function (updated) {
if (updated)
console.log("Success.");
else
console.log("Nothing was updated.");
})
.catch(function (err) { console.log(err); });
Any idea how can I accomplish that?
Thanks
Alex
You where right to use Table.update or Collection.modify. They should never delete other properties than the ones specified. Can you paste a jsitor.com or jsfiddle repro of that and someone may help you pinpoint why the code doesn't work as expected.
Now that you are saying I realised that company and contact stores are created dynamically and editedRecords store has the indexes explicitly declared therefore when update company or contact store, since dexie doesn't see the indexes will overwrite. I haven't tested it yet but I suspect this is the behaviour.
See the print screen below:
Dexie stores overview
Basically I have json raw data from db and in the browser I create the stores and stores data based on it, see code below:
function createDexieTables(jsonData) { //jsonData - array, is the json from db
const stores = {};
const editedRecordsTable = 'editedRecords';
jsonData.forEach((jsonPackage) => {
for (table in jsonPackage) {
if (_.find(dexieDB.tables, { 'name': table }) == undefined) {
stores[table] = 'dexieKey';
}
}
});
stores[editedRecordsTable] = 'dexieKey, table';
addDataToDexie(stores, jsonData);
}
function addDataToDexie(stores, jsonData) {
dbv1 = dexieDB.version(1);
if (jsonData.length > 0) {
dbv1.stores(stores);
jsonData.forEach((jsonPackage) => {
for (table in jsonPackage) {
jsonPackage[table].forEach((tableRow) => {
dexieDB.table(table).add(tableRow)
.then(function () {
console.log(tableRow, ' added to dexie db.');
})
.catch(function () {
console.log(tableRow, ' already exists.');
});
});
}
});
}
}
This is the json, which I convert to object and save to dexie in the value column and the key si "dexieKey":
[
{
"company": [
{
"dexieKey": "{1}",
"company": {
"com_pk": 1,
"com_name": "CloudFire",
"com_city": "Round Rock",
"serverLastEdit": [
{
"com_pk": "2021-06-02T11:30:24.774Z"
},
{
"com_name": "2021-06-02T11:30:24.774Z"
},
{
"com_city": "2021-06-02T11:30:24.774Z"
}
],
"userLastEdit": []
}
}
]
}
]
Any idea why indexes were not populated when generating them dynamically?
Given the JSON data, i understand what's going wrong.
Instead of passing the following to update():
{
company:
{
com_name: "TOP SERVE 2"
}
}
You probably meant to pass this:
{
"company.com_name": "TOP SERVE 2"
}
Another hint is to do the add within an rw transaction, or even better if you can use bulkAdd() instead to optimize the performance.

Use the first query result as a condition for the second query

I have 2 queries like this
const featuredArticles = gql `
query featureArticles() {
articles(where: {limit: 4, feature: true, sort: "published_at:desc") {
id
}
}
`;
const NO_FEATURE_ARTICLES_QUERY = gql`
query noFeatureArticles($slug: String!) {
articles(where: { id_nin: ??? },limit: 4, sort: "published_at:desc") {
${SINGLE_ARTICLE_MODEL}
}
}
`;
The first query will get the ID of 4 latest featured articles, the second one will get all articles available in the database. Now, I'm trying to change the second query to get all articles EXCEPT 4 articles from the first query but I just don't know how to use the result as a condition. Can you guy give me some hint? Thanks in advance!
My index.jsx looks like this:
const Home = () => (
<div style={{
overflowX: 'hidden',
}}
>
let arr=[]
<Query query={FEATURE_ARTICLES_QUERY}>
{({ data: { articles } }) => {
if (!articles.length) {
return null;
}
arr=[articles[0].id,.......,articles[10].id];
return <SectionFeature articles={articles} />;
}}
</Query>
<Query query={ARTICLES_QUERY} id={arr}>
{({ data: { quotes } }) => {
if (!quotes.length) {
return null;
}
return <SectionQuote quotes={quotes} />;
}}
</Query>
</div>
);
export default Home;
For example, I want to pass 1st query's result to arr[], then pass it to 2nd query. But the array will be gone right after the 1st query ends.
UPDATE: I'd done it. Thank you so much for the hint

How to use ReQL filter and match command on arrays

I have a table in rethinkdb where each row has following structure -
{
'name':'clustername',
'services':[
{
'name':'service1'
},
{
'name':'service2'
}
]
}
I am running a query to filter service2 object like this
r.table('clusters').filter({"name": "clustername"})
.pluck('services').filter((service) => {
return service("name").match('service2')
})
But this is not returning anything: No results were returned for this query
Can anyone tell why this is happening?
pluck returns sequence, so this query:
r.table('clusters').filter({"name": "clustername"}).pluck('services')
will return:
{
"services": [
{
"name": "service1"
} ,
{
"name": "service2"
}
]
}
You need get services field from it, it will return array with services field of items found by filter.
And after that you need to use your second filter on each item by using map.
So, correct query:
r.table('clusters').filter({"name": "clustername"}).pluck('services')("services").map(item => {
return item.filter(service => {
return service("name").match("service2");
});
})

updating an array of nested documents rethinkdb

I have a document schema like this:
{
"name":"",
"type":"",
"posts":[
{
"url":"",
"content":"",
...
},
{
"url":"",
"content":"",
...
}
...
]
}...
I forgot to create id's for each post on insertion in database. So i'm trying to create a query for that:
r.db('test').table('crawlerNovels').filter(function (x){
return x.keys().contains('chapters')
}).map(function (x){
return x('chapters')
}).map(
function(x){
return x.merge({id:r.uuid()})
}
)
instead this query return all posts with an id but doesn't actually update in the database. I tried using a forEach instead of a map function at the end this doesn't work
After lots of tweaking and frustration i figured it out:
r.db('test').table('crawlerNovels').filter(function (x){
return x.keys().contains('chapters')
}).update(function(novel){
return {"chapters":novel('chapters').map(
function(chapter){
return chapter.merge({"id":r.uuid()})
})}
},{nonAtomic:true})

How to use secondary indexes for a "contains" query

Rethinkdb docs has this example to improve getAll/contains queries with a secondary index:
// Create the index
r.table("users").indexCreate("userEquipment", function(user) {
return user("equipment").map(function(equipment) {
return [ user("id"), equipment ];
});
}, {multi: true}).run(conn, callback);
// Query equivalent to:
// r.table("users").getAll(1).filter(function (user) {
// return user("equipment").contains("tent");
// });
r.table("users").getAll([1, "tent"], {index: "userEquipment"}).distinct().run(conn, callback);
My questions is if there's a way to do the same but for querying with multiple tags. What would be the equivalent to make this query possible with a secondary index?
r.table("users").getAll(1).filter(function (user) {
return user("equipment").contains("tent", "tent2");
});
Probably we can do this
r.table("users").getAll([1, "tent"]).filter(function (user) {
return user("equipment").contains("tent2");
});
So build a multi index as you did, and try to getAll first, so that part is efficient with index, then filter to continue ensure that equipment contains array we want.

Resources