function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bohemianguy100bohemianguy100 

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.
Best Answer chosen by bohemianguy100
Venkat PolisettiVenkat Polisetti
1. Differences between embedding query string in quotes vs. braces
  • 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

Vinita_SFDCVinita_SFDC
Hello,

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?
Vinita_SFDCVinita_SFDC
Hello,

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?
bohemianguy100bohemianguy100
I'm just defining a public string property for an inputText component:

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.

sfdcfoxsfdcfox
The system indexes each word in a text field individually. So, given "The Quick Brown Fox Jumps Over The Lazy Dog", you can search for "quick", "brown", "fox", "jumps", "over", "lazy" or "dog" ("the" will probably be excluded from the index because it would return too many irrelevant resuls). It is true that searching AB* will return "absolute" but not "baby", because the "ab" does not start at the beginning of the word.
bohemianguy100bohemianguy100
okay, understood.  What is the difference of using the curly braces...i.e. {AB*} and AB*
bohemianguy100bohemianguy100
Or, is there a way to run a search that begins with AB* 
The equivalent of 'LIKE 'AB%'

Is that posible using SOSL?
Venkat PolisettiVenkat Polisetti
1. Differences between embedding query string in quotes vs. braces
  • 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
This was selected as the best answer
shalini sharma 24shalini sharma 24
Hello,
You can use the below SOSL query:
[FIND 'AB' IN ALL FIELDS RETURNING Product2 (Id, Name where name Like 'AB%')];