From ffd9c5c6cb351111fa9c7d81303fd4bc64619254 Mon Sep 17 00:00:00 2001 From: crimean Date: Mon, 8 Oct 2018 22:44:45 +0300 Subject: [PATCH] Fix search order (full match) --- .../net/osmand/CollatorStringMatcher.java | 8 +++- .../osmand/search/core/SearchCoreFactory.java | 44 +++++++++++++++++++ .../net/osmand/search/core/SearchResult.java | 5 ++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/CollatorStringMatcher.java b/OsmAnd-java/src/main/java/net/osmand/CollatorStringMatcher.java index 27390f3760..d6011c6209 100644 --- a/OsmAnd-java/src/main/java/net/osmand/CollatorStringMatcher.java +++ b/OsmAnd-java/src/main/java/net/osmand/CollatorStringMatcher.java @@ -22,7 +22,8 @@ public class CollatorStringMatcher implements StringMatcher { CHECK_STARTS_FROM_SPACE_NOT_BEGINNING, CHECK_EQUALS_FROM_SPACE, CHECK_CONTAINS, - CHECK_ONLY_STARTS_WITH_TRIM + CHECK_ONLY_STARTS_WITH_TRIM, + CHECK_EQUALS, } public CollatorStringMatcher(String part, StringMatcherMode mode) { @@ -55,6 +56,8 @@ public class CollatorStringMatcher implements StringMatcher { return cstartsWith(collator, base, part, true, false, false, false); case CHECK_ONLY_STARTS_WITH_TRIM: return cstartsWith(collator, base, part, true, false, false, true); + case CHECK_EQUALS: + return cstartsWith(collator, base, part, false, false, true, false); } return false; } @@ -160,6 +163,9 @@ public class CollatorStringMatcher implements StringMatcher { } } } + if (!checkBeginning && !checkSpaces && equals) { + return collator.equals(searchIn, theStart); + } return false; } diff --git a/OsmAnd-java/src/main/java/net/osmand/search/core/SearchCoreFactory.java b/OsmAnd-java/src/main/java/net/osmand/search/core/SearchCoreFactory.java index d4305b8c47..9c3c54c803 100644 --- a/OsmAnd-java/src/main/java/net/osmand/search/core/SearchCoreFactory.java +++ b/OsmAnd-java/src/main/java/net/osmand/search/core/SearchCoreFactory.java @@ -317,6 +317,11 @@ public class SearchCoreFactory { && phrase.isSearchTypeAllowed(ObjectType.CITY)) { String wrd = phrase.getUnknownWordToSearch(); NameStringMatcher nm = phrase.getNameStringMatcher(wrd, phrase.isUnknownSearchWordComplete()); + String unknownSearchPhrase = phrase.getUnknownSearchPhrase().trim(); + NameStringMatcher phraseMatcher = null; + if (!Algorithms.isEmpty(unknownSearchPhrase)) { + phraseMatcher = new NameStringMatcher(unknownSearchPhrase, StringMatcherMode.CHECK_EQUALS); + } // NameStringMatcher nm = phrase.getNameStringMatcher(); resArray.clear(); resArray = townCitiesQR.queryInBox(bbox, resArray); @@ -337,6 +342,9 @@ public class SearchCoreFactory { resultMatcher.publish(res); } else if (nm.matches(res.localeName) || nm.matches(res.otherNames)) { res.firstUnknownWordMatches = wrd.equals(phrase.getUnknownSearchWord()); + if (phraseMatcher != null) { + res.unknownPhraseMatches = phraseMatcher.matches(res.localeName) || phraseMatcher.matches(res.otherNames); + } subSearchApiOrPublish(phrase, resultMatcher, res, cityApi); } if (limit++ > LIMIT * phrase.getRadiusLevel()) { @@ -464,6 +472,11 @@ public class SearchCoreFactory { SearchPhraseDataType.ADDRESS); String wordToSearch = phrase.getUnknownWordToSearch(); + String unknownSearchPhrase = phrase.getUnknownSearchPhrase().trim(); + NameStringMatcher phraseMatcher = null; + if (!Algorithms.isEmpty(unknownSearchPhrase)) { + phraseMatcher = new NameStringMatcher(unknownSearchPhrase, StringMatcherMode.CHECK_EQUALS); + } while (offlineIterator.hasNext() && wordToSearch.length() > 0) { BinaryMapIndexReader r = offlineIterator.next(); currentFile[0] = r; @@ -478,6 +491,9 @@ public class SearchCoreFactory { r.searchAddressDataByName(req); for (SearchResult res : immediateResults) { res.firstUnknownWordMatches = wordToSearch.equals(phrase.getUnknownSearchWord()); + if (phraseMatcher != null) { + res.unknownPhraseMatches = phraseMatcher.matches(res.localeName) || phraseMatcher.matches(res.otherNames); + } if (res.objectType == ObjectType.STREET) { City ct = ((Street) res.object).getCity(); phrase.countUnknownWordsMatch(res, @@ -522,6 +538,13 @@ public class SearchCoreFactory { SearchPhraseDataType.POI); String searchWord = phrase.getUnknownWordToSearch(); final NameStringMatcher nm = phrase.getNameStringMatcher(searchWord, phrase.isUnknownSearchWordComplete()); + String unknownSearchPhrase = phrase.getUnknownSearchPhrase().trim(); + final NameStringMatcher phraseMatcher; + if (!Algorithms.isEmpty(unknownSearchPhrase)) { + phraseMatcher = new NameStringMatcher(unknownSearchPhrase, StringMatcherMode.CHECK_EQUALS); + } else { + phraseMatcher = null; + } QuadRect bbox = phrase.getRadiusBBoxToSearch(BBOX_RADIUS_INSIDE); final Set ids = new HashSet(); SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest( @@ -563,6 +586,9 @@ public class SearchCoreFactory { sr.priorityDistance = 1; } sr.priority = SEARCH_AMENITY_BY_NAME_PRIORITY; + if (phraseMatcher != null) { + sr.unknownPhraseMatches = phraseMatcher.matches(sr.localeName) || phraseMatcher.matches(sr.otherNames); + } phrase.countUnknownWordsMatch(sr); sr.objectType = ObjectType.POI; resultMatcher.publish(sr); @@ -865,6 +891,13 @@ public class SearchCoreFactory { private ResultMatcher getResultMatcher(final SearchPhrase phrase, final SearchResultMatcher resultMatcher, final String customName, final BinaryMapIndexReader selected, final Set searchedPois) { + String unknownSearchPhrase = phrase.getUnknownSearchPhrase().trim(); + final NameStringMatcher phraseMatcher; + if (!Algorithms.isEmpty(unknownSearchPhrase)) { + phraseMatcher = new NameStringMatcher(unknownSearchPhrase, StringMatcherMode.CHECK_EQUALS); + } else { + phraseMatcher = null; + } final NameStringMatcher ns; final boolean hasCustomName = !Algorithms.isEmpty(customName); if (hasCustomName) { @@ -905,6 +938,9 @@ public class SearchCoreFactory { res.location = object.getLocation(); res.priority = SEARCH_AMENITY_BY_TYPE_PRIORITY; res.priorityDistance = 1; + if (phraseMatcher != null) { + res.unknownPhraseMatches = phraseMatcher.matches(res.localeName) || phraseMatcher.matches(res.otherNames); + } res.objectType = ObjectType.POI; resultMatcher.publish(res); return false; @@ -999,6 +1035,11 @@ public class SearchCoreFactory { String wordToSearch = phrase.getUnknownWordToSearch(); boolean firstUnknownWordMatches = wordToSearch.equals(phrase.getUnknownSearchWord()); NameStringMatcher nm = phrase.getNameStringMatcher(wordToSearch, phrase.isUnknownSearchWordComplete()); + String unknownSearchPhrase = phrase.getUnknownSearchPhrase().trim(); + NameStringMatcher phraseMatcher = null; + if (!Algorithms.isEmpty(unknownSearchPhrase)) { + phraseMatcher = new NameStringMatcher(unknownSearchPhrase, StringMatcherMode.CHECK_EQUALS); + } for (Street object : c.getStreets()) { SearchResult res = new SearchResult(phrase); @@ -1016,6 +1057,9 @@ public class SearchCoreFactory { res.firstUnknownWordMatches = firstUnknownWordMatches || phrase.getNameStringMatcher().matches(res.localeName) || phrase.getNameStringMatcher().matches(res.otherNames); + if (phraseMatcher != null) { + res.unknownPhraseMatches = phraseMatcher.matches(res.localeName) || phraseMatcher.matches(res.otherNames); + } res.localeRelatedObjectName = c.getName(phrase.getSettings().getLang(), phrase.getSettings().isTransliterate()); res.object = object; res.preferredZoom = 17; diff --git a/OsmAnd-java/src/main/java/net/osmand/search/core/SearchResult.java b/OsmAnd-java/src/main/java/net/osmand/search/core/SearchResult.java index 3bf7cfa995..03294e6e8a 100644 --- a/OsmAnd-java/src/main/java/net/osmand/search/core/SearchResult.java +++ b/OsmAnd-java/src/main/java/net/osmand/search/core/SearchResult.java @@ -20,7 +20,7 @@ public class SearchResult { public SearchResult parentSearchResult; public Collection otherWordsMatch = null; public boolean firstUnknownWordMatches = true; - + public boolean unknownPhraseMatches = false; public SearchResult(SearchPhrase sp) { @@ -32,6 +32,9 @@ public class SearchResult { if(firstUnknownWordMatches) { inc = 1; } + if (unknownPhraseMatches) { + inc += 1000; + } if(otherWordsMatch != null) { inc += otherWordsMatch.size(); }