[MediaWiki-commits] [Gerrit] Resolve redirects when doing prefixsearch. - change (apps...wikipedia)

2015-03-25 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: Resolve redirects when doing prefixsearch.
..


Resolve redirects when doing prefixsearch.

Our search experience provides the user with prefix-search and full-text
search results. However, in the case of prefix-search results, if the
result is a Redirect page, it may be more useful to the user to see the
*target* of the redirect, rather than the redirect name itself.  In
addition, the redirect (source) page doesn't come with a thumbnail or
pageterms, which is even less useful to the user.

This patch enables automatic resolving of redirects for prefix-search,
whereby the target of the redirect will be shown, complete with thumbnail
and description. This is accomplished in a slightly roundabout way:
- The search API allows us to specify redirects=true to automatically
  resolve redirects. Simple enough...
- However, when we receive the results, any result that is a redirect
  target will no longer have the index property, which we use to sort
  the results in our list.
- However, the query response also contains a separate redirects array
  that contains a list of all the redirected results, apparently in the
  correct order.
- So first, I take all the results that have an index property, and
  make a list of the holes in the indices. I then fill the holes with
  the results that don't have an index property, in the order that they
  appear in the redirects list!

This can be vastly simplified, of course, if the API includes the index
property with the redirected results. However, this may be difficult to do
on the API side, so this will have to be the solution for now.

Change-Id: I65e281fa83071a4dbf288ecdd7d3bf5ee39a0aee
---
M wikipedia/src/main/java/org/wikipedia/search/TitleSearchTask.java
1 file changed, 37 insertions(+), 1 deletion(-)

Approvals:
  BearND: Looks good to me, approved
  Deskana: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/wikipedia/src/main/java/org/wikipedia/search/TitleSearchTask.java 
b/wikipedia/src/main/java/org/wikipedia/search/TitleSearchTask.java
index 31f22f2..0bdb3bc 100644
--- a/wikipedia/src/main/java/org/wikipedia/search/TitleSearchTask.java
+++ b/wikipedia/src/main/java/org/wikipedia/search/TitleSearchTask.java
@@ -33,6 +33,7 @@
 public RequestBuilder buildRequest(Api api) {
 return api.action(query)
 .param(generator, prefixsearch)
+.param(redirects, true)
 .param(gpssearch, prefix)
 .param(gpsnamespace, 0)
 .param(gpslimit, NUM_RESULTS_PER_QUERY)
@@ -88,13 +89,48 @@
 return new SearchResults(pageTitles, null, suggestion);
 }
 
+// Collect a list of redirect targets, if available.
+// This provides us with the order in which the redirects are listed 
in the results,
+// since the redirected results don't come with an index property.
+ArrayListString redirectTargetList = new ArrayList();
+if (queryResult.has(redirects)) {
+JSONArray redirs = queryResult.getJSONArray(redirects);
+for (int i = 0; i  redirs.length(); i++) {
+redirectTargetList.add(((JSONObject) 
redirs.get(i)).getString(to));
+}
+}
+
+// Create a list of indices, which will be claimed by results that 
have an index.
+// Results that are redirects will not have an index, so we will 
manually place them
+// into any indices that are left over.
+ArrayListInteger pageIndices = new ArrayList();
+for (int i = 0; i  pages.length(); i++) {
+pageIndices.add(i + 1);
+}
+
 // First, put all the page objects into an array
 JSONObject[] pageArray = new JSONObject[pages.length()];
 int pageIndex = 0;
 IteratorString pageIter = pages.keys();
 while (pageIter.hasNext()) {
-pageArray[pageIndex++] = (JSONObject)pages.get(pageIter.next());
+JSONObject page = (JSONObject)pages.get(pageIter.next());
+pageArray[pageIndex++] = page;
+if (page.has(index)) {
+pageIndices.remove((Integer) page.getInt(index));
+}
 }
