added search suggestions in XML format. This also supports CORS

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7293 6c8d7289-2bf4-0310-a012-ef5d649a1542
This commit is contained in:
orbiter 2010-11-03 09:20:36 +00:00
parent f5324b27f2
commit 3e303db1fa
4 changed files with 48 additions and 10 deletions

View File

@ -18,7 +18,8 @@
:: ::
<Url type="text/html" method="GET" template="http://#[thisaddress]#/compare_yacy.html?verify=true&amp;resource=global&amp;nav=all&amp;query={searchTerms}" /> <Url type="text/html" method="GET" template="http://#[thisaddress]#/compare_yacy.html?verify=true&amp;resource=global&amp;nav=all&amp;query={searchTerms}" />
#(/compareyacy)# #(/compareyacy)#
<Url type="application/x-suggestions+json" template="http://#[thisaddress]#/suggest?query={searchTerms}"/> <Url type="application/x-suggestions+json" template="http://#[thisaddress]#/suggest.json?query={searchTerms}"/>
<Url type="application/x-suggestions+xml" template="http://#[thisaddress]#/suggest.xml?query={searchTerms}"/>
<!-- syntax according to http://www.loc.gov/standards/sru/. Set verify=true to get snippets in the search results --> <!-- syntax according to http://www.loc.gov/standards/sru/. Set verify=true to get snippets in the search results -->
<Developer>See http://developer.berlios.de/projects/yacy/</Developer> <Developer>See http://developer.berlios.de/projects/yacy/</Developer>
<Query role="example" searchTerms="yacy+open+source" /> <Query role="example" searchTerms="yacy+open+source" />

View File

@ -20,7 +20,9 @@
import java.util.Iterator; import java.util.Iterator;
import net.yacy.cora.protocol.HeaderFramework;
import net.yacy.cora.protocol.RequestHeader; import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.protocol.ResponseHeader;
import de.anomic.data.DidYouMean; import de.anomic.data.DidYouMean;
import de.anomic.search.Segment; import de.anomic.search.Segment;
@ -28,12 +30,20 @@ import de.anomic.search.Segments;
import de.anomic.search.Switchboard; import de.anomic.search.Switchboard;
import de.anomic.server.serverObjects; import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch; import de.anomic.server.serverSwitch;
import de.anomic.server.servletProperties;
/** /**
* for json format:
* implementation of the opensearch suggestion extension, see * implementation of the opensearch suggestion extension, see
* http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1 * http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1
* or * or
* https://wiki.mozilla.org/Search_Service/Suggestions * https://wiki.mozilla.org/Search_Service/Suggestions
*
* for xml format:
* see Microsoft Search Suggestion Format
* http://msdn.microsoft.com/en-us/library/cc848863%28VS.85%29.aspx
* and
* http://msdn.microsoft.com/en-us/library/cc848862%28v=VS.85%29.aspx
*/ */
public class suggest { public class suggest {
@ -41,7 +51,11 @@ public class suggest {
public static serverObjects respond(final RequestHeader header, final serverObjects post, final serverSwitch env) { public static serverObjects respond(final RequestHeader header, final serverObjects post, final serverSwitch env) {
final Switchboard sb = (Switchboard) env; final Switchboard sb = (Switchboard) env;
final serverObjects prop = new serverObjects(); final servletProperties prop = new servletProperties();
final String ext = header.get("EXT", "");
final boolean json = ext.equals("json");
final boolean xml = ext.equals("xml");
// get query // get query
String originalquerystring = (post == null) ? "" : post.get("query", post.get("q", "")).trim(); String originalquerystring = (post == null) ? "" : post.get("query", post.get("q", "")).trim();
@ -63,17 +77,29 @@ public class suggest {
DidYouMean didYouMean = new DidYouMean(indexSegment.termIndex(), querystring); DidYouMean didYouMean = new DidYouMean(indexSegment.termIndex(), querystring);
Iterator<String> meanIt = didYouMean.getSuggestions(timeout, count).iterator(); Iterator<String> meanIt = didYouMean.getSuggestions(timeout, count).iterator();
int meanCount = 0; int c = 0;
String suggestion; String suggestion;
StringBuilder suggestions = new StringBuilder(120); //[#[query]#,[#{suggestions}##[text]##(eol)#,::#(/eol)##{/suggestions}#]]
while (meanCount < meanMax && meanIt.hasNext()) { while (c < meanMax && meanIt.hasNext()) {
suggestion = meanIt.next(); suggestion = meanIt.next();
suggestions.append(',').append('"').append(suggestion).append('"'); if (json) prop.putJSON("suggestions_" + c + "_text", suggestion);
meanCount++; else if (xml) prop.putXML("suggestions_" + c + "_text", suggestion);
else prop.putHTML("suggestions_" + c + "_text", suggestion);
prop.put("suggestions_" + c + "_eol", 0);
c++;
} }
if (c > 0) prop.put("suggestions_" + (c - 1) + "_eol", 1);
prop.put("suggestions", c);
if (json) prop.putJSON("query", originalquerystring);
else if (xml) prop.putXML("query", originalquerystring);
else prop.putHTML("query", originalquerystring);
prop.put("query", '"' + originalquerystring + '"'); // Adding CORS Access header for xml output
prop.put("suggestions", suggestions.length() > 0 ? suggestions.substring(1) : ""); if (xml) {
final ResponseHeader outgoingHeader = new ResponseHeader();
outgoingHeader.addHeader(HeaderFramework.CORS_ALLOW_ORIGIN, "*");
prop.setOutgoingHeader(outgoingHeader);
}
// return rewrite properties // return rewrite properties
return prop; return prop;

View File

@ -1 +1 @@
[#[query]#,[#[suggestions]#]] ["#[query]#",[#{suggestions}#"#[text]#"#(eol)#,::#(/eol)##{/suggestions}#]]

11
htroot/suggest.xml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0"?>
<SearchSuggestion xmlns ="http://schemas.microsoft.com/Search/2008/suggestions">
<Query>#[query]#</Query>
<Section>
#{suggestions}#
<Item>
<Text>#[text]#</Text>
</Item>
#{/suggestions}#
</Section>
</SearchSuggestion>