AEM Full Text Search for a keyword with spaces | Adobe Higher Education
Skip to main content
sumeet_a_kulkar
Level 2
October 13, 2021
해결됨

AEM Full Text Search for a keyword with spaces

  • October 13, 2021
  • 4 답변들
  • 16477 조회

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

 

 

이 주제는 답변이 닫혔습니다.
최고의 답변: rampai

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-search/td-p/252

 

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

 

Thanks, 

Ram

4 답변

Sanjay_Bangar
Community Advisor
Community Advisor
October 13, 2021

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
sumeet_a_kulkar
Level 2
October 13, 2021

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.

Sanjay_Bangar
Community Advisor
Community Advisor
October 13, 2021

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

lukasz-m
Community Advisor
Community Advisor
October 13, 2021

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 

 

rampai
Community Advisor
rampaiCommunity Advisor답변
Community Advisor
October 15, 2021

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-search/td-p/252

 

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

 

Thanks, 

Ram

Anny0505
Community Advisor
Community Advisor
October 16, 2021

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
sumeet_a_kulkar
Level 2
October 20, 2021

This one is correct Xpath query.

- Thanks.