ElasticSearch Issue With Matching Results - search

I have an issue querying where if 'ford' is in the database and I search for 'fordddddddd' it returns a match. I have ngrams for sub partial matching for queries like 'fo', for', ford' but 'fordddddd' should not match. What could be the issue? Below are my setting, mappings, and query.
Settings:
settings: {
number_of_shards: 1,
analysis: {
filter: {
ngram_filter: {
type: 'edge_ngram',
min_gram: 2,
max_gram: 15
}
},
analyzer: {
ngram_analyzer: {
type: 'custom',
tokenizer: 'standard',
filter: [
'lowercase',
'ngram_filter'
]
}
}
}
}
Mappings:
mappings: [
{
name: 'car',
_all: {
type: 'string',
analyzer: 'ngram_analyzer'
},
properties: {
description: {
properties: {
name: {
type: 'string',
include_in_all: true,
term_vector: 'yes',
analyzer: 'ngram_analyzer'
},
model: {
type: 'string',
include_in_all: true,
term_vector: 'yes',
analyzer: 'ngram_analyzer'
}
}
}
}
}
]
Query:
GET car/_search
{
"query": {
"match": {
"_all": {
"query": "Forddddddd"
}
}
}
}

The problem here is you are using the same analyzer for indexing and searching.
when you search for Forddddd, ngram_analyzer is applied and it generates tokens fo, for, ford and hence it gives you results, you should specify standard analyzer for searching and you will get desired results.
properties: {
name: {
type: 'string',
include_in_all: true,
term_vector: 'yes',
analyzer: 'ngram_analyzer',
search_analyzer: 'standard' <--- here
},
model: {
type: 'string',
include_in_all: true,
term_vector: 'yes',
analyzer: 'ngram_analyzer',
search_analyzer: 'standard' <--- here
}
}

Related

How to aggregate and filter mongodb collections correctly in node (mongoose)?

I have a collection of motorcycles in my MongoDB database. Each motorcycle (collection) has an array of units and each unit has an array of parts with the name and SKU number. I'm trying to fetch only those units which contain given SKU number (in this example PT00002).
I tried to use aggregation, project and filter, but every time I get empty array of units:
let responseData = await Model.aggregate([
{
$match: {
'units.parts.SKU': sku,
}
},
{
$project: {
'units': {
$filter: {
input: '$units',
as: 'unit',
cond: {
$eq: [
'$$unit.parts.SKU', sku
]
}
}
}
}
}
]);
Documents look like this:
{
_id: ObjectId('6351183b841ef5ca0e090482'),
name: 'Yamaha Tenere 2022',
units: [
{
name: 'Front wheel',
parts: [
{
SKU: 'PT00001',
name: 'Bolt m7'
},
{
SKU: 'PT00002',
name: 'Oring'
},
]
},
{
name: 'Rear wheel',
parts: [
{
SKU: 'PT00003',
name: 'Bolt m7'
},
{
SKU: 'PT00002',
name: 'Oring'
},
]
}
]
}
and
{
_id: ObjectId('6351183b841ef5ca0e090483'),
name: 'Yamaha Tenere 2021',
units: [
{
name: 'Exhaust system',
parts: [
{
SKU: 'PT00012',
name: 'Screw torx'
},
{
SKU: 'PT00002',
name: 'Oring'
},
]
},
{
name: 'Suspension',
parts: [
{
SKU: 'GGG02',
name: 'Front fork'
},
{
SKU: 'GGG02',
name: 'Rear fork'
},
]
}
]
}
Expected output (for SKU PT00002 ) should be like this (no "Suspension unit" in array):
{
_id: ObjectId('6351183b841ef5ca0e090482'),
name: 'Yamaha Tenere 2022',
units: [
{
name: 'Front wheel',
parts: [
{
SKU: 'PT00001',
name: 'Bolt m7'
},
{
SKU: 'PT00002',
name: 'Oring'
},
]
},
{
name: 'Rear wheel',
parts: [
{
SKU: 'PT00003',
name: 'Bolt m7'
},
{
SKU: 'PT00002',
name: 'Oring'
},
]
}
]
},
{
_id: ObjectId('6351183b841ef5ca0e090483'),
name: 'Yamaha Tenere 2021',
units: [
{
name: 'Exhaust system',
parts: [
{
SKU: 'PT00012',
name: 'Screw torx'
},
{
SKU: 'PT00002',
name: 'Oring'
},
]
},
]
}
Looks like I had small error with my aggregate query. The one below works:
let responseData = await Model.aggregate([
{
$match: {
'units.parts.SKU': sku,
}
},
{
$project: {
'name': 1,
'year': 1,
'code': 1,
'category': 1,
'units': {
$filter: {
input: '$units',
as: 'unit',
cond: {
$in: [
sku, '$$unit.parts.SKU'
],
}
}
}
}
}
]);

MongoDB aggregation project with cond not working

I'm getting "MongoServerError: Invalid $project :: caused by :: Unrecognized parameter to $cond: $if" for this code. Whats wrong with it and how do i fix it ?
{
$project: {
_id: 1,
date: 1,
slot: 1,
franchiseId: 1,
status: 1,
order: {
$cond: {
$if: { $gt: ['$carspa', null] },
$then: {
price: '$carspa.price',
mode: '$carspa.mode',
address: '$carspa.address',
addOn: '$carspa.addOn',
service: '$carspa_service.name'
},
$else: {
$if: { $gt: ['$mechanical', null] },
$then: {
price: '$mechanical.price',
mode: '$mechanical.mode',
address: '$mechanical.address',
addOn: '$mechanical.addOn',
service: '$mechanical_service.name'
},
$else: {
price: '$quickhelp.price',
mode: '$quickhelp.mode',
address: '$quickhelp.address',
addOn: '$quickhelp.addOn',
service: '$quickhelp_service.name'
}
},
},
},
}
}
Try
{
$project: {
_id: 1,
date: 1,
slot: 1,
franchiseId: 1,
status: 1,
order: {
$cond: {
if: { $gt: ['$carspa', null] },
then: {
price: '$carspa.price',
mode: '$carspa.mode',
address: '$carspa.address',
addOn: '$carspa.addOn',
service: '$carspa_service.name'
},
else: {
if: { $gt: ['$mechanical', null] },
then: {
price: '$mechanical.price',
mode: '$mechanical.mode',
address: '$mechanical.address',
addOn: '$mechanical.addOn',
service: '$mechanical_service.name'
},
else: {
price: '$quickhelp.price',
mode: '$quickhelp.mode',
address: '$quickhelp.address',
addOn: '$quickhelp.addOn',
service: '$quickhelp_service.name'
}
},
},
},
}
}

Group nested fields with aggregation and return results with others fields mongo db

