ElasticSearch - Get different types from different indices - scala

I have two indices: A and B.
A has the following types: car, motorbike and van.
B has the following types: bus, car and pickup.
I want to be able to have a single query which gets motorbike and van from A and car and pickup from B.
I want to use a filter to do this and currently, I have:
.filter(
not(
should(
termsQuery("key", Seq("car", "bus"))
)
)
)
But obviously, this will filter car for both indices. I know I can do two separate queries for each index and filter different types for each but I want to avoid this if possible.
Is it possible to do what I am trying to do in a single query?

You can search on index and type by using the special fields _index and _type so once you know that, it's just a matter of putting together a boolean query.
search("_all").query(
boolQuery().should(
boolQuery().must(
termQuery("_index", "A"),
termsQuery("_type", "motorbike", "van")
),
boolQuery().must(
termQuery("_index", "B"),
termsQuery("_type", "car", "pickup")
)
)
)

You can do something like this.
GET _search
{
"query": {
"bool": {
"should": [
{
"bool": {
"filter": [
{
"term": {
"_index": {
"value": "A"
}
}
},
{
"terms": {
"_type": ["motorbike","van"]
}
}
]
}
},
{
"bool": {
"filter": [
{
"term": {
"_index": {
"value": "B"
}
}
},
{
"terms": {
"_type": ["car","pickup"]
}
}
]
}
}
]
}
}
}

Related

How to save deletion in a deeply nested MongoDB document

