From c8ff449478e013926e2408512accd9b596fb1171 Mon Sep 17 00:00:00 2001 From: steven choi Date: Fri, 8 Sep 2023 09:22:35 +1000 Subject: [PATCH] #744 remove attributes.text index false and change attribute.text instead of text --- .../au/org/ala/profile/Attribute.groovy | 3 +- .../au/org/ala/profile/SearchService.groovy | 32 +++++-------------- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/grails-app/domain/au/org/ala/profile/Attribute.groovy b/grails-app/domain/au/org/ala/profile/Attribute.groovy index 1e5360a..eddec6c 100644 --- a/grails-app/domain/au/org/ala/profile/Attribute.groovy +++ b/grails-app/domain/au/org/ala/profile/Attribute.groovy @@ -14,8 +14,7 @@ class Attribute implements Comparable { root = false only = ["text", "title"] title component: true - text index: "false" - } + } String uuid Term title diff --git a/grails-app/services/au/org/ala/profile/SearchService.groovy b/grails-app/services/au/org/ala/profile/SearchService.groovy index 072006a..1d0dad8 100644 --- a/grails-app/services/au/org/ala/profile/SearchService.groovy +++ b/grails-app/services/au/org/ala/profile/SearchService.groovy @@ -220,23 +220,12 @@ class SearchService extends BaseDataAccessService { query } - private static QueryBuilder getNameAttributeQuery(String term, List termList) { + private static QueryBuilder getNameAttributeQuery(String term) { List nameTerms = Term.findAllByContainsName(true) // a name attribute is one where the attribute title contains the word 'name' - QueryBuilder query = boolQuery() - query.must(nestedQuery("attributes.title", boolQuery().must(termsQuery("attributes.title.uuid", nameTerms*.uuid)), ScoreMode.Avg)) - for (termItem in termList) { - query.must(wildcardQuery("text", termItem)) - } - query - } - - private static QueryBuilder getWildcardQuery(String fieldName, List termList) { - QueryBuilder query = boolQuery() - for (termItem in termList) { - query.must(wildcardQuery(fieldName, ASTERISK+termItem+ASTERISK).boost(4)) - } - query + boolQuery() + .must(nestedQuery("attributes.title", boolQuery().must(termsQuery("attributes.title.uuid", nameTerms*.uuid)), ScoreMode.Avg)) + .must(matchQuery("text", term).operator(AND)) } private static QueryBuilder buildFilter(String[] accessibleCollections, boolean includeArchived = false, Map> filterLists = [:]) { @@ -270,12 +259,7 @@ class SearchService extends BaseDataAccessService { operator = OR } - def termList = new ArrayList() - if (term) { - termList = Arrays.asList(term.split()) - } - - QueryBuilder attributesWithNames = getNameAttributeQuery(term, termList) + QueryBuilder attributesWithNames = getNameAttributeQuery(term) QueryBuilder query = boolQuery() if (options.includeArchived) { @@ -287,11 +271,11 @@ class SearchService extends BaseDataAccessService { query.mustNot(termQuery("profileStatus", Profile.STATUS_EMPTY)) } - query.must(getWildcardQuery("scientificName", termList)) + query.should(matchQuery("scientificName", term).boost(4)) query.should(nestedQuery("matchedName", boolQuery().must(matchQuery("matchedName.scientificName", term).operator(AND)), ScoreMode.Avg)) query.should(nestedQuery("attributes", attributesWithNames, ScoreMode.Avg).boost(3)) // score name-related attributes higher - query.should(nestedQuery("attributes", boolQuery().must(getWildcardQuery("text", termList)), ScoreMode.Avg)) - query.should(nestedQuery("attributes", boolQuery().must(matchPhrasePrefixQuery("text", term)), ScoreMode.Avg)) + query.should(nestedQuery("attributes", boolQuery().must(matchQuery("attributes.text", term).operator(operator)), ScoreMode.Avg)) + query.should(nestedQuery("attributes", boolQuery().must(matchPhrasePrefixQuery("attributes.text", term)), ScoreMode.Avg)) [query: query] }