I need to change the structure of some field in my mongoDB document.
Here the sample:
[
{
_id: "ObjectId('997v2ha1cv9b0036fa648zx3')",
title: "Adidas Predator",
size: "8",
colors: [
{
hex: "005FFF",
name: "Blue"
},
{
hex: "FF003A",
name: "Red"
},
{
hex: "FFFE00",
name: "Yellow"
},
{
hex: "07FF00",
name: "Green"
},
],
extras: [
{
description: "laces",
type: "exterior"
},
{
description: "sole",
type: "interior"
},
{
description: "logo"
},
{
description: "stud",
type: "exterior"
}
],
media: {
images: [
{
url: "http://link.com",
type: "exterior"
},
{
url: "http://link3.com",
type: "interior"
},
{
url: "http://link2.com",
type: "interior"
},
{
url: "http://link4.com",
type: "exterior"
}
]
}
}
];
My goal is to group some fields:
colors need to be and array with just the colors,
extras need to be an array with 3 object each one for a "type" (interior, exterior, null)
the same for images that is inside media
Here what I expected:
{
_id: "ObjectId('997b5aa1cv9b0036fa648ab5')",
title: "Adidas Predator",
size: "8",
colors: ["Blue", "Red", "Yellow", "Green"],
extras: [
{type: exterior, description: ["laces", "stud"]},
{type: interior, description: ["sole"]},
{type: null, description: ["logo"]}
],
images: [
{type: exterior, url: ["http://link.com", "http://link4.com"]},
{type: interior, url: ["http://link2.com", "http://link3.com"]},
]
};
With my code I can achieve my goal but I don't understand how to show all the information together through the pipeline.
Here my code:
db.collection.aggregate([
{
$project: {
title: 1,
size: 1,
colors: "$colors.name",
extras: 1,
media: "$media.images"
},
},
{
$unwind: "$media"
},
{
$group: {
_id: {
type: "$media.type",
url: "$media.url",
},
},
},
{
$group: {
_id: "$_id.type",
url: {
$push: "$_id.url"
},
},
},
]);
The result is:
[
{
_id: "exterior",
url: [
"http://link.com",
"http://link4.com"
]
},
{
_id: "interior",
url: [
"http://link3.com",
"http://link2.com"
]
}
];
If I do the same thing with extras I get the same (correct) structure.
How can I show all the data together like in the expected structure?
Thanks in advice.
The strategy will be to maintain the require parent fields throughout the pipeline using $first to just grab the initial value, It ain't pretty but it works:
db.collection.aggregate([
{
"$addFields": {
colors: {
$map: {
input: "$colors",
as: "color",
in: "$$color.name"
}
}
}
},
{
$unwind: "$extras"
},
{
"$addFields": {
imageUrls: {
$map: {
input: {
$filter: {
input: "$media.images",
as: "image",
cond: {
$eq: [
"$$image.type",
"$extras.type"
]
}
}
},
as: "image",
in: "$$image.url"
}
}
}
},
{
$group: {
_id: {
_id: "$_id",
extraType: "$extras.type"
},
extraDescriptions: {
"$addToSet": "$extras.description"
},
imageUrls: {
"$first": "$imageUrls"
},
colors: {
$first: "$colors"
},
size: {
$first: "$size"
},
title: {
$first: "$title"
}
}
},
{
$group: {
_id: "$_id._id",
colors: {
$first: "$colors"
},
size: {
$first: "$size"
},
title: {
$first: "$title"
},
images: {
$push: {
type: {
"$ifNull": [
"$_id.extraType",
null
]
},
url: "$imageUrls"
}
},
extras: {
$push: {
type: {
"$ifNull": [
"$_id.extraType",
null
]
},
description: "$extraDescriptions"
}
}
}
}
])
Mongo Playground
You can try $function operator, to defines a custom aggregation function or expression in JavaScript.
$project to show required fields and get array of colors name
$function, write your JS logic if you needed you can sort this logic of group, it will return result with 2 fields (extras, images)
$project to show required fields and separate extras and images field from result
db.collection.aggregate([
{
$project: {
title: 1,
size: 1,
colors: "$colors.name",
result: {
$function: {
body: function(extras, images) {
function groupBy(objectArray, k, v) {
var results = [], res = objectArray.reduce((acc, obj) => {
if (!acc[obj[k]]) acc[obj[k]] = [];
acc[obj[k]].push(obj[v]);
return acc;
}, {});
for (var o in res) {
results.push({ [k]: o === 'undefined' ? null : o, [v]: res[o] })
}
return results;
}
return {
extras: groupBy(extras, 'type', 'description'),
images: groupBy(images, 'type', 'url')
}
},
args: ["$extras", "$media.images"],
lang: "js"
}
}
}
},
{
$project: {
title: 1,
size: 1,
colors: 1,
extras: "$result.extras",
images: "$result.images"
}
}
])
Playground
IMPORTANT:
Executing JavaScript inside an aggregation expression may decrease performance. Only use the $function operator if the provided pipeline operators cannot fulfill your application's needs.

how to pass param while getting billing data azure function

For getting the azure subscription billing data I am passing params it's not apply
const axios = require('axios');
let usage = [];
function getUsage(subscriptionId, accessToken) {
const url = `https://management.azure.com/subscriptions/${subscriptionId}/providers/Microsoft.Consumption/usageDetails?api-version=2019-01-01`
const options = {
headers: {
Authorization: `Bearer ${accessToken}`
}
}
// I am trying to pass data for filtering
const params ={
type: "Usage",
timeframe: "MonthToDate",
dataset: {
granularity: "None",
aggregation: {
totalCost: {
name: "PreTaxCost",
function: "Sum"
}
},
grouping: [
{
type: "Dimension",
name: "MeterCategory"
},
{
type: "Dimension",
name: "ResourceLocation"
},
{
type: "Dimension",
name: "ResourceGroup"
} ]
}
}
axios.get(url, options, params).then(response => {
console.log('response.data------', response.data);
}).catch(error => {
console.log(error);
});
}
//calling API here
await getUsage(
"subscriptionId",
"access-token"
);
Please change the format of your params from
params ={ type: "Usage", timeframe: "MonthToDate", dataset: { granularity: "None", aggregation: { totalCost: { name: "PreTaxCost", function: "Sum" } }, grouping: [ { type: "Dimension", name: "MeterCategory" }, { type: "Dimension", name: "ResourceLocation" }, { type: "Dimension", name: "ResourceGroup" } ] } }
to
params ={params:{ type: "Usage", timeframe: "MonthToDate", dataset: { granularity: "None", aggregation: { totalCost: { name: "PreTaxCost", function: "Sum" } }, grouping: [ { type: "Dimension", name: "MeterCategory" }, { type: "Dimension", name: "ResourceLocation" }, { type: "Dimension", name: "ResourceGroup" } ] } }}
Just add a "{params:" at the beginning and add a "}" at the end.
I think "params" should be a parameter of it. Please have a try.

How to find Mongoose data recursively?

I am newbie in MEANJS and i have a problem i.e, there are collection called employee and have multiple documents with their boss field. Now i want get all employees with their lower level.
For example:-
1) {_id:ObjectId('587dcd3edca5f235f862fdfd'), name:'John'} //he doesn't have boss
2) {_id:ObjectId('587dcd3edca5f235f86dddew'), name: 'Jimmy', 'boss': ObjectId('587dcd3edca5f235f862fdfd')} //john is boss
3) {_id:ObjectId('587dcd3edca5f235f863ew'), name: 'David', 'boss': ObjectId('587dcd3edca5f235f86dddew')} //john,Jimmy are bosses
4) {_id:ObjectId('587dcd3edca5f235f86qwa'), name: 'Dyan', 'boss': ObjectId('587dcd3edca5f235f86dddew')} //john,Jimmy,David are bosses
5) {_id:ObjectId('587dcd3edca5f235f8ew32'), name:'Jack', 'boss': ObjectId('587dcd3edca5f235f862fdfd')} //john is boss
6) {_id:ObjectId('587dcd3edca5f2wsw23rlot'), name: 'Loren', 'boss':ObjectId('587dcd3edca5f235f8ew32')} //john,Jack is boss
If we take
Jonh then output will ['Jimmy','Jack','David','Dyan','Loren']
Jack then output will ['Loren']
Here is my try code:-
getBosses(user._id)
function getBosses(id){
User.find({boss:id})
.exec(function(err,users){
if(err)
return console.log(err);
//How handle here 'users' array
//for something getBosses call recursively
})
}
As far as I understood you need to find all subordinates of that people. I think the best way to do it is using $graphLookup.
db.bosses.insertMany([
{ _id: "587dcd3edca5f235f862fdfd", name: "John" },
{
_id: "587dcd3edca5f235f86dddew",
name: "Jimmy",
boss: "587dcd3edca5f235f862fdfd",
},
{
_id: "587dcd3edca5f235f863ew",
name: "David",
boss: "587dcd3edca5f235f86dddew",
},
{
_id: "587dcd3edca5f235f86qwa",
name: "Dyan",
boss: "587dcd3edca5f235f86dddew",
},
{
_id: "587dcd3edca5f235f8ew32",
name: "Jack",
boss: "587dcd3edca5f235f862fdfd",
},
{
_id: "587dcd3edca5f2wsw23rlot",
name: "Loren",
boss: "587dcd3edca5f235f8ew32",
},
]);
db.bosses.aggregate([
{
$graphLookup: {
from: "bosses",
startWith: "$_id",
connectFromField: "_id",
connectToField: "boss",
as: "subordinates",
},
},
{
$project: {
_id: false,
name: true,
subordinates: {
$reduce: {
input: "$subordinates",
initialValue: "",
in: { $concat: ["$$value", ", ", "$$this.name"] },
},
},
},
},
{
$project: {
name: true,
subordinates: { $substrBytes: ["$subordinates", 2, -1] },
},
},
]);
The result of the last one is:
[
{ name: 'John', subordinates: 'David, Dyan, Loren, Jack, Jimmy' },
{ name: 'Jimmy', subordinates: 'David, Dyan' },
{ name: 'David', subordinates: '' },
{ name: 'Dyan', subordinates: '' },
{ name: 'Jack', subordinates: 'Loren' },
{ name: 'Loren', subordinates: '' }
]
The most important thing is $graphLookup stage of the aggregate pipeline. Last two $project stages is just response formatting - return only name and subordinates as string field with comma separated names.
To get data for a specific person you can use $match stage before $graphLookup like that:
db.bosses.aggregate([
{ $match: { name: "John" } },
{
$graphLookup: ...

Resources