I am new to MongoDB and I am using MongoDB shell to perform the operations.
I am working to remove the array named Process from all the Items, but it seems that I do not grasp the remove concept correctly.
The documents we use are deeply nested - we do not know how many items there are, or how deep the level of nesting.
What I tried so far is to use recursion to iterate through the items:
function removeAllProcessFields(docItems)
{
if(Array.isArray(docItems))
{
docItems.forEach(function(item)
{
print("idItem: "+item._id);
if(item.Process == null)
{
print("Process null");
}
else
{
$unset: { Process: ""}
}
removeAllProcessFields(item.Items);
})
}
}
var docs = db.getCollection('MyCollection').find({})
docs.forEach(function(doc)
{
print("idDoc: "+doc._id);
removeAllProcessFields(doc.Items);
})
But I have difficulties on using unset properly to save the operation.
An example document would be:
{
"_id": "622226d319517e83e8ed6151",
"Name": "test1",
"Description": "",
"Items": [{
"_id": "622226d319517e83e8ed614e",
"Name": "test-item",
"Description": "",
"Process": [{
"Name": "Step1"
}, {
"Name": "Step2"
}],
"Items": [{
"_id": "622226d319517e83e8ed614f",
"Name": "test-subItem1",
"Description": "",
"Process": [{
"Name": "StepSub1"
}, {
"Name": "StepSub2"
}, {
"Name": "StepSub3"
}],
"Items": []
},
{
"_id": "622226d319517e83e8ed6150",
"Name": "test-subItem2",
"Description": "",
"Process": [{
"Name": "StepSub4"
}, {
"Name": "StepSub5"
}, {
"Name": "StepSub6"
}],
"Items": []
}
]
}]
}
What I hope to achieve would be:
{
"_id": "622226d319517e83e8ed6151",
"Name": "test1",
"Description": "",
"Items": [{
"_id": "622226d319517e83e8ed614e",
"Name": "test-item",
"Description": "",
"Items": [{
"_id": "622226d319517e83e8ed614f",
"Name": "test-subItem1",
"Description": "",
"Items": []
},
{
"_id": "622226d319517e83e8ed6150",
"Name": "test-subItem2",
"Description": "",
"Items": []
}
]
}]
}
Something like this maybe using the $[] positional operator:
db.collection.update({},
{
$unset: {
"Items.$[].Items.$[].Process": 1,
"Items.$[].Process": 1
}
})
You just need to construct it in the recursion ...
playground
JavaScript recursive function example:
mongos> db.rec.find()
{ "_id" : ObjectId("622a6c46ae295edb276df8e2"), "Items" : [ { "a" : 1 }, { "Items" : [ { "Items" : [ { "Items" : [ ], "Process" : [ 1, 2, 3 ] } ], "Process" : [ 4, 5, 6 ] } ], "Process" : [ ] } ] }
mongos> db.rec.find().forEach(function(obj){ var id=obj._id,ar=[],z=""; function x(obj){ if(typeof obj.Items != "undefined" ){ obj.Items.forEach(function(k){ if( typeof k.Process !="undefined" ){ z=z+".Items.$[]";ar.push(z.substring(1)+".Process") }; if(typeof k.Items != "undefined"){x(k)}else{} }) }else{} };x(obj);ar.forEach(function(del){print( "db.collection.update({_id:ObjectId('"+id+"')},{$unset:{'"+del+"':1}})" );}) })
db.collection.update({_id:ObjectId('622a6c46ae295edb276df8e2')},{$unset:{'Items.$[].Process':1}})
db.collection.update({_id:ObjectId('622a6c46ae295edb276df8e2')},{$unset:{'Items.$[].Items.$[].Process':1}})
db.collection.update({_id:ObjectId('622a6c46ae295edb276df8e2')},{$unset:{'Items.$[].Items.$[].Items.$[].Process':1}})
mongos>
Explained:
Loop over all documents in collection with forEach
Define recursive function x that will loop over any number of nested Items and identify if there is Process field and push to array ar
Finally loop over array ar and construct the update $unset query , in the example only printed for safety , but you can improve generating single query per document and executing unset query ...
Assuming you are on v>=4.4 you can use the "merge onto self" feature of $merge plus defining a recursive function to sweep through the collection and surgically remove one or a list of fields at any level of the hierarchy. The same sort of needs arise when processing json-schema data which is also arbitrarily hierarchical.
The solution below has extra logic to "mark" documents that had any modifications so the others can be removed from the update set passed to $merge. It also can be further refined to reduce some variables; it was edited down from a more general solution that had to examine keys and values.
db.foo.aggregate([
{$replaceRoot: {newRoot: {$function: {
body: function(obj, target) {
var didSomething = false;
var process = function(holder, spot, value) {
// test FIRST since [] instanceof Object is true!
if(Array.isArray(value)) {
for(var jj = value.length - 1; jj >= 0; jj--) {
process(value, jj, value[jj]);
}
} else if(value instanceof Object) {
walkObj(value);
}
};
var walkObj = function(obj) {
Object.keys(obj).forEach(function(k) {
if(target.indexOf(k) > -1) {
delete obj[k];
didSomething = true;
} else {
process(obj, k, obj[k]);
}
});
}
// ENTRY POINT:
if(!Array.isArray(target)) {
target = [ target ]; // if not array, make it an array
}
walkObj(obj);
if(!didSomething) {
obj['__didNothing'] = true;
}
return obj;
},
// Invoke!
// You can delete multiple fields with an array, e.g.:
// ..., ['Process','Description']
args: [ "$$ROOT", 'Process' ],
lang: "js"
}}
}}
// Only let thru docs WITHOUT the marker:
,{$match: {'__didNothing':{$exists:false}} }
,{$merge: {
into: "foo",
on: [ "_id" ],
whenMatched: "merge",
whenNotMatched: "fail"
}}
]);

Sanity.io GROQ query for array of objects

