Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #70 from sparkica/fix_search_term

suggest-term: search for terms returns null pointer exception
  • Loading branch information...
commit 684b4ecf35859c7d090fad9ebeb15e3295965383 2 parents 7f49796 + a4a6f0d
@fadmaa authored
View
4 .classpath
@@ -26,8 +26,6 @@
<classpathentry kind="lib" path="/OpenRefine/main/webapp/WEB-INF/lib/opencsv-2.2.jar"/>
<classpathentry kind="lib" path="/OpenRefine/main/webapp/WEB-INF/lib/rhino-1.7R2.jar"/>
<classpathentry kind="lib" path="/OpenRefine/main/webapp/WEB-INF/lib/secondstring-20100303.jar"/>
- <classpathentry kind="lib" path="/Users/fadi/development/Java/orefine/OpenRefine/main/webapp/WEB-INF/lib/signpost-commonshttp4-1.2.1.2.jar"/>
- <classpathentry kind="lib" path="/Users/fadi/development/Java/orefine/OpenRefine/main/webapp/WEB-INF/lib/signpost-core-1.2.1.2.jar"/>
<classpathentry kind="lib" path="/OpenRefine/main/webapp/WEB-INF/lib/slf4j-log4j12-1.5.6.jar"/>
<classpathentry kind="lib" path="/OpenRefine/main/webapp/WEB-INF/lib/velocity-1.5.jar"/>
<classpathentry kind="lib" path="/OpenRefine/main/webapp/WEB-INF/lib/vicino-1.1.jar"/>
@@ -61,5 +59,7 @@
<classpathentry kind="lib" path="/OpenRefine/main/webapp/WEB-INF/lib/poi-ooxml-3.8-20120326.jar"/>
<classpathentry kind="lib" path="/OpenRefine/main/webapp/WEB-INF/lib/poi-ooxml-schemas-3.8-20120326.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/OpenRefine"/>
+ <classpathentry kind="lib" path="/OpenRefine/main/webapp/WEB-INF/lib/signpost-commonshttp4-1.2.1.2.jar" sourcepath="/OpenRefine/main/webapp/WEB-INF/lib-src/signpost-commonshttp4-1.2.1.2-sources.jar"/>
+ <classpathentry kind="lib" path="/OpenRefine/main/webapp/WEB-INF/lib-src/signpost-core-1.2.1.2-sources.jar"/>
<classpathentry kind="output" path="module/MOD-INF/classes"/>
</classpath>
View
198 module/scripts/suggestterm.suggest.js
@@ -4,82 +4,136 @@
}
$.suggest("suggestterm",
- $.extend(true, {}, $.suggest.suggest.prototype, {
- create_item: function(data,response_data) {
- var css = this.options.css;
-
- var li = $("<li>").addClass(css.item);
- var name = $("<div>").addClass(css.item_name)
+ $.extend(
+ true,
+ {},
+ $.suggest.suggest.prototype,
+ {
+ create_item: function(data,response_data) {
+ var css = this.options.css;
+
+ var li = $("<li>").addClass(css.item);
+ var name = $("<div>").addClass(css.item_name)
+ .append($("<label>")
+ .append($.suggest.strongify(data.name,response_data.prefix)));
+ // this converts html escaped strings like "&amp;"
+ // back to "&"
+ data.name = name.text();
+ li.append(name);
+ name.prepend($("<div>").addClass(css.item_type).text(data.id));
+ //TODO very smelly hack to disable cache
+ $.suggest.cache = {};
+ return li;
+ },
+
+ request: function(value, cursor) {
+ var self = this,
+ o = this.options;
+
+ var data = {};
+ var query = value;
+ data[o.query_param_name] = query;
+
+ clearTimeout(this.request.timeout);
+ data["prefix"] = query;
+ data["type_strict"] = "classes";
+ data["type"] = theProject.id;
+
+ var url = o.service_url + o.service_path + "?" + $.param(data, true);
+ var ajax_options = {
+ url: o.service_url + o.service_path,
+ data: data,
+ traditional: true,
+ error: function(xhr) {
+ self.status_error();
+ self.trackEvent(self.name, "request", "error", {
+ url: this.url,
+ response: xhr ? xhr.responseText : ''
+ });
+ self.input.trigger("fb-error", Array.prototype.slice.call(arguments));
+ },
+ complete: function(xhr) {
+ if (xhr) {
+ self.trackEvent(self.name, "request", "tid",
+ xhr.getResponseHeader("X-Metaweb-TID"));
+ }
+ },
+ success: function(data) {
+ $.suggest.cache[url] = data;
+ data.prefix = value; // keep track of prefix to match up response with input value
+ self.response(data, cursor ? cursor : -1);
+ },
+ dataType: "json",
+ cache: true
+ };
+ this.request.timeout = setTimeout(function() {
+ $.ajax(ajax_options);
+ }, o.xhr_delay);
- .append($("<label>")
- .append($.suggest.strongify(data.name,response_data.prefix)));
- // this converts html escaped strings like "&amp;"
- // back to "&"
- data.name = name.text();
- li.append(name);
- name.prepend($("<div>").addClass(css.item_type).text(data.id));
- //TODO very smelly hack to disable cache
- $.suggest.cache = {};
- return li;
- },
-
- flyout_request:function(data){
-
- var self = this;
-
- var o = this.options,
- sug_data = this.flyoutpane.data("data.suggest");
- if (sug_data && data.id === sug_data.id) {
- if (!this.flyoutpane.is(":visible")) {
- var s = this.get_selected();
- this.flyout_position(s);
- this.flyoutpane.show();
- this.input.trigger("fb-flyoutpane-show", this);
- }
- return;
- }
-
- // check $.suggest.flyout.cache
- var cached = $.suggest.flyout.cache[data.id];
- if (cached) {
- this.flyout_response(cached);
- return;
- }
-
- clearTimeout(this.flyout_request.timeout);
- this.flyout_request.timeout =
- setTimeout(function(){self.flyout_response(data);}, o.xhr_delay);
-
- },
-
- flyout_response:function(data){
- var o = this.options,
- p = this.pane,
- s = this.get_selected() || [];
- if (p.is(":visible") && s.length) {
- var sug_data = s.data("data.suggest");
- if (sug_data && data.id === sug_data.id) {
- this.flyoutpane.html('<div class="fbs-flyout-content">' + data.description + '</div>');
- this.flyout_position(s);
- this.flyoutpane.show()
- .data("data.suggest", sug_data);
- this.input.trigger("fb-flyoutpane-show", this);
- }
- }
- }
+ },
+ flyout_request:function(data){
+ var self = this;
+
+ var o = this.options,
+ sug_data = this.flyoutpane.data("data.suggest");
+ if (sug_data && data.id === sug_data.id) {
+ if (!this.flyoutpane.is(":visible")) {
+ var s = this.get_selected();
+ this.flyout_position(s);
+ this.flyoutpane.show();
+ this.input.trigger("fb-flyoutpane-show", this);
+ }
+ return;
+ }
+
+ // check $.suggest.flyout.cache
+ var cached = $.suggest.flyout.cache[data.id];
+ if (cached) {
+ this.flyout_response(cached);
+ return;
+ }
+
+ clearTimeout(this.flyout_request.timeout);
+ this.flyout_request.timeout =
+ setTimeout(function(){self.flyout_response(data);}, o.xhr_delay);
+
+ },
+
+ flyout_response:function(data){
+ var o = this.options,
+ p = this.pane,
+ s = this.get_selected() || [];
+ if (p.is(":visible") && s.length) {
+ var sug_data = s.data("data.suggest");
+ if (sug_data && data.id === sug_data.id) {
+ this.flyoutpane.html('<div class="fbs-flyout-content">' + data.description + '</div>');
+ this.flyout_position(s);
+ this.flyoutpane.show()
+ .data("data.suggest", sug_data);
+ this.input.trigger("fb-flyoutpane-show", this);
+ }
+ }
+ }
}));
-
-
$.extend($.suggest.suggestterm, {
- defaults: $.extend(true, {}, $.suggest.suggest.defaults, {
- service_url: "",
- service_path: "command/rdf-extension/suggest-term",
- type_strict:"classes",
- suggest_new:"Add it",
- cache:false,
-// soft:true,
- nomatch:'<em class="fbs-nomatch-text">No suggested matches. (Shift + Enter) to add it</em>'
+ defaults: $.extend(
+ true,
+ {},
+ $.suggest.suggest.defaults,
+ {
+ service_url: "",
+ service_path: "command/rdf-extension/suggest-term",
+ flyout_service_path: "command/rdf-extension/suggest-term",
+ type_strict:"classes",
+ suggest_new:"Add it",
+ cache:false,
+ // soft:true,
+ nomatch: {
+ title: 'No suggested matches. (Shift + Enter) to add it',
+ heading: null,
+ tips: null
+ }
})
});
View
5 src/org/deri/grefine/rdf/commands/SuggestTermCommand.java
@@ -38,11 +38,14 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
String projectId = request.getParameter("type");
response.setHeader("Content-Type", "application/json");
- JSONWriter writer = new JSONWriter(response.getWriter());
+ JSONWriter writer = new JSONWriter(response.getWriter());
String type = request.getParameter("type_strict");
String query = request.getParameter("prefix");
+
+
+
try{
writer.object();
View
56 src/org/deri/grefine/rdf/vocab/imp/VocabularySearcher.java
@@ -9,14 +9,17 @@
import java.util.List;
import java.util.Set;
+import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.TokenStream;
-import org.apache.lucene.analysis.tokenattributes.TermAttribute;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.CorruptIndexException;
+import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.BooleanClause.Occur;
@@ -28,6 +31,7 @@
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
+import org.apache.lucene.util.Version;
import org.deri.grefine.rdf.vocab.IVocabularySearcher;
import org.deri.grefine.rdf.vocab.PrefixExistException;
import org.deri.grefine.rdf.vocab.RDFNode;
@@ -56,14 +60,17 @@
private IndexSearcher searcher;
private Directory _directory;
-
+
public VocabularySearcher(File dir) throws IOException {
- _directory = new SimpleFSDirectory(new File(dir, "luceneIndex"));
- writer = new IndexWriter(_directory, new SimpleAnalyzer(),
- IndexWriter.MaxFieldLength.LIMITED);
- writer.commit();
- searcher = new IndexSearcher(_directory);
- }
+ _directory = new SimpleFSDirectory(new File(dir, "luceneIndex"));
+ Analyzer a = new SimpleAnalyzer(Version.LUCENE_36);
+ IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36,a);
+
+ writer = new IndexWriter(_directory,conf);
+ writer.commit();
+ IndexReader r = IndexReader.open(_directory);
+ searcher = new IndexSearcher(r);
+ }
@Override
public void importAndIndexVocabulary(String name, String uri, String fetchUrl,VocabularyImporter importer)throws VocabularyImportException, VocabularyIndexException,PrefixExistException, CorruptIndexException, IOException {
@@ -93,7 +100,7 @@ public void importAndIndexVocabulary(String name, String uri, Repository reposit
public List<SearchResultItem> searchClasses(String str, String projectId)
throws ParseException, IOException {
Query query = prepareQuery(str, CLASS_TYPE, projectId);
- TopDocs docs = searcher.search(query, getMaxDoc());
+ TopDocs docs = searcher.search(query, getMaxDoc());
return prepareSearchResults(docs);
}
@@ -130,7 +137,7 @@ public void update() throws CorruptIndexException, IOException {
// TODO this shouldn't be required but it is not working without it...
// check
searcher.close();
- searcher = new IndexSearcher(_directory);
+ searcher = new IndexSearcher(IndexReader.open(_directory));
}
@Override
@@ -227,7 +234,7 @@ private Query prepareQuery(String s, String type, String projectId)
q.add(q2, Occur.MUST);
if (s != null && s.trim().length() > 0) {
- SimpleAnalyzer analyzer = new SimpleAnalyzer();
+ SimpleAnalyzer analyzer = new SimpleAnalyzer(Version.LUCENE_36);
if (s.indexOf(":") == -1) {
// the query we need:
// "projectId":projectId AND "type":type AND ("prefix":s* OR
@@ -239,12 +246,13 @@ private Query prepareQuery(String s, String type, String projectId)
TokenStream stream = analyzer.tokenStream("localPart",
new StringReader(s));
// get the TermAttribute from the TokenStream
- TermAttribute termAtt = (TermAttribute) stream
- .addAttribute(TermAttribute.class);
+ CharTermAttribute termAtt = (CharTermAttribute) stream
+ .addAttribute(CharTermAttribute.class);
stream.reset();
+
while (stream.incrementToken()) {
- String tmp = termAtt.term() + "*";
+ String tmp = termAtt.toString() + "*";
q3.add(new WildcardQuery(new Term("localPart", tmp)),
Occur.SHOULD);
}
@@ -254,12 +262,12 @@ private Query prepareQuery(String s, String type, String projectId)
stream = analyzer.tokenStream("description",
new StringReader(s));
// get the TermAttribute from the TokenStream
- termAtt = (TermAttribute) stream
- .addAttribute(TermAttribute.class);
+ termAtt = (CharTermAttribute) stream
+ .addAttribute(CharTermAttribute.class);
stream.reset();
while (stream.incrementToken()) {
- String tmp = termAtt.term() + "*";
+ String tmp = termAtt.toString() + "*";
q3.add(new WildcardQuery(new Term("description", tmp)),
Occur.SHOULD);
}
@@ -268,12 +276,12 @@ private Query prepareQuery(String s, String type, String projectId)
stream = analyzer.tokenStream("label", new StringReader(s));
// get the TermAttribute from the TokenStream
- termAtt = (TermAttribute) stream
- .addAttribute(TermAttribute.class);
+ termAtt = (CharTermAttribute) stream
+ .addAttribute(CharTermAttribute.class);
stream.reset();
while (stream.incrementToken()) {
- String tmp = termAtt.term() + "*";
+ String tmp = termAtt.toString() + "*";
q3.add(new WildcardQuery(new Term("label", tmp)),
Occur.SHOULD);
}
@@ -297,14 +305,14 @@ private Query prepareQuery(String s, String type, String projectId)
TokenStream stream = analyzer.tokenStream("localPart",
new StringReader(p2));
// get the TermAttribute from the TokenStream
- TermAttribute termAtt = (TermAttribute) stream
- .addAttribute(TermAttribute.class);
+ CharTermAttribute termAtt = (CharTermAttribute) stream
+ .addAttribute(CharTermAttribute.class);
stream.reset();
if (!p2.isEmpty()) {
while (stream.incrementToken()) {
- q4.add(new WildcardQuery(new Term("localPart", termAtt
- .term() + "*")), Occur.SHOULD);
+ q4.add(new WildcardQuery(new Term("localPart", termAtt.toString()
+ + "*")), Occur.SHOULD);
}
}
stream.close();
Please sign in to comment.
Something went wrong with that request. Please try again.