From bd1d52df08074548c3681233cc48e6e46cd6a324 Mon Sep 17 00:00:00 2001 From: Ralph Soika Date: Tue, 3 Sep 2019 23:01:55 +0200 Subject: [PATCH] implementation of method getTotalHits Issue #554 --- .../org/imixs/workflow/util/JSONParser.java | 16 +++++++ .../engine/solr/SolrIndexService.java | 4 +- .../engine/solr/SolrSearchService.java | 44 +++++++++---------- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/imixs-workflow-core/src/main/java/org/imixs/workflow/util/JSONParser.java b/imixs-workflow-core/src/main/java/org/imixs/workflow/util/JSONParser.java index 78571b507..ad21885ac 100644 --- a/imixs-workflow-core/src/main/java/org/imixs/workflow/util/JSONParser.java +++ b/imixs-workflow-core/src/main/java/org/imixs/workflow/util/JSONParser.java @@ -69,6 +69,22 @@ public static String getKey(String key, String json) { result = parser.getString(); break; } + if (event.name().equals(Event.VALUE_NUMBER.toString())) { + result = parser.getBigDecimal()+""; + break; + } + if (event.name().equals(Event.VALUE_TRUE.toString())) { + result = "true"; + break; + } + if (event.name().equals(Event.VALUE_FALSE.toString())) { + result = "false"; + break; + } + if (event.name().equals(Event.VALUE_NULL.toString())) { + result = null; + break; + } if (event.name().equals(Event.START_OBJECT.toString())) { // just return the next json object here result = parser.getObject().toString(); diff --git a/imixs-workflow-index-solr/src/main/java/org/imixs/workflow/engine/solr/SolrIndexService.java b/imixs-workflow-index-solr/src/main/java/org/imixs/workflow/engine/solr/SolrIndexService.java index 6e256dfce..9717cb379 100644 --- a/imixs-workflow-index-solr/src/main/java/org/imixs/workflow/engine/solr/SolrIndexService.java +++ b/imixs-workflow-index-solr/src/main/java/org/imixs/workflow/engine/solr/SolrIndexService.java @@ -359,8 +359,8 @@ public String query(String searchTerm, int pageSize, int pageIndex, SortOrder so } } - // page size - if (pageSize <= 0) { + // page size of 0 is allowed here - this will be used by the getTotalHits method of the SolrSearchService + if (pageSize < 0) { pageSize = DEFAULT_PAGE_SIZE; } diff --git a/imixs-workflow-index-solr/src/main/java/org/imixs/workflow/engine/solr/SolrSearchService.java b/imixs-workflow-index-solr/src/main/java/org/imixs/workflow/engine/solr/SolrSearchService.java index 9caaf9859..9c0090649 100644 --- a/imixs-workflow-index-solr/src/main/java/org/imixs/workflow/engine/solr/SolrSearchService.java +++ b/imixs-workflow-index-solr/src/main/java/org/imixs/workflow/engine/solr/SolrSearchService.java @@ -33,7 +33,6 @@ import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; -import java.util.Set; import java.util.logging.Logger; import javax.annotation.security.DeclareRoles; @@ -46,13 +45,13 @@ import org.eclipse.microprofile.config.inject.ConfigProperty; import org.imixs.workflow.ItemCollection; -import org.imixs.workflow.WorkflowKernel; import org.imixs.workflow.engine.DocumentService; import org.imixs.workflow.engine.index.DefaultOperator; import org.imixs.workflow.engine.index.SchemaService; import org.imixs.workflow.engine.index.SearchService; import org.imixs.workflow.engine.index.SortOrder; import org.imixs.workflow.exceptions.QueryException; +import org.imixs.workflow.util.JSONParser; /** * This session ejb provides a service to search the solr index. @@ -178,11 +177,10 @@ public List search(String searchTerm, int pageSize, int pageInde * method did not load any data. The provided search term will we extended with * a users roles to test the read access level of each workitem matching the * search term. + *

+ * In Solr we can get the count if we the the query param 'row=0'. + * The the response contains still the numFound but not docs! * - * The optional param 'maxResult' can be set to overwrite the - * DEFAULT_MAX_SEARCH_RESULT. - * - * @see search(String, int, int, Sort, Operator) * * @param sSearchTerm * @param maxResult @@ -194,31 +192,31 @@ public List search(String searchTerm, int pageSize, int pageInde @Override public int getTotalHits(final String _searchTerm, final int _maxResult, final DefaultOperator defaultOperator) throws QueryException { - - int maxResult = _maxResult; - - if (maxResult <= 0) { - maxResult = DEFAULT_MAX_SEARCH_RESULT; - } + long l=System.currentTimeMillis(); + int hits=0; +// if (_maxResult!=0) { +// logger.severe("getTotalHits should be called with maxResult==0"); +// return 0; +// } - // quey only the $uniqueid String searchTerm = schemaService.getExtendedSearchTerm(_searchTerm); // test if searchtem is provided if (searchTerm == null || "".equals(searchTerm)) { return 0; } - // post query.... - String result = solarIndexService.query(searchTerm, _maxResult, 0,null,defaultOperator, true); - -// TODO now parse the count!!! -// -// -// logger.finest("......Result = " + result); -// -// + // post query with row = 0 + String result = solarIndexService.query(searchTerm, 0, 0,null,defaultOperator, true); + try { + String response=JSONParser.getKey("response", result); + hits=Integer.parseInt(JSONParser.getKey("numFound", response)); + } catch (NumberFormatException e) { + logger.severe("getTotalHits - failed to parse solr result object! - " + e.getMessage()); + hits=0; + } - return 0; + logger.info("......computed totalHits in " +(System.currentTimeMillis()-l) + "ms"); + return hits; } /**