1
votes

I want to search for a exact combination of words in all nodes in the aem using query builder. Trying to debug the query http://localhost:4502/libs/cq/search/content/querydebug.html it returns me results that doesn't match my query. For example if want to search for 'foo bar' in all nodes and I need to receive all nodes that contain 'Foo Bar', 'foo Bar', 'Foo bar', 'FOO BAR' but not only 'foo' and only 'bar' and not 'foo-bar'. Query in service is done by using QueryBuilder.

6
I don't know AEM, but if it uses Lucene query syntax, surround it with quotes: "foo bar". You will still find 'foo-bar', but with standard analysis, the indexed form of that is indistinguishable from 'foo bar', so that would take changing your analysis and reindexing.femtoRgon

6 Answers

1
votes

QueryBuilder is useful when you try to perform a query similar to SQL where you search against a property and its value. The full text search capabilities of the query debug interface is very limited as you have experienced.

However, remember that AEM uses an underlying Lucene and/or Solr index and it does provide a way to perform a native solr / lucene query.

  • Firstly create a embedded solr index (embedded is sufficient for a local development AEM instance) as mentioned under "Configuring AEM with an embedded SOLR server" in https://docs.adobe.com/docs/en/aem/6-0/deploy/upgrade/queries-and-indexing.html. This will trigger solr indexing of your JCR content.
  • Once indexing is complete (as seen from logs), you can perform native queries using the crx/de query interface. Example query: select [jcr:path] from [nt:base] where native('solr', '<filter>?<solr_query_goes_here>'. Quite obviously you need to be familiar with solr queries. Thanks to the following slide share (slide 50 talks about native queries within AEM) http://www.slideshare.net/justinedelson/demystifying-oak-search

AEM support for native solr queries is a bit patchy. You might need to edit the SOLR schema xml file manually (created under the crx-quickstart folder) to add additional filters, custom fields etc. We had successfully tuned solr within AEM to perform a spacial search using the above method.

0
votes

If you need all sorts of combinations for "foo bar" then you have to query:
fulltext=foo bar

You will only get the first 10 results. To get all, you'll need to:
p.limit=-1

You may want to specify the path:
path=/content/website/

Visit Adobe Query Builder API for more info.

0
votes

Behind the scenes, AEM creates an xpath query and then executes it. Then, for any part of the query that doesn't map to xpath, it runs through the results and filters them.

You should also think about if there is a property to match as opposed to any text. That will give you much better results since you want accuracy. Right now you are casting an overly wide net, and I think you should consider restricting if for nothing other than performance reasons. Just a suggestion.

You say the results don't match your query, can you give us some idea of what comes back? And can you please put your actual query here. That will make it much easier to help.

0
votes

this is a minimal example that provides a full-text search:

Query query = queryBuilder.createQuery(...);    
// limit path 
Predicate path = new Predicate(PathPredicateEvaluator.PATH);
path.set(PathPredicateEvaluator.PATH, "/content/where/ever);
query.getPredicates().add(path);
// Fulltext
Predicate fulltextSearch = new Predicate(FulltextPredicateEvaluator.FULLTEXT);
fulltextSearch.set(FulltextPredicateEvaluator.FULLTEXT, "foo bar");
fulltextSearch.set(FulltextPredicateEvaluator.REL_PATH, "jcr:content");
query.getPredicates().add(fulltextSearch);

// can I haz excerpt?
query.setExcerpt(true);
// Paging?
query.setStart(...);
query.setHitsPerPage(-1);

Note: it's not required to configure a solr index or whatever, you should be fine out of the box. But if you limit the search to specific fields, you should create an index entry in oak:index. You can find a great cheat-sheet here.

0
votes

I'm not sure if this helps.

but to get all the combinations of nodes that have the text i'm looking for I use jcr:like in xpath.

for example if I want to search all the nodes which has any property with Foo bar in its value or key, then my query looks like:

/jcr:root/content/yourpath//*[jcr:like(\*/, '%FOO bar%')]
0
votes

You will not get that flexibility in QueryBuilder but you can still get what you want by using JCR-SQL2.

The following query will return all entries with "Foo Bar", "foo bar", "foo Bar", "Foo bar", but not "foo", "bar", "foo-bar" when your value is "foo bar".

SELECT * FROM [nt:unstructured] WHERE ISDESCENDANTNODE('/jcr:root/content/yourpath') AND LOWER([prop]) LIKE "%foo bar%" ORDER BY [cq:lastModified] desc

Just ensure that while checking for the values in repository you send the value in lowercase for case-insensitive search.

For case-sensitive search you can use:

SELECT * FROM [nt:unstructured] WHERE ISDESCENDANTNODE('/jcr:root/content/yourpath') AND [prop] LIKE "%foo bar%" ORDER BY [cq:lastModified] desc