From 626c43e00c25015e550aca6e2396c7f047aaf424 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 21 Jul 2016 11:34:54 +0200 Subject: [PATCH 1/2] Add method filter duplicate results --- .../src/net/osmand/search/SearchUICore.java | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/search/SearchUICore.java b/OsmAnd-java/src/net/osmand/search/SearchUICore.java index e544381b2a..1ad584619b 100644 --- a/OsmAnd-java/src/net/osmand/search/SearchUICore.java +++ b/OsmAnd-java/src/net/osmand/search/SearchUICore.java @@ -4,16 +4,19 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import java.util.Iterator; import java.util.List; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import net.osmand.Collator; import net.osmand.OsmAndCollator; import net.osmand.PlatformUtil; import net.osmand.ResultMatcher; import net.osmand.binary.BinaryMapIndexReader; +import net.osmand.data.Amenity; import net.osmand.data.LatLon; import net.osmand.osm.MapPoiTypes; import net.osmand.search.core.ObjectType; @@ -25,11 +28,13 @@ import net.osmand.search.core.SearchSettings; import net.osmand.search.core.SearchWord; import net.osmand.search.core.SearchPhrase.NameStringMatcher; import net.osmand.util.Algorithms; +import net.osmand.util.MapUtils; import org.apache.commons.logging.Log; public class SearchUICore { + private static final int TIMEOUT_BETWEEN_CHARS = 200; private static final Log LOG = PlatformUtil.getLog(SearchUICore.class); private SearchPhrase phrase; private SearchResultCollection currentSearchResult = new SearchResultCollection(); @@ -43,6 +48,7 @@ public class SearchUICore { List apis = new ArrayList<>(); private SearchSettings searchSettings; private MapPoiTypes poiTypes; + private Collator collator; public SearchUICore(MapPoiTypes poiTypes, String locale, BinaryMapIndexReader[] searchIndexes) { @@ -54,6 +60,7 @@ public class SearchUICore { phrase = new SearchPhrase(searchSettings); singleThreadedExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, taskQueue); init(); + collator = OsmAndCollator.primaryCollator(); } public static class SearchResultCollection { @@ -141,6 +148,7 @@ public class SearchUICore { return true; } + public SearchResultCollection search(final String text, final ResultMatcher matcher) { SearchResultCollection quickRes = new SearchResultCollection(); final int request = requestNumber.incrementAndGet(); @@ -158,10 +166,13 @@ public class SearchUICore { if(rm.isCancelled()) { return; } - Thread.sleep(200); // FIXME remove timeout + if(TIMEOUT_BETWEEN_CHARS > 0) { + Thread.sleep(TIMEOUT_BETWEEN_CHARS); + } searchInBackground(phrase, rm); if (!rm.isCancelled()) { sortSearchResults(phrase, rm.getRequestResults()); + filterSearchDuplicateResults(phrase, rm.getRequestResults()); LOG.info(">> Search phrase " + phrase + " " + rm.getRequestResults().size()); SearchResultCollection collection = new SearchResultCollection(rm.getRequestResults(), phrase); @@ -214,14 +225,47 @@ public class SearchUICore { } } + public boolean sameSearchResult(SearchResult r1, SearchResult r2) { + if(r1.location != null && r2.location != null) { + Amenity a1 = null; + if(r1.object instanceof Amenity) { + a1 = (Amenity) r1.object; + } + Amenity a2 = null; + if(r2.object instanceof Amenity) { + a2 = (Amenity) r2.object; + } + if (r1.localeName.equals(r2.localeName)) { + double similarityRadius = 20; + if(a1 != null && a2 != null) { + if(a1.getType().getKeyName().equals("natural") && + a2.getType().getKeyName().equals("natural")) { + similarityRadius = 1000; + } + } + return MapUtils.getDistance(r1.location, r2.location) < similarityRadius; + } + } + return false; + } + public void filterSearchDuplicateResults(SearchPhrase sp, List searchResults) { + Iterator it = searchResults.iterator(); + SearchResult found = null; + while(it.hasNext()) { + SearchResult r = it.next(); + if(found != null && sameSearchResult(found, r)) { + it.remove(); + } else { + found = r; + } + } + } - public void sortSearchResults(SearchPhrase sp, List searchResults) { // sort SearchResult by 1. searchDistance 2. Name final LatLon loc = sp.getLastTokenLocation(); - final net.osmand.Collator clt = OsmAndCollator.primaryCollator(); Collections.sort(searchResults, new Comparator() { @Override @@ -237,7 +281,7 @@ public class SearchUICore { if(st1 != st2) { return Algorithms.compare(st1, st2); } - return clt.compare(o1.localeName, o2.localeName); + return collator.compare(o1.localeName, o2.localeName); } }); } From 132852ae7caeeb71b1a40898a8393b1f8de56087 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 21 Jul 2016 11:37:24 +0200 Subject: [PATCH 2/2] Add method filter duplicate results --- OsmAnd-java/src/net/osmand/search/SearchUICore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OsmAnd-java/src/net/osmand/search/SearchUICore.java b/OsmAnd-java/src/net/osmand/search/SearchUICore.java index 1ad584619b..181baeca70 100644 --- a/OsmAnd-java/src/net/osmand/search/SearchUICore.java +++ b/OsmAnd-java/src/net/osmand/search/SearchUICore.java @@ -240,7 +240,7 @@ public class SearchUICore { if(a1 != null && a2 != null) { if(a1.getType().getKeyName().equals("natural") && a2.getType().getKeyName().equals("natural")) { - similarityRadius = 1000; + similarityRadius = 10000; } } return MapUtils.getDistance(r1.location, r2.location) < similarityRadius;