From 5e04ec4db05e32f56a4d3516286e0238d2d39cb6 Mon Sep 17 00:00:00 2001 From: crimean Date: Sat, 3 Nov 2018 12:36:50 +0300 Subject: [PATCH] Fix search space issue --- .../osmand/search/core/SearchCoreFactory.java | 50 +++++++------------ .../net/osmand/search/core/SearchPhrase.java | 27 ++++++++-- 2 files changed, 41 insertions(+), 36 deletions(-) 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 ae8d81cd68..5c7a138794 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 @@ -530,7 +530,7 @@ public class SearchCoreFactory { return false; } if (phrase.isNoSelectedType() && phrase.isUnknownSearchWordPresent() - && phrase.isUnknownSearchWordComplete() && searchAmenityTypesAPI.hasFoundPoiTypes()) { + && phrase.isUnknownSearchWordComplete() && phrase.hasUnknownSearchWordPoiTypes()) { return false; } final BinaryMapIndexReader[] currentFile = new BinaryMapIndexReader[1]; @@ -653,30 +653,12 @@ public class SearchCoreFactory { private List customPoiFilters = new ArrayList<>(); private TIntArrayList customPoiFiltersPriorites = new TIntArrayList(); private MapPoiTypes types; - private List foundPoiTypes = new ArrayList<>(); - private SearchPhrase lastSearchedPhrase; public SearchAmenityTypesAPI(MapPoiTypes types) { super(ObjectType.POI_TYPE); this.types = types; } - public List getFoundPoiTypes() { - return foundPoiTypes; - } - - public boolean hasFoundPoiTypes() { - return foundPoiTypes.size() > 0; - } - - public SearchPhrase getLastSearchedPhrase() { - return lastSearchedPhrase; - } - - public void setLastSearchedPhrase(SearchPhrase lastSearchedPhrase) { - this.lastSearchedPhrase = lastSearchedPhrase; - } - public void clearCustomFilters() { this.customPoiFilters.clear(); this.customPoiFiltersPriorites.clear(); @@ -739,8 +721,7 @@ public class SearchCoreFactory { } } } - foundPoiTypes = new ArrayList<>(results); - lastSearchedPhrase = phrase; + phrase.setUnknownSearchWordPoiTypes(new ArrayList<>(results)); if (resultMatcher != null) { for (AbstractPoiType pt : results) { @@ -852,18 +833,16 @@ public class SearchCoreFactory { } searchPoi(phrase, resultMatcher, obj, null, ptf); } else if (searchAmenityTypesAPI != null) { - if (searchAmenityTypesAPI.lastSearchedPhrase == null - || !searchAmenityTypesAPI.lastSearchedPhrase.getUnknownSearchPhrase().equals(phrase.getUnknownSearchPhrase())) { + if (phrase.getUnknownSearchWordPoiTypes() == null) { searchAmenityTypesAPI.search(phrase, null); } - List poiTypes = searchAmenityTypesAPI.getFoundPoiTypes(); - for (AbstractPoiType pt : poiTypes) { - SearchPoiTypeFilter ptf = getPoiTypeFilter(pt); - String customName = phrase.getPoiNameFilter(pt); + AbstractPoiType poiType = phrase.getUnknownSearchWordPoiType(); + if (poiType != null) { + SearchPoiTypeFilter ptf = getPoiTypeFilter(poiType); + String customName = phrase.getPoiNameFilter(poiType); if (customName != null) { - phrase.setUnknownSearchWordPoiType(pt); - searchPoi(phrase, resultMatcher, null, customName, ptf); - break; + phrase.setUnknownSearchWordPoiType(poiType); + searchPoi(phrase, resultMatcher, null, customName.length() == 0 ? null : customName, ptf); } } } @@ -939,7 +918,14 @@ public class SearchCoreFactory { res.priority = SEARCH_AMENITY_BY_TYPE_PRIORITY; res.priorityDistance = 1; if (phraseMatcher != null) { - res.unknownPhraseMatches = phraseMatcher.matches(res.localeName) || phraseMatcher.matches(res.otherNames); + boolean unknownPhraseMatches = phraseMatcher.matches(res.localeName) || phraseMatcher.matches(res.otherNames); + AbstractPoiType unknownSearchWordPoiType = phrase.getUnknownSearchWordPoiType(); + if (unknownPhraseMatches && unknownSearchWordPoiType != null) { + unknownPhraseMatches = !phraseMatcher.matches(unknownSearchWordPoiType.getTranslation()) + && !phraseMatcher.matches(unknownSearchWordPoiType.getEnTranslation()) + && !phraseMatcher.matches(unknownSearchWordPoiType.getSynonyms()); + } + res.unknownPhraseMatches = unknownPhraseMatches; } res.objectType = ObjectType.POI; resultMatcher.publish(res); @@ -988,7 +974,7 @@ public class SearchCoreFactory { @Override public int getSearchPriority(SearchPhrase p) { if ((p.isLastWord(ObjectType.POI_TYPE) && p.getLastTokenLocation() != null) - || (p.isNoSelectedType() && p.isUnknownSearchWordComplete())) { + || (p.isNoSelectedType())) { return SEARCH_AMENITY_BY_TYPE_PRIORITY; } return -1; diff --git a/OsmAnd-java/src/main/java/net/osmand/search/core/SearchPhrase.java b/OsmAnd-java/src/main/java/net/osmand/search/core/SearchPhrase.java index 884ea96b7c..ff13b5a6ef 100644 --- a/OsmAnd-java/src/main/java/net/osmand/search/core/SearchPhrase.java +++ b/OsmAnd-java/src/main/java/net/osmand/search/core/SearchPhrase.java @@ -36,6 +36,7 @@ public class SearchPhrase { private String rawUnknownSearchPhrase = ""; private String unknownSearchPhrase = ""; private AbstractPoiType unknownSearchWordPoiType; + private List unknownSearchWordPoiTypes = null; private NameStringMatcher sm; private SearchSettings settings; @@ -186,7 +187,7 @@ public class SearchPhrase { public boolean isUnknownSearchWordComplete() { - return lastUnknownSearchWordComplete || unknownWords.size() > 0; + return lastUnknownSearchWordComplete || unknownWords.size() > 0 || unknownSearchWordPoiType != null; } public boolean isLastUnknownSearchWordComplete() { @@ -248,6 +249,24 @@ public class SearchPhrase { return getPoiNameFilter(unknownSearchWordPoiType); } + public boolean hasUnknownSearchWordPoiTypes() { + return unknownSearchWordPoiTypes != null && unknownSearchWordPoiTypes.size() > 0; + } + + public List getUnknownSearchWordPoiTypes() { + return unknownSearchWordPoiTypes; + } + + public void setUnknownSearchWordPoiTypes(List unknownSearchWordPoiTypes) { + this.unknownSearchWordPoiTypes = unknownSearchWordPoiTypes; + for (AbstractPoiType pt : unknownSearchWordPoiTypes) { + if (getPoiNameFilter(pt) != null) { + setUnknownSearchWordPoiType(pt); + break; + } + } + } + public String getPoiNameFilter(AbstractPoiType pt) { String nameFilter = null; if (pt != null) { @@ -256,11 +275,11 @@ public class SearchPhrase { String enTranslation = pt.getEnTranslation(); String translation = pt.getTranslation(); String synonyms = pt.getSynonyms(); - if (unknownSearchPhrase.length() > enTranslation.length() && nm.matches(enTranslation)) { + if (unknownSearchPhrase.length() >= enTranslation.length() && nm.matches(enTranslation)) { nameFilter = unknownSearchPhrase.substring(enTranslation.length()).trim(); - } else if (unknownSearchPhrase.length() > translation.length() && nm.matches(translation)) { + } else if (unknownSearchPhrase.length() >= translation.length() && nm.matches(translation)) { nameFilter = unknownSearchPhrase.substring(translation.length()).trim(); - } else if (unknownSearchPhrase.length() > synonyms.length() && nm.matches(synonyms)) { + } else if (unknownSearchPhrase.length() >= synonyms.length() && nm.matches(synonyms)) { nameFilter = unknownSearchPhrase.substring(synonyms.length()).trim(); } }