'How to differentiate bewteen begins with and contains in mongo Atlas using regex
I want to boost my score for begins with the case. For example _ I have below two records.
HCL2% CRM
DETHRANAOL HCL CRM
I need a score of HCL2% CRM more than DETHRANAOL HCL CRM when searching with HCL for expected results.
I am trying regex ^HCL(.*) so that it matches with HCL2% and then I will boost its score but this regex is not working in the mongo atlas query.
Can someone help?
Below is the query I am using.
db.compound.aggregate([
{
$search: {
"index": "compoundSearchIndex",
"compound": {
"should": [
{
"autocomplete": {
"query": [
"HCL"
],
"path": "name.en",
"fuzzy": {
"maxEdits": 2,
"prefixLength": 1,
"maxExpansions": 10
}
}
},
{
"regex": {
"query": [
"HCL"
],
"path": "name.en",
"allowAnalyzedField": true,
"score": {
"boost": {
"value": 30
}
}
}
},
{
"regex": {
"query": [
"HCL(.*)"
],
"path": "name.en",
"allowAnalyzedField": true,
"score": {
"boost": {
"value": 10
}
}
}
},
{
"regex": {
"query": [
"(.*)HCL(.*)"
],
"path": "name.en",
"allowAnalyzedField": true
}
}
],
"must": [
{
"equals": {
"value": true,
"path": "active"
}
}
],
"minimumShouldMatch": 1
}
}
},
{
$project: {
"name": 1,
"score": {
$meta: "searchScore"
}
}
}
]);
Solution 1:[1]
Using lucene.keyword analyzer instead of lucene.standard solved the issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Verma |
