Last active
December 6, 2017 03:30
-
-
Save mithranalandur/ce170ce287eca2a8f2cfaab6342068df to your computer and use it in GitHub Desktop.
Sample numeric query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uk.co.flax.luwak.*; | |
import uk.co.flax.luwak.matchers.SimpleMatcher; | |
import uk.co.flax.luwak.presearcher.TermFilteredPresearcher; | |
import java.io.IOException; | |
import org.apache.lucene.document.IntPoint; | |
import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
public class Application { | |
public static void main(String[] args){ | |
try { | |
Monitor monitor = new Monitor(new SampleParser("title"), new TermFilteredPresearcher()); | |
MonitorQuery mq = new MonitorQuery("query1", "title:test1"); | |
MonitorQuery mq3 = new MonitorQuery("query3", "upvote:[12 TO 2147483647]"); | |
monitor.update(mq); | |
monitor.update(mq3); | |
InputDocument doc = InputDocument.builder("doc1") | |
.addField("title", "test1", new StandardAnalyzer()) | |
.addField("text", "text", new StandardAnalyzer()) | |
.addField(new IntPoint("upvote", 10)) | |
.build(); | |
Matches<QueryMatch> matches = monitor.match(doc, SimpleMatcher.FACTORY); | |
System.out.print(matches.getPresearcherHits()); | |
monitor.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (UpdateException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.lucene.analysis.Analyzer; | |
import org.apache.lucene.document.IntPoint; | |
import org.apache.lucene.queryparser.classic.CharStream; | |
import org.apache.lucene.queryparser.classic.QueryParser; | |
import org.apache.lucene.queryparser.classic.QueryParserTokenManager; | |
import org.apache.lucene.search.Query; | |
public class Qparser extends QueryParser { | |
public Qparser(String f, Analyzer a) { | |
super(f, a); | |
} | |
protected Qparser(CharStream stream) { | |
super(stream); | |
} | |
protected Qparser(QueryParserTokenManager tm) { | |
super(tm); | |
} | |
@Override | |
protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) { | |
return IntPoint.newRangeQuery("upvote", Integer.parseInt(part1),Integer.parseInt(part2)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.lucene.analysis.Analyzer; | |
import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
import org.apache.lucene.search.Query; | |
import uk.co.flax.luwak.MonitorQueryParser; | |
import java.util.Map; | |
public class SampleParser implements MonitorQueryParser { | |
private final String defaultField; | |
private final Analyzer analyzer; | |
/** | |
* Creates a parser with a given default field and analyzer | |
* @param defaultField the default field | |
* @param analyzer an analyzer to use to analyzer query terms | |
*/ | |
public SampleParser(String defaultField, Analyzer analyzer) { | |
this.defaultField = defaultField; | |
this.analyzer = analyzer; | |
} | |
/** | |
* Creates a parser using a lucene {@link StandardAnalyzer} | |
* @param defaultField the default field | |
*/ | |
public SampleParser(String defaultField) { | |
this(defaultField, new StandardAnalyzer()); | |
} | |
public Query parse(String query, Map<String, String> metadata) throws Exception { | |
return new Qparser(defaultField, analyzer).parse(query); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment