From 26ad2dd37590a51419ada7e5c2562d1135a211e5 Mon Sep 17 00:00:00 2001 From: Lihui Hu Date: Wed, 26 Jun 2024 10:59:27 -0400 Subject: [PATCH] update Cache ID --- ...tImportSupportingGsrsEntityController.java | 7 +- ...tLegacyTextSearchGsrsEntityController.java | 29 +++--- .../java/ix/core/search/text/TextIndexer.java | 89 +++++++++++-------- 3 files changed, 68 insertions(+), 57 deletions(-) diff --git a/gsrs-spring-boot-autoconfigure/src/main/java/gsrs/controller/AbstractImportSupportingGsrsEntityController.java b/gsrs-spring-boot-autoconfigure/src/main/java/gsrs/controller/AbstractImportSupportingGsrsEntityController.java index 0080c087..73386b45 100644 --- a/gsrs-spring-boot-autoconfigure/src/main/java/gsrs/controller/AbstractImportSupportingGsrsEntityController.java +++ b/gsrs-spring-boot-autoconfigure/src/main/java/gsrs/controller/AbstractImportSupportingGsrsEntityController.java @@ -1089,7 +1089,7 @@ public FacetMeta searchImportFacetFieldDrilldownV1(@RequestParam("q") Optional userLists = new ArrayList<>(); String userName = ""; - if(GsrsSecurityUtils.getCurrentUsername().isPresent()) { + if(field.isPresent() && field.get().equalsIgnoreCase("User List") && GsrsSecurityUtils.getCurrentUsername().isPresent()) { userName = GsrsSecurityUtils.getCurrentUsername().get(); userLists= userSavedListService.getUserSearchResultLists(userName, getEntityService().getEntityClass().getName()); } @@ -1098,10 +1098,7 @@ public FacetMeta searchImportFacetFieldDrilldownV1(@RequestParam("q") Optional @RequestParam("sortBy") Optional sortBy, @RequestParam("sortDesc") Optional sortOrder, HttpServletRequest request) throws ParseException, IOException { + + SearchOptions so = new SearchOptions.Builder() .kind(getEntityService().getEntityClass()) .top(Integer.MAX_VALUE) // match Play GSRS @@ -399,20 +401,17 @@ public FacetMeta searchFacetFieldDrilldownV1(@RequestParam("q") Optional List userLists = new ArrayList<>(); String userName = ""; - if(GsrsSecurityUtils.getCurrentUsername().isPresent()) { + if(field.isPresent() && field.get().equalsIgnoreCase("User List") && GsrsSecurityUtils.getCurrentUsername().isPresent()) { userName = GsrsSecurityUtils.getCurrentUsername().get(); userLists= userSavedListService.getUserSearchResultLists(userName, getEntityService().getEntityClass().getName()); } - + String cacheID = getFacetCacheID("", query.orElse(""), so, field.orElse("")); log.info("cache ID: " + cacheID); TextIndexer.TermVectors tv = (TextIndexer.TermVectors)gsrscache.getRaw(cacheID); - if(tv == null) { - tv = getlegacyGsrsSearchService().getTermVectorsFromQuery(query.orElse(null), so, field.orElse(null)); - gsrscache.setRaw(cacheID, tv); - log.info("/search/@facet: getting facets from indexes"); - }else { - log.info("/search/@facet: getting facets from cache"); + if(tv == null) { + tv = getlegacyGsrsSearchService().getTermVectorsFromQuery(query.orElse(null), so, field.orElse(null)); + gsrscache.setRaw(cacheID, tv); } String sortByProp = sortBy.isPresent()?sortBy.get():""; @@ -445,20 +444,17 @@ public FacetMeta searchFacetFieldV1(@RequestParam("field") Optional fiel List userLists = new ArrayList<>(); String userName = ""; - if(GsrsSecurityUtils.getCurrentUsername().isPresent()) { + if(field.isPresent() && field.get().equalsIgnoreCase("User List") && GsrsSecurityUtils.getCurrentUsername().isPresent()) { userName = GsrsSecurityUtils.getCurrentUsername().get(); userLists= userSavedListService.getUserSearchResultLists(userName, getEntityService().getEntityClass().getName()); } String cacheID = getFacetCacheID("", "", so, field.orElse("")); - log.info("cache ID: " + cacheID); +// log.info("cache ID: " + cacheID); TextIndexer.TermVectors tv = (TextIndexer.TermVectors)gsrscache.getRaw(cacheID); if(tv == null) { tv = getlegacyGsrsSearchService().getTermVectors(field); - gsrscache.setRaw(cacheID, tv); - log.info("/@facet: getting facets from indexes"); - }else { - log.info("/@facet: getting facets from cache"); + gsrscache.setRaw(cacheID, tv); } String sortByProp = sortBy.isPresent()?sortBy.get():""; @@ -1470,7 +1466,8 @@ private String generateResultIDJson(String id) { } protected String getFacetCacheID(String namespace, String query, SearchOptions so, String field) { - return namespace + "FacetSearch/" + query + field + ",ffilter=" + so.getFfilter() - +",fskip=" + so.getFskip() + "," + so.toString(); + SearchRequest.Builder builder = new SearchRequest.Builder(); + String SRHash = builder.options(so).query(query).build().getDefiningSetSha1(); + return namespace + "FacetSearch/" + field + SRHash; } } diff --git a/gsrs-spring-legacy-indexer/src/main/java/ix/core/search/text/TextIndexer.java b/gsrs-spring-legacy-indexer/src/main/java/ix/core/search/text/TextIndexer.java index a4123c62..8ff7c624 100644 --- a/gsrs-spring-legacy-indexer/src/main/java/ix/core/search/text/TextIndexer.java +++ b/gsrs-spring-legacy-indexer/src/main/java/ix/core/search/text/TextIndexer.java @@ -458,37 +458,53 @@ else if(sortBy.equalsIgnoreCase(FacetImpl.COUNT)){ } } } + + if(userName.isEmpty() || userLists.size() == 0) { + + terms.entrySet() + .stream() + .parallel() + .map(es->Tuple.of(es.getKey(), es.getValue().getNDocs())) + .filter(filt) + .map(t->new FV(fac,t.k(),t.v())) + .collect(StreamUtil.maxElements(top+skip, facetComparator)) + .skip(skip) + .limit(top) + .forEach(fv->{ + fac.add(fv); + }); + }else { + + terms.entrySet() + .stream() + .parallel() + .map(es->Tuple.of(es.getKey(), es.getValue().getNDocs())) + .filter(filt) + .map(t->new FV(fac,t.k(),t.v())) + .filter(fv->{ + if(field.equalsIgnoreCase("User List")) { + // check the current user, filter facet by user name + UserListIndexedValue dataItem = UserSavedListService.getUserNameAndListNameFromIndexedValue(fv.getLabel()); + String nameInLabel = dataItem.getUserName(); + String listName = dataItem.getListName(); + if(userName.equalsIgnoreCase(nameInLabel) && userLists.contains(listName)) { + return true; + }else { + return false; + } + }else { + return true; + } + }) + .collect(StreamUtil.maxElements(top+skip, facetComparator)) + .skip(skip) + .limit(top) + .forEach(fv->{ + fac.add(fv); + }); + + } - terms.entrySet() - .stream() - .parallel() - .map(es->Tuple.of(es.getKey(), es.getValue().getNDocs())) - .filter(filt) - .map(t->new FV(fac,t.k(),t.v())) - .filter(fv->{ - if(field.equalsIgnoreCase("User List")) { - // check the current user, filter facet by user name - UserListIndexedValue dataItem = UserSavedListService.getUserNameAndListNameFromIndexedValue(fv.getLabel()); - String nameInLabel = dataItem.getUserName(); - String listName = dataItem.getListName(); - if(userName.isEmpty() || userLists.size() == 0) { - return false; - }else if(userName.equalsIgnoreCase(nameInLabel) && userLists.contains(listName)) { - return true; - }else { - return false; - } - }else { - return true; - } - }) - .collect(StreamUtil.maxElements(top+skip, facetComparator)) - .skip(skip) - .limit(top) - .forEach(fv->{ - fac.add(fv); - }); - return new FacetMeta.Builder() .facets(fac) .ffilter(filter) @@ -558,6 +574,7 @@ static class TermVectorsCollector private TermVectorsCollector (Class kind, String originalField, IndexSearcher searcher, Query extrafilter, Query q) throws IOException { + String adaptedField = TERM_VEC_PREFIX + originalField; tvec = new TermVectors (kind, adaptedField); @@ -571,11 +588,10 @@ private TermVectorsCollector (Class kind, String originalField, IndexSearcher .collect(Collectors.toSet()); fieldSet.add(FIELD_KIND); - this.reader = searcher.getIndexReader(); Query filter = filterForKinds(kind); - + if(q==null){ q = new MatchAllDocsQuery(); } @@ -592,18 +608,19 @@ private TermVectorsCollector (Class kind, String originalField, IndexSearcher .add(filter, BooleanClause.Occur.FILTER) .build(); } - + searcher.search(q, this); - + Collections.sort(tvec.docs); - + tvec.terms= counts.entrySet() .stream() .map(Tuple::of) .map(Tuple.vmap(DocumentSet::of)) .collect(Tuple.toMap()); counts = null; - + + } //