Aggregation with two different conditions - node.js

I'v one issue to write aggregation query. I'v 3 doc.I want filter 2 doc with some condition.In which 2 doc having same data with 1 changed value.
I have 3 entries of data in database. English is default lang for my data. In which 2 entries(one with default lang and other with 'Hindi' lang) having same data with some unique id with one diff language parameter and 3rd entry having only data in default lang another uniqueid.
Now I want data in following condition:
1) I have have sent English as a lang parameter Then query will find out the data with english lang
2) If I send Hindi lang as a parameter then from all entries I get 2 entries one entries with hindi lang and one entry with english(default lang) as a result
Query :
certification.find({$and: [{certificate_id: certificate_id}, {$or : [{lang : lang }, {lang : 'English'}]}]});
lang, certificate_id : parameter passed by user
Please help me to write query with mongodb.

Related

How to query field exist some document in firebase

I using firebase, nodejs and i have a question how to query field exist some document.
Example :
Collections : users => document : A with field {
....
is_correct: true
}
document : B with field {
.....
}
In my above example , i have two document in collection users. On document A i have field is_correct: true and on document B field is_correct not exist.
My collection users about 15.000 document and it have 50 document contain field is_correct: true
When i write code look like :
await firestore.collection('users').where('is_correct', '==', true).get();
it can get correct me 50 document. But i don't understand it can using index on field is_correct or not ? And it can query best performance ? I understand firebase can't get document if field undefined. It impart in case ? Please help ? Thanks you
For a simple query like
firestore.collection('users').where('is_correct', '==', true)
you don't need to configure any index, Firestore does it automatically.
As you mentioned, only documents where the given field exists can match the query.
And this is the case also for Not-equal (!=) and not-in queries: they exclude documents where the given field does not exist, as explained in the documentation.
Also, note that a field exists when it's set to any value, including an empty string (""), null, and NaN (not a number).
So, in conclusion, if you want to query for documents where is_correct is not true, you need to create this field in these documents with a value different than true.

How to query fields with multiple values in Azure Cognitive Search

Working on Azure Cognitive Search with backend as MS SQL table, have some scenarios where need help to define a query.
Sample table structure and data :
Scenarios 1 : Need to define a query which will return data based on category.
I have tied query using search.ismatch but its uses prefix search and matches other categories as well with similar kind of values i.e. "Embedded" and "Embedded Vision"
$filter=Region eq 'AA' and search.ismatch('Embedded*','Category')
https://{AZ_RESOURCE_NAME}.search.windows.net/indexes/{INDEX_NAME}/docs?api-version=2020-06-30-Preview&$count=true&$filter=Region eq 'AA' and search.ismatch('Embedded*','Category')
And it will response with below result, where it include "Embedded" and "Embedded Vision" both categories.
But my expectation is to fetch data only if it match "Embedded" category, as highlighted below
Scenario 2: For the above Scenario 1, Need little enhancement to find records with multiple category
For example if I pass multiple categories (i.e. "Embedded" , "Automation") need below highlighted output
you'll need to use a different analyzer which will break the tokens on every ';' just for the category field rather than 'whitespaces'.
You should first ensure your Category data is populated as a Collection(Edm.String) in the index. See Supported Data Types in the official documentation. Each of your semicolon-separated values should be separate values in the collection, in a property called Category (or similar).
You can then filter by string values in the collection. See rules for filtering string collections. Assuming that your index contains a string collection field called Category, you can filter by categories containing Embedded like this:
Category/any(c: c eq 'Embedded')
You can filter by multiple values like this:
Category/any(c: search.in(c, 'Embedded, Automation'))
Start with clean data in your index using proper types for the data you have. This allows you to implement proper facets and you can utilize the syntax made specifically for this. Trying to work around this with wildcards is a hack that should be avoided.
To solve above mention problem used a below SQL function which will convert category to a json string array supported by Collection(Edm.String) data type in Azure Search.
Sql Function
CREATE FUNCTION dbo.GetCategoryAsArray
(
#ID VARCHAR(20)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE #result NVARCHAR(MAX) = ''
SET #result = REPLACE(
STUFF(
(SELECT
','''+ TRIM(Value) + ''''
FROM dbo.TABLEA p
CROSS APPLY STRING_SPLIT (Category, ';')
WHERE p.ID = #ID
FOR XML PATH('')
),1,1,''),'&','&')
RETURN '[' + #result + ']'
END
GO
View to use function and return desired data
CREATE View dbo.TABLEA_VIEW AS
select
id
,dbo. GetCategoryAsArray(id) as CategoryArr
,type
,region
,Category
from dbo.TABLEA
Defined a new Azure Search Index using above SQL View as data source and during Index column mapping defined CategoryArr column as Collection(Edm.String) data type
Query to use to achieve expected output from Azure Search
$filter=Region eq 'AA' and CategoryArr/any(c: search.in(c, 'Embedded, Automation'))

Solr - Order results by proximity to number

I am using solr to get results based on the search text entered by the user.
I want to order the results based on proximity to the calories field of the document as shown below.
I have used Range (calories:[0 TO 300]) however that doesnt fulfill my needs.
{
"food_group":"Proteins",
"carbs":"6.295",
"protein":"13.729",
"fat":"2.551",
"calories":103.0
}
For example if user enters 100 as calories i want to show the document with 101 before the document with 97 and so on...(There is no sorting logic in this)
You can use abs(sub(user_calories, calories)) function as sort
Example for user input 100 :
q = "calories:[0 TO 300]",
sort = "abs(sub(100,calories)) asc"
Example Url :
http://127.0.0.1:8983/solr/test/select?q=calories%3A%5B0+TO+300%5D&sort=abs(sub(100%2Ccalories))+asc

Suggestion on MongoDB indexing

I have the following MongoDB schema
Item
id
title : string
description : string
num_views : number
num_likes : number
num_unlikes : number
val_trending : number
val_rating : number
timestamp : number
val_trending is calculated based on the value of num_views, num_likes, num_dislikes, timestamp
val_rating is calculated based on the value of num_likes, num_dislikes, timestamp
I have to query Items based on num_views, val_trending or val_rating
i.e.
Item.find ().sort ({ val_trending : -1 }).limit (20)
Item.find ().sort ({ num_views : -1 }).limit (20)
num_views, num_likes and num_unlikes are updated when a user view, like or unlike an Item accordingly
val_trending is updated when a user view, like or unlike an Item
val_rating is updated when a user like or unlike an Item
That’s a lot of updating and that's what got me worried. I thought of indexing num_views, val_trending and val_rating for faster results. But this will also show down updates.
I will be querying Items several times every second and updates will occur more frequently as user might also like or unlike an Item after viewing an Item.
So, my question is: what sort of implementation (indexing) should I do in order to get the best performance?
Note: The DB will hold approximately 10K Items at start, and it will increase daily.

Ordered search results in Mongodb?

Am doing a simple tag search using mongodb with data being stored in a list.
Entity A {_id:...,tags : ['a','b','f']}
Entity B {_id:...,tags : ['g','a','v']}
Entity C {_id:...,tags : ['a','c','e']}
Entity D {_id:...,tags : ['c','s','e']}
Entity E {_id:...,tags : ['a','c','s']}
Search String : 'a c s g'
Query being
db.collecction.find({tags:{$in:['a','c','s','g']}})
Expected Response would be Entity with most matching tags at top and rest thereafter.
Reponse:
1.Entity E - 3 tags matched
2.Entity D/C/B - 2 tags matched
3.Entity C/B/D - 2 tags matched
4.Entity B/D/C - 2 tags matched
5.Entity A - 1 tag matched
What would be the best way to achieve the same??
You have to implement the ranking/sorting on the application level.
There is nothing in MongoDB that would help you here.
You may look into
http://www.mongodb.org/display/DOCS/Aggregation
but I don't see how this could be used for your particular usecase.

Resources