Searching text by descending keyword match - hibernate-search

I have a keyword that I'm going to search, for example APPLE, but the result must be returned in descending matching of the keyword, for instance first of all it will return all the rows that matches APPLE, then APPL, APP, AP, A.

Related

Is there a function to submit a wildcard search request via firebase firestore?

For example, querying the collection foo with the query string a* should return any item in the collection that starts off with the letter a. I have a collection of U.S States in a firebase firestore collection and it connects to an iOS app written in Swift via the firestore API. Inside of a SearchResultsViewController users should be able to search for a state via a substring of that state. For example when a user types a into the searchText field, the results returned to them should be [Alabama, Alaska, Arizona, Arkansas] because those states start off with the letter a. Each document has an id which is an Auto-ID given by firebase and a name which is the name field which contains the name of the state. The query will be run against the name field of the document.
Presuming something like a stateName property in the docs (not the docs' ids), a subrange of a lexical ordering can be returned with a pair of whereField inequalities...
collectionRef.whereField("stateName", isGreaterThanOrEqualTo: "A")
collectionRef.whereField("stateName", isLessThan: "B")
Note this isn't an arbitrary wildcard. It applies only to the starts of the strings and answers whatever falls into a range alphabetically.

MongoDB multiple type of index on same field

Can I have multiple type of index on same field? Will it affect performance?
Example :
db.users.createIndex({"username":"text"})
db.users.createIndex({"username":1})
Yes, you can have different types of indexes on single field. You can create indexes of type e.g text, 2dsphere, hash
You can not create same index with sparse and unique options.
Every write operation is going to update a relevant index entry of all possible types in this case
The two index options are very different.
When you create a regular index on a string field it indexes the entire value in the string. Mostly useful for single word strings (like a username for logins) where you can match exactly.
A text index on the other hard will tokenize and stem the content of the field. So it will break the string into individual words or tokens, and will further reduce them to their stems so that variants of the same word will match ("talk" matching "talks", "talked" and "talking" for example, as "talk" is a stem of all three). Mostly useful for true text (sentences, paragraphs, etc).
Text Search
Text search supports the search of string content in documents of a collection. MongoDB provides the $text operator to perform text search in queries and in aggregation pipelines.
The text search process:
tokenizes and stems the search term(s) during both the index creation and the text command execution.
assigns a score to each document that contains the search term in the indexed fields. The score determines the relevance of a
document to a given search query.
The $text operator can search for words and phrases. The query matches on the complete stemmed words. For example, if a document field contains the word blueberry, a search on the term blue will not match the document. However, a search on either blueberry or blueberries will match.
$regex searches can be used with regular indexes on string fields, to provide some pattern matching and wildcard search. Not a terribly effective user of indexes but it will use indexes where it can:
If an index exists for the field, then MongoDB matches the regular expression against the values in the index, which can be faster than a collection scan. Further optimization can occur if the regular expression is a “prefix expression”, which means that all potential matches start with the same string. This allows MongoDB to construct a “range” from that prefix and only match against those values from the index that fall within that range.
http://docs.mongodb.org/manual/core/index-text/
http://docs.mongodb.org/manual/reference/operator/query/regex/

meteor, find whether user exists in mongodb document array or not?

This is my meteor code to search whether user exist in the array or not
var u_exist=Polls_Coll.findOne( {option1:{$elemMatch:{ids:"xxx"}}} );
My question is, How to know whether the statement returning something or not(user exist or not)
$elemMatch will return only where one of the conditions supplied actually finds a match in the array. So if you don't get a document back then there was no match.
Also findOne is a single document. Modifiers such as .count() will not work on that. If you have more documents to be expected use find intstead. Also findOne not not make much sense without applying a unique identifier such as _id in the query. Without that you are almost certainly not getting what you want.
While useful for your purpose, findOne is not a good match with the $elemMatch operator. The reasoning is you can possibly get multiple results of the same document having the same set of array elements that matched the condition that you gave.
Buyer beware.

Mongodb, how do I aggregate match / group some documents but only if one or other conditions are satisfied?

I have two properties b and u that are arrays.
I want my query to return me all documents that have at least 1 element in b that satisfies condition 1 or at least 1 element in u that satisfied condition 2.
My problem is that if condition 1 is satisfied, it will return me the array b with all elements that satisfy it, AND ALL the elements from u.
Same with condition B.
I want: If there is an element in b that satisfied condition 1, return me that array with the elements that satisfy it. If not, empty array or exclude b. Same with condition 2 and u.
If no elements satisfy the conditions, exclude the document.
Unfortunately, you can't do that with MongoDB in a single step on the database server. You'll need to do it client side.
While you can project (documentation) your results to only include/exclude some fields (or the first matching result in an array for example as shown here), you can't conditionally do it based on a search with multiple arrays (and the projection operator returns only the first match, not just the results that match).
You might need to consider a different document/collection structure to meet your requirements. MongoDB doesn't have sub-document level filtering/searching yet.

Mongo - dot notation works like $or? Can't provide multiple parameters?

I tried to update an existing document with two dot notation parameters, my query:
{ _id: "4eda5...", comments._id: "4eda6...", comments.author: "john" }
my update was:
{ "comments.$.deleted": true }
However, weirdly enough, when I passed a non-existent combination of comment id+author, it just updated the first matching comment by that author.
Any ideas why that's happening?
EDIT: C# Code sample
var query = Query.And(Query.EQ("_id", itemId), Query.EQ("cmts._id", commentId));
if (!string.IsNullOrEmpty(author))
query = Query.And(query, Query.EQ("cmts.Author", author));
var update = Update.Set("cmts.$.deleted", true);
var result = myCol.Update(query, update, UpdateFlags.None, SafeMode.True);
You want $elemMatch if you want the _id and author to be in the same comment. Really, your query doesn't make much sense including the author as the id should be as unique as you can get, no?
It is based on the first matching array element which replaces the "$" in for the update.
This is working by design. It is similar to an or since it can find a document which both has the _id and an author that match in any of the array elements.
The query is not working the way you are expecting it to. Basically, when using the $ positional notation you need to make sure that your query only has one clause that queries an array, otherwise it is ambiguous which of the two array comparisons the $ should refer to.
In your case, you are asking for a document where:
The _id is equal to some value
The comments array contains some document where the _id is equal to some value
The comments array contains some document where the author is equal to some value
Nothing in your query says that 2. and 3. need to be satisfied by the same document.
So even though you are using a non-existent combination of comment._id and comment.author, your comment array does have at least one entry where the _id is equal to your search value and some other entry (just not the same one) where the author is equal to your search value.
Since the author was the last one checked, that's what set the value of the $, and that's why that array element got updated.