Parametrize search field
This commit is contained in:
parent
3b21bd113e
commit
f1519b55c2
|
@ -3,6 +3,8 @@ package org.RI.P2;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.swing.table.DefaultTableModel;
|
import javax.swing.table.DefaultTableModel;
|
||||||
|
|
||||||
|
@ -22,10 +24,12 @@ public class Searcher extends javax.swing.JFrame {
|
||||||
IndexSearcher searcher;
|
IndexSearcher searcher;
|
||||||
String dataPath;
|
String dataPath;
|
||||||
String indexPath;
|
String indexPath;
|
||||||
|
String field;
|
||||||
|
|
||||||
Searcher(String dataPath, String indexPath) throws IOException {
|
Searcher(String dataPath, String indexPath, String field) throws IOException {
|
||||||
this.dataPath = dataPath;
|
this.dataPath = dataPath;
|
||||||
this.indexPath = indexPath;
|
this.indexPath = indexPath;
|
||||||
|
this.field = field;
|
||||||
searcher = createIndexSearcher();
|
searcher = createIndexSearcher();
|
||||||
initComponents();
|
initComponents();
|
||||||
}
|
}
|
||||||
|
@ -86,7 +90,7 @@ public class Searcher extends javax.swing.JFrame {
|
||||||
|
|
||||||
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
|
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
|
||||||
throws IOException, org.apache.lucene.queryparser.classic.ParseException {
|
throws IOException, org.apache.lucene.queryparser.classic.ParseException {
|
||||||
searchFiles(jTextField1.getText());
|
searchFiles(jTextField1.getText(), field);
|
||||||
}
|
}
|
||||||
|
|
||||||
IndexSearcher createIndexSearcher() throws IOException {
|
IndexSearcher createIndexSearcher() throws IOException {
|
||||||
|
@ -96,9 +100,9 @@ public class Searcher extends javax.swing.JFrame {
|
||||||
return searcher;
|
return searcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
TopDocs queryIndex(String queryString, int resultNumber)
|
TopDocs queryIndex(String queryString, String field, int resultNumber)
|
||||||
throws IOException, org.apache.lucene.queryparser.classic.ParseException {
|
throws IOException, org.apache.lucene.queryparser.classic.ParseException {
|
||||||
Query query = new QueryParser("abstract", new StandardAnalyzer()).parse(queryString);
|
Query query = new QueryParser(field, new StandardAnalyzer()).parse(queryString);
|
||||||
TopDocs topDocs = searcher.search(query, resultNumber);
|
TopDocs topDocs = searcher.search(query, resultNumber);
|
||||||
return topDocs;
|
return topDocs;
|
||||||
}
|
}
|
||||||
|
@ -112,12 +116,31 @@ public class Searcher extends javax.swing.JFrame {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void searchFiles(String query) throws IOException, org.apache.lucene.queryparser.classic.ParseException {
|
void searchFiles(String query, String field)
|
||||||
|
throws IOException, org.apache.lucene.queryparser.classic.ParseException {
|
||||||
int resultNumber = 20;
|
int resultNumber = 20;
|
||||||
TopDocs results = queryIndex(query, resultNumber);
|
TopDocs results = queryIndex(query, field, resultNumber);
|
||||||
showResults(results);
|
showResults(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void validateField(String fieldContent) {
|
||||||
|
List<String> availableOptions = new ArrayList<>();
|
||||||
|
availableOptions.add("title");
|
||||||
|
availableOptions.add("authors");
|
||||||
|
availableOptions.add("institutions");
|
||||||
|
availableOptions.add("abstract");
|
||||||
|
availableOptions.add("emails");
|
||||||
|
if (!availableOptions.contains(fieldContent)) {
|
||||||
|
System.out.println("Wrong field name. Available options:");
|
||||||
|
System.out.println("authors");
|
||||||
|
System.out.println("title");
|
||||||
|
System.out.println("abstract");
|
||||||
|
System.out.println("institutions");
|
||||||
|
System.out.println("emails");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void usage() {
|
private static void usage() {
|
||||||
System.out.println("Usage: Searcher <directory>");
|
System.out.println("Usage: Searcher <directory>");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
|
@ -125,11 +148,13 @@ public class Searcher extends javax.swing.JFrame {
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
throws IOException, ParseException, org.apache.lucene.queryparser.classic.ParseException {
|
throws IOException, ParseException, org.apache.lucene.queryparser.classic.ParseException {
|
||||||
if (args.length != 1) {
|
if (args.length != 2) {
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
String dataDirectory = args[0];
|
String dataDirectory = args[0];
|
||||||
String indexDirectory = ".index";
|
String indexDirectory = ".index";
|
||||||
|
String searchField = args[1];
|
||||||
|
validateField(searchField);
|
||||||
Indexer indexer = new Indexer(dataDirectory, indexDirectory);
|
Indexer indexer = new Indexer(dataDirectory, indexDirectory);
|
||||||
indexer.populateIndex();
|
indexer.populateIndex();
|
||||||
|
|
||||||
|
@ -154,7 +179,7 @@ public class Searcher extends javax.swing.JFrame {
|
||||||
java.awt.EventQueue.invokeLater(new Runnable() {
|
java.awt.EventQueue.invokeLater(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
new Searcher(args[0], indexDirectory).setVisible(true);
|
new Searcher(dataDirectory, indexDirectory, searchField).setVisible(true);
|
||||||
} catch (IOException exp) {
|
} catch (IOException exp) {
|
||||||
System.err.println(exp);
|
System.err.println(exp);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue