You need to sign in to do that
Don't have an account?
bohemianguy100
sosl query wildcard not returning correct results
I have a function that uses a sosl query:
private List<Product2> runSoslToExecute() {
List<List<Product2>> searchResults = [FIND :query IN ALL FIELDS RETURNING Product2 (Id, Name)];
List<Product2> results = new List<Product2>();
for(Product2 p : searchResults[0]) {
results.add(p);
}
return results;
}
If I search for "AB*" then I also get results that include "1AB...". I thought the "*" wildcard only searches in the middle and end of the search and not at the beginning? Is there a way to run the sosl search so it only searches "AB" at the beginning?
Thanks for any help.
private List<Product2> runSoslToExecute() {
List<List<Product2>> searchResults = [FIND :query IN ALL FIELDS RETURNING Product2 (Id, Name)];
List<Product2> results = new List<Product2>();
for(Product2 p : searchResults[0]) {
results.add(p);
}
return results;
}
If I search for "AB*" then I also get results that include "1AB...". I thought the "*" wildcard only searches in the middle and end of the search and not at the beginning? Is there a way to run the sosl search so it only searches "AB" at the beginning?
Thanks for any help.
-
For APEX, a literal string must be enclosed in single quotes.
-
For force.com API (SOAP API), it should be demarcated with braces.
2. I do not think, you can search for field values that begin with a specific set of characters in SOSL, you can certainly do that in SOQL.-
if you try to FIND 'AB*', it would return all values where 'AB' followed by 0 or more characters. Do not think it would return values only starting with 'AB'. If you want it to return values starting with 'AB', switch to SOQL.
Hope this helps.Venkat
All Answers
Please share how are you defing "query" in the query. If you search like:
[FIND {AB*} IN ALL FIELDS RETURNING Product2 (Id, Name)];
What do you get?
Please share how are you defing "query" in the query. If you search like:
[FIND {AB*} IN ALL FIELDS RETURNING Product2 (Id, Name)];
What do you get?
public String query {get; set;}
I tried hardcoding in just {AB*} into the SOSL query to test and I didn't get any results back.
The equivalent of 'LIKE 'AB%'
Is that posible using SOSL?
-
For APEX, a literal string must be enclosed in single quotes.
-
For force.com API (SOAP API), it should be demarcated with braces.
2. I do not think, you can search for field values that begin with a specific set of characters in SOSL, you can certainly do that in SOQL.-
if you try to FIND 'AB*', it would return all values where 'AB' followed by 0 or more characters. Do not think it would return values only starting with 'AB'. If you want it to return values starting with 'AB', switch to SOQL.
Hope this helps.Venkat
You can use the below SOSL query:
[FIND 'AB' IN ALL FIELDS RETURNING Product2 (Id, Name where name Like 'AB%')];