Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

AEM Full Text Search for a keyword with spaces

Avatar

Level 2

Hi,

 

I would like to carry out search in AEM where in I would like to search for the exact word such as "Insurance Broker".

The search should provide only that reference which has the exact word match "Insurance Broker". 

If we use AEM full text search and carry out the search using above keyword, it returns the pages in result even if the word "Insurance" and "Broker" are present at two different locations within page. Appreciate any assistance. 

 

Sample Query (ignore the OR condition as I need to search for multiple keywords)

Thanks,

Sumit Kulkarni

type=cq:Page
path=/content/mysite/en
group.p.or=true
group.1_group.fulltext=Insurance Broker
group.2_group.fulltext=Insurance Claim
orderby.sort=desc
p.limit=-1

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi @sumeet_a_kulkar,

 

Check this out. Please note that the search string is in double quotes inside the single quotes. I haven't tried this myself but hope it helps.

 

SELECT * FROM [nt:base] AS s WHERE CONTAINS(s.*, '"Insurance Broker"')

 

References:

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/sql-2-query-for-full-text-...

 

https://stackoverflow.com/questions/48077571/why-does-contains-find-inequal-text-strings-in-jcr-sql2

 

Thanks, 

Ram

View solution in original post

7 Replies

Avatar

Community Advisor

Hi @sumeet_a_kulkar ,

                                   Please check the below example and try same in your project

Java:
String pageTitle, pagePath
Map params = [:]
params["path"] = path
params["type"] = "cq:Page"
params["fulltext"] = searchKey
params["orderby"] = "@jcr:score"
now use Query interface to create a query object that will in turn use builder object of Query Builder interface to build the query.
Query query = builder.createQuery(PredicateGroup.create(params), session)
searchResult will contain the result of the JCR query. A hit represents a single search result which is adapted to a page so as to fetch the page title and page url for display.


SearchResult searchResult = query.result
Long totalHits = searchResult.getTotalMatches()
for (Hit hit : searchResult.hits) {

Page page = hit.resource.adaptTo(Page)
pageTitle = page.title
pagePath = page.path
searchHit = new JSONObject()
searchHit.put("totalHits", totalHits)
searchHit.put("path", pagePath)

if (!pageTitle) {
pageTitle = pagePath.substring(pagePath.lastIndexOf(‘/’) + 1, pagePath.length())

}
searchHit.put("title", pageTitle)

resultArray.put(searchHit)
}
resultObject.put("data", resultArray)
return resultObject

Avatar

Level 2

Thank you for the reply. But if the search key is not a single word but a combination of words then the page appears in result if the page contains all these keywords at random locations. It does not consider the entire searchkey as a single word. (Insurance Broker as a single word)

params["fulltext"] = searchKey

 I am trying sql2 query as well but this also behaves as Xpath based on my observation so far.

Avatar

Community Advisor

You can add more words coma separated in one line instead of sql2.

Avatar

Community Advisor

Hi @sumeet_a_kulkar, could you please try to put entire search phrase inside double quote. So your sample query will look like that:

type=cq:Page
path=/content/mysite/en
group.p.or=true
group.1_group.fulltext="Insurance Broker"
group.2_group.fulltext="Insurance Claim"
orderby.sort=desc
p.limit=-1 

 

Avatar

Correct answer by
Level 6

Hi @sumeet_a_kulkar,

 

Check this out. Please note that the search string is in double quotes inside the single quotes. I haven't tried this myself but hope it helps.

 

SELECT * FROM [nt:base] AS s WHERE CONTAINS(s.*, '"Insurance Broker"')

 

References:

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/sql-2-query-for-full-text-...

 

https://stackoverflow.com/questions/48077571/why-does-contains-find-inequal-text-strings-in-jcr-sql2

 

Thanks, 

Ram

Avatar

Community Advisor

Your X path query is missing double quotes for search key please modify like this and try search 

 

type=cq:Page
path=/content/mysite/en
group.p.or=true
group.1_group.fulltext="Insurance Broker"
group.2_group.fulltext="Insurance Claim"
orderby.sort=desc
p.limit=-1