From 39a42f7fd541f400a4d84a9302a35da0f6fe0171 Mon Sep 17 00:00:00 2001 From: Aakash Muttineni Date: Tue, 12 Apr 2016 11:49:16 +0530 Subject: [PATCH] Java Examples https://trac.xapian.org/ticket/716 Converting Python examples to Java Examples converted: * index1.py * search1.py * delete1.py ToDo: * index_ranges.py * index_filters.py * index_sorting.py * search_filters.py * search_ranges.py * search_sorting.py --- code/java/delete1.java | 34 ++++++++++++ code/java/delete1.java.out | 0 code/java/index1.java | 103 +++++++++++++++++++++++++++++++++++++ code/java/search1.java | 70 +++++++++++++++++++++++++ code/java/search1.java.out | 2 + 5 files changed, 209 insertions(+) create mode 100755 code/java/delete1.java create mode 100644 code/java/delete1.java.out create mode 100755 code/java/index1.java create mode 100755 code/java/search1.java create mode 100644 code/java/search1.java.out diff --git a/code/java/delete1.java b/code/java/delete1.java new file mode 100755 index 00000000..01e0330e --- /dev/null +++ b/code/java/delete1.java @@ -0,0 +1,34 @@ +package code.java; + +import org.xapian.WritableDatabase; +import org.xapian.XapianConstants; + +public class delete1 { + + // Command line args - dbpath identifiers... + public static void main(String[] args) + { + if(args.length < 2) + { + System.out.println("Insufficient number of arguments (should be dbpath identifiers...)"); + return; + } + deleteDocs(args[0], args); + } + + public static void deleteDocs(String dbpath, String[] identifierArgs) + { + // Open the database we're going to be deleting from. + WritableDatabase db = new WritableDatabase(dbpath, XapianConstants.DB_OPEN); + + // Identifiers start from index 1 + for(int i=1; i < identifierArgs.length; i++) + { + String idterm = "Q" + identifierArgs[i]; + db.deleteDocument(idterm); + } + + // Commit to delete documents from disk + db.commit(); + } +} diff --git a/code/java/delete1.java.out b/code/java/delete1.java.out new file mode 100644 index 00000000..e69de29b diff --git a/code/java/index1.java b/code/java/index1.java new file mode 100755 index 00000000..5da7805c --- /dev/null +++ b/code/java/index1.java @@ -0,0 +1,103 @@ +package code.java; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.xapian.Document; +import org.xapian.Stem; +import org.xapian.TermGenerator; +import org.xapian.WritableDatabase; +import org.xapian.XapianConstants; +import org.xapian.XapianJNI; + +public class index1 { + + // Command line args - dbpath datapath + public static void main(String[] args) + { + if(args.length < 2) + { + System.out.println("Insufficient number of arguments (should be dbpath datapath)"); + return; + } + index(args[0], args[1]); + } + + public static void index(String dbpath, String datapath) + { + // Create or open the database we're goign to be writing to. + WritableDatabase db = new WritableDatabase(dbpath, XapianConstants.DB_CREATE_OR_OPEN); + // Set up a TermGenerator that we'll use in indexing. + TermGenerator termGenerator = new TermGenerator(); + termGenerator.setStemmer(new Stem("en")); + + //Parsing the CSV input file + Scanner csvScanner,lineScanner; + csvScanner = lineScanner = null; + + try { + File csv = new File(datapath); + csvScanner = new Scanner(csv); + } catch (FileNotFoundException ex) { + Logger.getLogger(index1.class.getName()).log(Level.SEVERE, null, ex); + } + + // Ignoring first line (contains descriptors) + csvScanner.nextLine(); + + while(csvScanner.hasNextLine()) + { + String currentLine = csvScanner.nextLine(); + lineScanner = new Scanner(currentLine); + lineScanner.useDelimiter(","); + + /* Parsing each line for identifier, title, and description */ + + // Identifier is the first comma seperated value (according to CSV file) + String identifier = lineScanner.next(); + + // Title is third comma seperated value + lineScanner.next(); + String title = lineScanner.next(); + + // Description is ninth comma sperated value + for(int i=0;i<5;i++) + lineScanner.next(); + String description = lineScanner.next(); + + /* Finished Parsing line */ + + // We make a document and tell the term generator to use this. + Document doc = new Document(); + termGenerator.setDocument(doc); + + // Index each field with a suitable prefix. + termGenerator.indexText(title, 1, "S"); + termGenerator.indexText(description, 1, "XD"); + + // Index fields without prefixes for general search. + termGenerator.indexText(title); + termGenerator.increaseTermpos(); + termGenerator.indexText(description); + + // Store all fields for display purposes + doc.setData(currentLine); + doc.addValue(0, title); + + // We use the identifier to ensure each object ends up in the + // database only once no matter how many times we run the + // indexer. + String idterm = "Q"+identifier; + doc.addBooleanTerm(idterm); + db.replaceDocument(idterm, doc); + } + + // Commit to write documents to disk + db.commit(); + + lineScanner.close(); + csvScanner.close(); + } +} diff --git a/code/java/search1.java b/code/java/search1.java new file mode 100755 index 00000000..2288e2d1 --- /dev/null +++ b/code/java/search1.java @@ -0,0 +1,70 @@ +package code.java; + +import org.xapian.Database; +import org.xapian.Document; +import org.xapian.Enquire; +import org.xapian.MSet; +import org.xapian.MSetIterator; +import org.xapian.Query; +import org.xapian.QueryParser; +import org.xapian.Stem; + +public class search1 { + + // Command line args - dbpath querystring + public static void main(String[] args) + { + if(args.length < 2) + { + System.out.println("Insufficient number of arguments (should be dbpath querystring)"); + return; + } + search(args[0], args[1]); + } + + public static void search(String dbpath, String queryString) + { + search(dbpath, queryString, 0, 10); + } + + public static void search(String dbpath, String queryString, int offset, int pagesize) + { + // offset - defines starting point within result set + // pagesize - defines number of records to retrieve + + // Open the databse we're going to search. + Database db = new Database(dbpath); + + // Set up a QueryParser with a stemmer and suitable prefixes + QueryParser queryParser = new QueryParser(); + queryParser.setStemmer(new Stem("en")); + queryParser.setStemmingStrategy(QueryParser.stem_strategy.STEM_SOME); + // Start of prefix configuration. + queryParser.addPrefix("title", "S"); + queryParser.addPrefix("description", "XD"); + // End of prefix configuration. + + // And parse the query + Query query = queryParser.parseQuery(queryString); + + // Use an Enquire object on the database to run the query + Enquire enquire = new Enquire(db); + enquire.setQuery(query); + + // And print out something about each match + MSet mset= enquire.getMSet(offset, pagesize); + MSetIterator msetIterator = mset.begin(); + + while(msetIterator.hasNext()) + { + long rank = msetIterator.getRank(); + long docID = msetIterator.getDocId(); + Document doc = db.getDocument(docID); + String title = doc.getValue(0); + + System.out.println((rank+1)+": #"+docID+" "+title); + msetIterator.next(); + } + + } +} diff --git a/code/java/search1.java.out b/code/java/search1.java.out new file mode 100644 index 00000000..7bd9a595 --- /dev/null +++ b/code/java/search1.java.out @@ -0,0 +1,2 @@ +1: #001 Ansonia Sunwatch (pocket compas dial) +INFO:xapian.search:'title:sunwatch'[0:10] = 1