I'm learning to code and now I am on the stage of a small pet project with Sanity as a CMS.
Long story short, making an API I'm trying to fetch cocktails data with votes for the cocktails. The votes are stored within persons who voted:
GROQ query
*[
_type == "cocktail" &&
!(_id in path('drafts.**'))
] {
name,
_id,
"votes" : *[_type == "person" && references(^._id)] {
votes[] {
score,
"id": cocktail._ref
}
}
}
which returns
[
{
"_id": "pdUGiuRzgLGpnc4cfx76nA",
"name": "Cuba Libre",
"votes": [
{
"votes": {
"id": "pdUGiuRzgLGpnc4cfx76nA",
"score": 2
}
},
{
"votes": {
"id": "pdUGiuRzgLGpnc4cfx76nA",
"score": 2
}
}
]
},
{
"_id": "pdUGiuRzgLGpnc4cfxBOyM",
"name": "The ERSH7",
"votes": []
}
]
As you can see, the merge provides embedded arrays of votes meanwhile I want sth like:
[{
...cocktail attributes...
"votes" : [
{score: 2, id: pdUGiuRzgLGpnc4cfx76nA},
{score: 2, id: pdUGiuRzgLGpnc4cfx76nA}
]
}
... more cocktails....
]
Trying to get this I modified the query:
*[
_type == "cocktail" &&
!(_id in path('drafts.**'))
] {
name,
_id,
"votes" : *[_type == "person" && references(^._id)].votes[] {
score,
"id": cocktail._ref
}
}
which should take a projection from every element of the votes arr. Unfortunately I get empty arrays:
[
{
"_id": "pdUGiuRzgLGpnc4cfx76nA",
"name": "Cuba Libre",
"votes": [
{},
{}
]
}
...more cocktails
]
How can I achieve the desired result?
Thank you for reading! Would appreciate any help!
Yes, had similar struggles with "flattening" the projections my self. I solved it with dot-syntax. Try just adding .votes on your first attempt:
*[
_type == "cocktail" &&
!(_id in path('drafts.**'))
]
{
name,
_id,
"votes" : *[_type == "person" && references(^._id)] {
votes[] {
score,
"id": cocktail._ref
}
}
.votes
}
If this is correct, the whole query can be simplified but I'm not at the level, yet, where I can do that without testing against a similar set ,'-)
Actually, GROQ has syntax to flatten array starting from v2021-03-25
find this example:
{
'nestedArray': [
{'foo': [1,2,3,4,5]},
{'foo': [6,7,8,9,10,11,12]},
{'foo': [13,14,15]},
]
}{
'stillNestedArray': #.nestedArray[].foo,
'flatArray': #.nestedArray[].foo[]
}
Note the [] right after foo - this is what's flatten it
So the original query from the question should look like
*[
_type == "cocktail" &&
!(_id in path('drafts.**'))
] {
name,
_id,
"votes" : *[_type == "person" && references(^._id)] {
votes[] {
score,
"id": cocktail._ref
}
}.votes[]
}

How to query nested heterogeneous document

I have an array that have this format:
data{
[sequentialId]{guid:value1,name:value2}
}
I need to do something like db.data.find("data.?.name":"value1")
All the solutions I've seen don't work because they are expecting me to know the name of the attribute, but in this case the ID is sequential.
I've looked at the standard way of querying nested documents described here
https://docs.mongodb.com/manual/tutorial/query-array-of-documents/
I also looked at some options like $unwind but I can't get anything to work
here's a small sample
{"41":{"b":453081600,"f":1,"h":171,"s":4,"w":4,"wr":[2,0]},
"80":{"b":337132800,"f":2,"h":169,"s":4,"w":4,"wr":[0,0]},
"388":{"b":148694400,"f":1,"h":188,"l":{"c":[{"e":2001,"g":13,"m":292,"s":1992,"t":18},{"e":2006,"g":11,"m":197,"s":2001,"t":1},{"e":2009,"g":2,"m":111,"s":2006,"t":1790},{"e":2009,"g":0,"m":1,"s":2009,"t":1937},{"e":2010,"g":1,"m":14,"s":2010,"t":1},{"e":2011,"g":0,"m":8,"s":2010,"t":13}],"n":[{"e":2007,"g":1,"m":73,"s":1996,"t":1318}]},"s":2,"w":3,"wr":[0,0]}}
for example in this set of data I might need to query all the docs where "f"=1
The following query can get us the expected output:
Note: We are fetching the documents which have name is equal to a
db.collection.aggregate([
{
$addFields:{
"filtered":{
$filter:{
"input":"$data",
"as":"info",
"cond":{
$let:{
"vars":{
"array":{
$objectToArray:"$$info"
}
},
"in":{
$in:["a","$$array.v.name"]
}
}
}
}
}
}
},
{
$match:{
"filtered.0":{
$exists:true
}
}
},
{
$project:{
"filtered":0
}
}
]).pretty()
Data set:
{
"data":[
{
"41": {
"b": 453081600,
"name": "a"
},
"80": {
"b": 337132800,
"name": "b"
},
"388": {
"b": 148694400,
"name": "c"
}
}
]
}
{
"data":[
{
"41": {
"b": 453081600,
"name": "b"
},
"80": {
"b": 337132800,
"name": "b"
},
"388": {
"b": 148694400,
"name": "c"
}
}
]
}
Output:
{
"data":[
{
"41": {
"b": 453081600,
"name": "a"
},
"80": {
"b": 337132800,
"name": "b"
},
"388": {
"b": 148694400,
"name": "c"
}
}
]
}
Query analysis:
We are creating a filtered array to hold only those records from data array which contains name equal to a inside any subdocument with an unknown key.
It's done by first converting each document of data into an array of key-value pairs. Now we can directly query the values without worrying about the keys.
Finally, filter those documents in which the size of filtered array is greater than zero.

