How to implement search using Query Builder API for partial search text in CQ/AEM - aem

I have a requirement to fetch search results based on partial text match. For example, if there is a node under products say "apple-iphone-6" and the user enters "iphone" text in the searchbox, I should still be able to fetch the result.
I tried the below query on querybuilder and it worked:
http://localhost:4502/bin/querybuilder.json?path=/etc/commerce/products&type=nt:unstructured&nodename=*iphone*
But, how to implement this programatically for the *iphone* part? I am creating a query using the predicates as follows
String searchTerm = "iphone";
map.put("path", "/etc/commerce/products");
map.put("type", "nt:unstructured");
map.put("nodename", searchTerm);
Query query = queryBuilder.createQuery(PredicateGroup.create(map), session);
SearchResult result = query.getResult();
But I do not get any results, reason being, the node name(apple-iphone-6) does not exactly match the search term (iphone).
But the same thing works fine in case I append * to the nodename value which then implements partial text based search in the querybuilder example. What change should I do in the code to get results based on partial node name matches?

You already have found the solution on your own, the NodenamePredicateEvaluator accepts wildcard arguments, so you would need to surround the search term with wildcards, for example like this:
String searchTerm = "iphone";
...
map.put("nodename", "*" + searchTerm + "*");

in this case "like" opration can be used:
EX-> patial text serach for jcr:title
map.put("group.1_property", "fn:lower-case(#jcr:content/jcr:title)");
map.put("group.1_property.value", "%"+fulltextSearchTerm + "%");
map.put("group.1_property.operation", "like");

For just the nodename the answer posted is correct, but if you want to search inside properties as well then :
map.put("fulltext","*"+searchTetm +"*");
map.put("fulltext.relPath","jcr:content");

Related

How to override Ag Grid QuickFilter to receive exact match results?

By default Ag Grid Quick Filter function return rows that contains search string. For example if I type "30 June" in the searchbox, quick filter will also return rows that contains "30 cars were sold by 2 June" text. How can I override default behavior to receive only rows that exactly match my search string?
What I did was the following:
In the search itself, I removed the spaces from the search criteria:
this.gridApi.setQuickFilter(event.toLowerCase().replace(" ", ""));
In each column that I wanted an exact match, I added this code in the column definition:
getQuickFilterText: (params) => { return params.value && params.value.toLowerCase().replace(" ", "");}
(That is the override method for search. See here for more details: https://www.ag-grid.com/angular-data-grid/filter-quick/)
It seems to be working for me.
To achieve the exact match results column wise, You have to do these two things :
Remove cacheQuickFilter property from your default column definition object in gridOptions as caching convert all the columns data into a string separated by backward slash. That's the reason it will not be able to search column by column.
Add getQuickFilterText function in each column definition and add a condition for a exact match else return an empty string.
getQuickFilterText: params => {
return (params.value === <quick filter value>) ? params.value : ''
}
Now the tricky part here is how to access quick filter value inside getQuickFilterText function. You can achieve this in two ways :
Assign an id to quick filter search element and then access it's value using document.getElementById('quick-filter').value
Store the quick filter search value on change and put into a store state or service and then access that inside getQuickFilterText function.

Apply filtering with an array of value instead of one value for a filter in Algolia

I have in my index a list of object, each of them has an objectID value.
On some search, i want to filter OUT a certain number of them, using there objectID.
For the moment it works with one value as a string, i would like to know how to do for multiple value.
filters = 'NOT objectID:' + objectIDToFilter;
This work for one object, what can i do to apply this for an array of ObjectID. because :
filters = 'NOT objectID:' + arrayObjectID;
does not work.
I was thinking of generating a huge string with an arrayId.map with all my 'NOT objectID:1 AND NOT objectID: 2 ...' but i wanted to know if there is a cleaner way to do it.
I unfortunately misunderstood the line in algolia doc :
Array Attributes: Any attribute set up as an array will match the filter as soon as one of the values in the array match.
This apparently refers to the value itself in Algolia and not the filter
So i did not found a solution on algolia doc, i went for the long string, hope there is no limits on how much filter we can add on a query (found nothing about that).
Here is what i did if someone need it :
let filters = `NOT objectID:${userID}`;
blockedByUsers.map((blockedByUser) => {
filters = filters + ` AND NOT objectID:${blockedByUser}`;
});
If you need to add multiple but don't have a starting point like i do, you can't start the query with an AND , a solution i found to bypass that:
let filters = `NOT objectID:${blockedByUsers[0]}`;
blockedByUsers.map((blockedByUser, i) => {
if (i > 0) filters = filters + ` AND NOT objectID:${blockedByUser}`;
});
There is probably a cleaner solution, but this work. If you have found other solution for that problems i'll be happy to see :)

Lucene.net search doesn't return names

I am using WhitespaceAnalyzer to index some values.
document.AddField("transcript", <transcript value>, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED);
and do a search like follows
booleanMiniQuery.Add(new TermQuery(new Term("transcript", <search value>)), rule);
when contains some names like baileys OR bailey doing search for that doesn't return any result.
Can any one tell what I am doing wrong
Before you search for a term in Lucene, you need to analyze the string with the same analyzer that you indexed that text with (in this case WhiteSpaceAnalyzer).
I would recommend using the QueryParser to make this process a lot easier, for eample:
var qParser = New QueryParsers.Classic.QueryParser(Version, "transcript", WhiteSpaceAnalyzer) var termQuery = qParser.parse(<search value>)
booleanMiniQuery.Add(termQuery, rule);

How to concat two columns for search with feathers-sequelize?

I need to search for users by name, their first name and last names are stored in separate columns in a postgresql database. The columns need to be concatenated for search to work properly. Typing the full first and last name of a user should match a result.
What could I pass as a query to the find method of a Feathers service that would allow me to do this?
As in the answer linked you can pass the where clause to the Feathers service by modifying params.query in a before hook:
app.service('users').before({
find(hook) {
const where = Sequelize.where(Sequelize.fn("concat",
Sequelize.col("firstname"),
Sequelize.col("lastname")), {
like: '%John Do%'
}
);
hook.params.query.where = where;
}
})

NumericRangeQuery in NHibernate.Search

I am creating a search, where the user can both choose an interval and search on a term in the same go.
This is however giving me trouble, since I have up until have only used the usual text query.
I am wondering how I am to go about using both a NumericRangeQuery and a regular term query. Usually I would use a query below:
var parser = new MultiFieldQueryParser(
new[] { "FromPrice", "ToPrice", "Description"}, new SimpleAnalyzer());
Query query = parser.Parse(searchQuery.ToString());
IFullTextSession session = Search.CreateFullTextSession(this.Session);
IQuery fullTextQuery = session.CreateFullTextQuery(query, new[] { typeof(MyObject) });
IList<MyObject> results = fullTextQuery.List<MyObject>();
But if I was to e.g. search the range FromPrice <-> ToPrice and also the description, how should I do this, since session.CreateFullTextQuery only takes one Query object?
you can create a single query that is a BooleanQuery combining all the conditions you want to be met.
For the ranges, heres a link to the synthax using the QueryParser:
http://lucene.apache.org/core/old_versioned_docs/versions/2_9_2/queryparsersyntax.html#Range Searches