Absent Member.
Absent Member.

(SM) support tip: How to use the negative operator in KM Advanced search with SOLR

When using field searches from the KM advanced search, special characters and other syntax are typically stripped to keep the search from failing.  Users would like to be able to search not only for a field equals value but also for field unequals value or what we would call a "negative" query.  Out of the box, KM using SOLR does not support this but it is easy to achieve.


We cannot simply add the negative operator to our value like this: -value.  This is incorrect syntax for the search.  The proper syntax would be: -field:value.  But since the user cannot manually add the negative operator to the field name, how can me make this work without a full rewrite of the advanced search screen?

Simple, here's what to do:

We will modify the author field for this example. From Manage Knowledgebases for the library you wish to work on (ie Knowledge_Library), go to the "type information" tab. Click on the magnifying glass next to the Advanced Search Script "Knowledge_Library_kmprocesslibcriteria", a script should open. Scroll down to the section that begins with "Else" around line 50.  This is where the SOLR specific code resides. Look for the line "if(KMQuery.kmlib.author != null)" We want to modify this area from this: if(KMQuery.kmlib_author != null)     strQuery += " (author: " + KMQuery.kmlib_author + " AND knowledgebase_name: Knowledge_Library)"; to this: if(KMQuery.kmlib_author != null)    if (KMQuery.kmlib_author.charAt(0) == "-")     strQuery += " (-author: " + KMQuery.kmlib_author.substr(1,KMQuery.kmlib_author.length) + " AND knowledgebase_name: Knowledge_Library)";    else     strQuery += " (author: " + KMQuery.kmlib_author + " AND knowledgebase_name: Knowledge_Library)";

Save and then exit this script library.

How it works: We are checking first to see if the author field contains a value.  If so, we check to see if the first character is the negative operator "-".  If it is, we add the negative operator to the field name and remove it from the field value.  Otherwise, we work as we did before.

Now, if we enter "falcon" in the author field of the advanced search form, we will find all documents where falcon is the author. If we enter "-falcon", we will find all documents where falcon IS NOT the author. This can be done for any field in the advanced search form.

HP Support
If you find that this or any post resolved your issue, please be sure
to mark it as an accepted solution.
Please also give kudo if you find it interesting 🙂
Labels (1)
0 Replies
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.