Skip to content

Instantly share code, notes, and snippets.

@fbricon
Created February 20, 2015 19:28
Show Gist options
  • Save fbricon/ae0587eee2df081e6057 to your computer and use it in GitHub Desktop.
Save fbricon/ae0587eee2df081e6057 to your computer and use it in GitHub Desktop.
SearchiskoEngine.java
/*************************************************************************************
* Copyright (c) 2015 Red Hat, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* JBoss by Red Hat - Initial implementation.
************************************************************************************/
package org.jboss.tools.help.internal.search;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.help.search.ISearchEngine;
import org.eclipse.help.search.ISearchEngineResult;
import org.eclipse.help.search.ISearchEngineResultCollector;
import org.eclipse.help.search.ISearchScope;
import org.jboss.dmr.ModelNode;
import org.jboss.tools.common.util.HttpUtil;
import org.jboss.tools.help.internal.JBossHelpActivator;
import org.jboss.tools.help.internal.search.SearchiskoEngineScopeFactory.Scope;
/**
* Performs search queries against a <a href="https://github.com/searchisko/searchisko">Searchisko</a> instance.
* By default, queries are performed against <a href="http://dcp.jboss.org/v1/rest/">http://dcp.jboss.org/v1/rest/</a>.
*
* @author Fred Bricon
*/
public class SearchiskoEngine implements ISearchEngine {
@Override
public void run(String query, ISearchScope scope, ISearchEngineResultCollector collector, IProgressMonitor monitor) {
try {
String searchQuery = getSearchUrl(((Scope)scope).getURLTemplate(), query);
Collection<ISearchEngineResult> results = performQuery(searchQuery, monitor);
collector.accept(results.toArray(new ISearchEngineResult[results.size()]));
} catch (Exception e) {
IStatus error = new Status(IStatus.ERROR, JBossHelpActivator.PLUGIN_ID, e.getLocalizedMessage());
JBossHelpActivator.getDefault().getLog().log(error);
collector.error(error);
}
}
protected Collection<ISearchEngineResult> performQuery(String searchQuery, IProgressMonitor monitor)
throws IOException {
List<ISearchEngineResult> results = null;
try (InputStream is = HttpUtil.getInputStreamFromUrlByGetMethod(searchQuery)) {
ModelNode searchResult = ModelNode.fromJSONStream(is);
if (searchResult.isDefined()) {
ModelNode hitsWrapper = searchResult.get("hits");
if (hitsWrapper.isDefined()) {
ModelNode hitsNode = hitsWrapper.get("hits");
if (hitsNode.isDefined()) {
List<ModelNode> hits = hitsNode.asList();
results = new ArrayList<>(hits.size());
for (ModelNode hit : hits) {
if (monitor.isCanceled()) {
return results;
}
ISearchEngineResult result = SearchiskoResultBuilder.create(hit);
if (result != null) {
results.add(result);
}
}
}
}
}
}
return results == null ? Collections.<ISearchEngineResult> emptyList() : results;
}
protected String getSearchUrl(String urlTemplate, String query) {
String eQuery;
try {
eQuery = URLEncoder.encode(query, "UTF-8"); //$NON-NLS-1$
} catch (UnsupportedEncodingException e) {
eQuery = query;
}
//Filter categories
//searchQuery.append("+AND+sys_type%3A(blogpost+article+webpage+solution)");
//searchQuery.append("&size=100");
return urlTemplate.replace("{expression}", eQuery);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment