I want to search values having special characters such as "(" in a document.
I 'm using following criteria in mongoose and fetch the names that matches like "abc (pvt) ltd".
var criteria = {};
criteria.name = new RegExp(searchPrameters.name, "i");
I found the solution with replacing of the parenthesis using string.replace() function.
searchPrameters.name = searchPrameters.name.replace('(','\\(');
searchPrameters.name = searchPrameters.name.replace(')','\\)');
Related
I have a model I want to perform a text search on with mongoose.
let models = await MyModel.find({$text: {$search: '"go"'}});
However, I have noticed when I search for strings with two or less characters no results are returned.
How can I search for strings with two or less characters?
You can try using the aggregate query and RegEx, as follows:
let searchText = "go";
searchText = new RegExp(searchText, "i");
let models = await MyModel.aggregate([ {$match: {"text": { $regex: searchText}}} ])
here text is the field on which search will be applied. You can add $or conditions to search in more than one field.
we are using azure search API but are getting no results for special characters that contain no alphanumeric characters.
We was having trouble returned any matching results for Japanese language and any special characters at all until we wrapped the string in quotes (") see the examples below (we are escaping the special characters also.
strings that did not work
var searchTerm = "嘘つきな唇で";
var searchTerm = "test#123";
var searchTerm = "?sd-^&*d$£(";
After wrapping in quotes i.e.
searchTerm = "\"" + searchTerm + "\"*"
all the above searches returned the expected matches but now we have an issue of no matches with strings with only special characters in i.e.
var searchTerm = "####";
var searchTerm = "&#*(%$";
new SearchParameters
{
SearchFields = new List<string> {"name", "publicId"},
Top = 50,
SearchMode = SearchMode.Any,
QueryType = QueryType.Simple,
Filter = $"status eq 1"
}
Any help on this would be greatly appreciated
Kind regards
Without escaping or using another analyzer (rather than StandardAnalyzer which is the default) I don't think you'll be able to retrieve the results as some of the samples you've provided are reserved / special chars:
Please check:
https://learn.microsoft.com/en-us/azure/search/query-lucene-syntax#escaping-special-characters
EDIT: please read about analyzers in here: https://learn.microsoft.com/en-us/azure/search/search-analyzers
I have list of codes which are separated by comma, the code contains min of 3 and max of 6 characters including numbers. I have written regex for this. How can I expand my regex to work with 1 or more codes as array or list?
Here is my regex for codes
const checkCodes = new RegExp('^[A-Z]+[A-Z0-9]{2,5}$');
The above regex works well with single codes
codes - "BCD"
but not for below line, which I am trying to achieve
codes - ["BCD", "VOC123",....1 or more codes]
const checkCodes = new RegExp('^[A-Z]+[A-Z0-9]{2,5}$');
let input = ["CODE1", "NOT.A.CODE", "CODE42"];
console.log(input.filter(str => checkCodes.test(str)))
const checkCodesMulti = /^([A-Z]+[A-Z0-9]{2,5})(?:\s*,\s*([A-Z]+[A-Z0-9]{2,5}))*$/
let input = "BCD, VOC123, ETC";
let array_of_codes = checkCodesMulti.exec(input).splice(1)
I want to have valid query like that countries=fr,be
I tried this:
countries: Joi.string().valid(['fr','be'])
But this allows only to have countries=fr or countries=be, but not multiple comma separated values. How can I achieve this?
You can use regex like so:
var list = ['fr', 'be']; // your counrty codes
var joinedList = '(' + list.join('|') + ')'; // prepare RegExp cases
var regex = new RegExp('^' + joinedList + '(,' + joinedList + ')*$') // the thing in itself
Then:
countries: Joi.string().regex(regex).required()
It would match any country code in list, alone or in a comma-separated list. I added required() as I believe valid() implicitly makes the field required as long as you don't allow undefined value.
I'm trying to do a search on items that contain a certain substring, but I'm getting no results back (even though I know the data is right for the query, including case):
collection.find({name: "/.*" + keyword + ".*/"}).toArray(function(err, items)
Should that not match everything that contains the keyword? It just returns an empty object.
I'm just using the regular MongoDB driver in an ExpressJS app.
You need to build a regular expression first should try something like this:
var regex = RegExp("/.*" + keyword + ".*/")
Then pass in the variable to the query. I generally find it easier to do the query as a variable and pass that in:
var query = { FieldToSearch: new RegExp('^' + keyword) };
collection.find(query).toArray(...)
I've included the regex as a left rooted regex to take advantage of indexes (always recommended if possible for performance). For more take a look here:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions
I did it like this
keyword = "text_to_search";
collection.find({name: {$Regex: keyword, $options:$i }})
I used i $i make the query case insensitive but u can use other options too
try this:
var keyword = req.params.keywords;
var regex = RegExp(".*" + keyword + ".*");
Note.find({noteBody: regex, userID: userID})
I got the keywords from the request parameters and I want to search from the noteBody with these keywords, now the keywords is a variable. If you want to put a variable in the database find, the format must be var regex = RegExp("." + keyword + "."). Hope this helps. Thanks