Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
5 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1: #001 Ansonia Sunwatch (pocket compas dial) | ||
INFO:xapian.search:'title:sunwatch'[0:10] = 1 |