Cannot create an array of nested object using a project operation

I'm forced to use the aggregation framework and the project operation of Spring Data MongoDb.
What I'd like to do is creating an array of object as a result of a project operation.
For example, considering this intermediate aggregation result:
[
{
"content": {
"processes": [
{
"id": "101a",
"title": "delivery"
},
{
"id": "101b",
"title": "feedback"
}
]
}
}
]
What I want to obtain is this:
[
{
"results":
{
"titles": [
{
"id": "101a",
"value": "delivery"
},
{
"id": "101b",
"value": "feedback"
}
]
}
}
]
This was just an example, I don't want to simply "rename" some fields.
What I want is the possibility to create an array of objects.
If I try something like this:
projectionOperation
.and("$content.processes.id").as("results.titles.id")
.and("$content.processes.title").as("results.titles.value");
I obtain this:
[
{
"results":
{
"titles": {
"id": ["101a", "101b"]
"value": ["delivery", "feedback"]
}
}
}
}
]
With this projection the array is created, but not "in the proper position".
However, If I use the nested operator, I haven't figure out a way to specify that I want to create an array instead of an object.
With this projection:
projectionOperation.and("results.titles")
.nested(
bind("id", "process.id")
.and("value", "process.title")
);
I can create a proper nested object but not into an array:
"results.titles": {
"id": "101b",
"value": "feedback"
}
You can try below aggregation code.
ProjectionOperation po = Aggregation.project().and(
VariableOperators.mapItemsOf("content.processes").as("rt")
.andApply(
new AggregationExpression() {
#Override
public Document toDocument(AggregationOperationContext aggregationOperationContext) {
return new Document("id", "$$rt.id").append("value", "$$rt.title");
}
}
)
).as("result");

MongoDb aggregation project onto collection

I've a problem with a huge MongoDb aggregation pipeline. I've many constraint and I've simplified the problem a lot. Hence, don't discuss the goal for this query.
I've a mongo aggregation that gives something similar to this:
[
{
"content": {
"processes": [
{
"id": "101a",
"title": "delivery"
},
{
"id": "101b",
"title": "feedback"
}
]
}
}
]
To this intermediate result I'm forced to apply a project operation in order to obtain something similar to this:
[
{
"results":
{
"titles": [
{
"id": "101a",
"value": "delivery"
},
{
"id": "101b",
"value": "feedback"
}
]
}
}
]
enter code here
But applying this projections:
"results.titles.id": "$content.processes.id",
"results.titles.value": "$content.processes.title"
I obtain this:
[
{
"results":
{
"titles": {
"id": ["101a", "101b"]
"value": ["delivery", "feedback"]
}
}
}
}
]
Collection are created but not in the proper position.
Is it possible to exploit some operator inside the project operation in order to tell mongo to create an array in a parent position?
Something like this:
"results.titles.$[x].value" : "$content.processes.value"
You can use the dot notation to project entire array:
db.col.aggregate([
{
$project: {
"results.titles": "$content.processes"
}
}
])
and if you need to rename title to value then you have to apply $map operator:
db.col.aggregate([
{
$project: {
"results.titles": {
$map: {
input: "$content.processes",
as: "process",
in: {
id: "$$process.id",
value: "$$process.title"
}
}
}
}
}
])