+// add an index to any results that didn't have one, in the order that 
they appear
+// in the redirect map.
+for (String redirTo : redirectTargetList) {
+for (JSONObject page : pageArray) {
+if (page.getString(title).equals(redirTo)
+ !page.has(index)  pageIndices.size()  0) {
+page.put(index, pageIndices.get(0));
+pageIndices.remove(0);
+}
+}
+}
+
 // now sort the array based on the index property
 Arrays.sort(pageArray, new ComparatorJSONObject() {
 @Override

-- 
To view, 

[MediaWiki-commits] [Gerrit] Resolve redirects when doing prefixsearch. - change (apps...wikipedia)

2015-03-24 Thread Dbrant (Code Review)
Dbrant has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/199285

Change subject: Resolve redirects when doing prefixsearch.
..

Resolve redirects when doing prefixsearch.

Our search experience provides the user with prefix-search and full-text
search results. However, in the case of prefix-search results, if the
result is a Redirect page, it may be more useful to the user to see the
*target* of the redirect, rather than the redirect name itself.  In
addition, the redirect (source) page doesn't come with a thumbnail or
pageterms, which is even less useful to the user.

This patch enables automatic resolving of redirects for prefix-search,
whereby the target of the redirect will be shown, complete with thumbnail
and description. This is accomplished in a slightly roundabout way:
- The search API allows us to specify redirects=true to automatically
  resolve redirects. Simple enough...
- However, when we receive the results, any result that is a redirect
  target will no longer have the index property, which we use to sort
  the results in our list.
- However, the query response also contains a separate redirects array
  that contains a list of all the redirected results, apparently in the
  correct order.
- So first, I take all the results that have an index property, and
  make a list of the holes in the indices. I then fill the holes with
  the results that don't have an index property, in the order that they
  appear in the redirects list!

This can be vastly simplified, of course, if the API includes the index
property with the redirected results. However, this may be difficult to do
on the API side, so this will have to be the solution for now.

Change-Id: I65e281fa83071a4dbf288ecdd7d3bf5ee39a0aee
---
M wikipedia/src/main/java/org/wikipedia/search/TitleSearchTask.java
1 file changed, 37 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/85/199285/1

diff --git a/wikipedia/src/main/java/org/wikipedia/search/TitleSearchTask.java 
b/wikipedia/src/main/java/org/wikipedia/search/TitleSearchTask.java
index 31f22f2..0bdb3bc 100644
--- a/wikipedia/src/main/java/org/wikipedia/search/TitleSearchTask.java
+++ b/wikipedia/src/main/java/org/wikipedia/search/TitleSearchTask.java
@@ -33,6 +33,7 @@
 public RequestBuilder buildRequest(Api api) {
 return api.action(query)
 .param(generator, prefixsearch)
+.param(redirects, true)
 .param(gpssearch, prefix)
 .param(gpsnamespace, 0)
 .param(gpslimit, NUM_RESULTS_PER_QUERY)
@@ -88,13 +89,48 @@
 return new SearchResults(pageTitles, null, suggestion);
 }
 
+// Collect a list of redirect targets, if available.
+// This provides us with the order in which the redirects are listed 
in the results,
+// since the redirected results don't come with an index property.
+ArrayListString redirectTargetList = new ArrayList();
+if (queryResult.has(redirects)) {
+JSONArray redirs = queryResult.getJSONArray(redirects);
+for (int i = 0; i  redirs.length(); i++) {
+redirectTargetList.add(((JSONObject) 
redirs.get(i)).getString(to));
+}
+}
+
+// Create a list of indices, which will be claimed by results that 
have an index.
+// Results that are redirects will not have an index, so we will 
manually place them
+// into any indices that are left over.
+ArrayListInteger pageIndices = new ArrayList();
+for (int i = 0; i  pages.length(); i++) {
+pageIndices.add(i + 1);
+}
+
 // First, put all the page objects into an array
 JSONObject[] pageArray = new JSONObject[pages.length()];
 int pageIndex = 0;
 IteratorString pageIter = pages.keys();
 while (pageIter.hasNext()) {
-pageArray[pageIndex++] = (JSONObject)pages.get(pageIter.next());
+JSONObject page = (JSONObject)pages.get(pageIter.next());
+pageArray[pageIndex++] = page;
+if (page.has(index)) {
+pageIndices.remove((Integer) page.getInt(index));
+}
 }
+// add an index to any results that didn't have one, in the order that 
they appear
+// in the redirect map.
+for (String redirTo : redirectTargetList) {
+for (JSONObject page : pageArray) {
+if (page.getString(title).equals(redirTo)
+ !page.has(index)  pageIndices.size()  0) {
+page.put(index, pageIndices.get(0));
+pageIndices.remove(0);
+}
+}
+}
+
 // now sort the array based on the index property
 Arrays.sort(pageArray, new ComparatorJSONObject() {
 @Override

-- 
To view, visit