Search collection fields and populated fields at the same time - node.js

I have 2 collections as the following:
Products
- name (String)
- price (number)
- category (ObjectID refs to Category collection)
Category
- name
And we have search query for example "some query"
I'm trying to get the records where product.name or product.category.name partially includes the search query

If you are using Mongoose populate, that uses DBRef, which is not easily used on the server side.
If the Products document contains only the _id of the corresponding Category document, you would need to use aggregation to read all Products documents, and for each one load all of the corresponding Category documents, then filter using a regular expression.
That is not scalable.
If you denormalize a little bit by having the Products document contain both the _id and name of the Category (assuming Category actually has other data), you could use a text index on the Products collection, or if you are using Atlas, full text search in the Products collection.

Related

Solr default search field for multiple fields which has different analyzers

I have a document which has title, stockCode, category fields.
I have different field types (and analysis chains) for each. For instance title has EdgeNGram 2 to 20, category has EdgeNGram 3 to 10 with different range and stockCode just has lowercase filter.
So that, I don't want to search from documents with keyword "sample" with building the query like title:sample OR stockCode:sample OR category:sample.
I'd like to search with just "q=sample".
I copied my fields to text but It does not work. Because all fields analyzed as same. But I don't want to index stockCode as EdgeNGram or any other filters. I'd like to index my fields as I configured and I'd like to search a keyword over them base on my indexes.
I've been researching about that for three days, and Solr has a little bit poor documentation.
You can use the edismax handler, as this will allow you to give a list of fields to query and supply the query by itself. You can also give separate weights to each field for scoring them differently.
defType=edismax&q=sample&qf=title^10 stockCode category
.. will search for sample in each of the three fields, giving a 10x boost to any hits in the title field.
You can find the documentation about the edismax query parser under Searching in the reference guide.

mongoDB Search without joins

I have two collections: Profiles and Employees.
Employees consists of firstName, lastName etc.
Profiles-Collection, amongst other data, has a bunch of key value pairs that describe the profession or level of experience, e.g."software-engineer": true, "javascript": 3
Since you can't have joins in mongoDB I need to search each collection individually and then "join" that result. That leaves me with 2 options:
1) Have two separate search bars on the frontend so that I know which search query belongs to which collection
2) Have a single search bar and search both collections with the same query
Option two is implemented in a way that a search on a single collection either returns the desired data when the search query has a match or returns ALL data when the search query finds no match. That means searching after "John Doe" gives us john and searching after "angular" gives us all employees that work with angular. But it also means searching after "john angular" gives us all employees that work with angular OR are called john.
What I actually want is a AND search (like in option 1) but with a single search bar. Is there a way to implement this in MongoDB or is this only possible in a relational database?

Azure search filtering performance over Edm.Int32 and Collection(Edm.String)

I'm using Azure Search on my e-commerce site, and now want to implement filtering.
I've faced issue with performance. I have index with products. Each product belong to category. Each category can have nested subcategories.
My business purpose is when customer is on category page i need to show products even from subcategories, so i have doubts about how to store this relation(products to categories) in azure products index.
I'm considering two possibilities:
I can store only products category id in field with type Edm.Int32. Then when customer goes to this category i query to my sql server to get all subcategory ids and then construct my query to index like this
categoryId eq 34 or categoryId eq 36 or categoryId eq 37 ...
Other way is to create field with type Collection(Edm.String) and to store products category id and nested categories ids in this field and then my query to index would look like this
categoryIds/any(c: c eq '35')
So which way will be faster?
Option #2 is likely faster since the number of documents in the index will be far fewer, but the only way to be sure is to run some experiments with your data and queries. Overall query performance is going to depend on other factors like whether you're doing full-text search, faceting, geo-spatial, etc.

Retrieving just the joined entities from Core Data

I have a movie showtimes entity which has a one-to-one relationship to a movie entity. The inverse (movie -> movie showtime) relationship is a one-to-many relationship. If a movie is deleted, the associated movie showtimes will also be deleted, but if a movie showtime is deleted the associated movie will stay. (Not sure how much of that is relevant but wanted to clarify the situation as much as I could)
Now, is there a way to query Core Data to get only the unique movies for which I have showtimes?
Is it possible to select from the movie showtimes and somehow restrict the results to just the associated unique movies? Or would selecting from the movie entity bring back only the movies with a matching row in the movie showtime entity?
Sure. Write a fetch request on Movie with no restrictions, and you'll get all Movie instances.
Taking your questions in turn:
Now, is there a way to query Core Data to get only the unique movies for which I have showtimes?
You could use a predicate to select movies where the count of showtimes is greater than zero:
"showtimes.#count > 0"
Is it possible to select from the movie showtimes and somehow restrict the results to just the associated unique movies?
If you have (courtesy of your first query) an array (say scheduledMovies) of movies which have showtimes, then you can fetch the associated showtimes using a predicate like this:
"movie IN %#", scheduledMovies
Or would selecting from the movie entity bring back only the movies with a matching row in the movie showtime entity?
If you fetch movies, then you will get ALL movies, unless you specify a predicate as per your first question. But if you fetch showtimes, you can get an array of the associated movies using key value coding with the key:
"#distinctUnionOfObjects.movie"
The resulting array will not have any movies with no showtimes. (The first part of this key removes any duplicates, since several showtimes might have the same movie).

Predicate subquery for products associated by tag

I have a datamodel in core data as such:
Store <--->> Product <<--->> Tag
I'm looking to create a query that matches products based on their associated tags. A typical use scenario would be you choose to search storeA and output storeB. By selecting storeA a list of storeA products is presented. Let's say you pick productA from the list which brings you to a second viewcontroller. That second viewcontroller lists all relating productB's (because earlier you set storeB as output). I'm following this: predicate subquery to return items by matching tags but want the added store filter of products. Is subquery the way to go? How would I include a filter by selected Store?

Resources