Lucene query serch on text column. Output should be two columns matching string and record_id column


(Danesh) #1

Hi,

I have a description column on which i am applying search and with it i want to pass additional record_id column which will help in joining the result set to other data sets. Please see the code below,
input column is record_id column where serch is not required and and input text is description column.
What would be the simple way to return matching string and record_id as output ??

RAMDirectory ramDirectory = new RAMDirectory();
String inputColumn = (String) input.get(0);
String inputText = (String) input.get(1);
Document document = new Document();
//You must specify the index to be analyzed for it to be "searchable"
document.add(new Field(inputColumn, inputText, Store.NO,
Index.ANALYZED, TermVector.NO));
//Add the document to a searchable index
config = new IndexWriterConfig(Version.LUCENE_34, sAnalyzer);
IndexWriter w = new IndexWriter(ramDirectory, config);
//Out index only contains one posting that is searchable.
w.addDocument(document);
w.close();

IndexSearcher srcher = new IndexSearcher(
				IndexReader.open(ramDirectory));
		//For each rule, create the regex query and see if it applies
		for (Entry<String, String> ruleEntry : regexRules) {
			//Parse the string as a Lucene "query"
			Query query;
			try {
				query = queryParser.parse(ruleEntry.getValue());
				System.out.println("Value" +ruleEntry.getValue());
			} catch (ParseException e) {
				//Query could not be properly parsed. Something wrong with the Lucene query syntax
				System.err.println("The query is not well formed: " + ruleEntry.getValue() + "for the canonical name " + ruleEntry.getKey());
				e.printStackTrace();
				continue;
			//	System.out.println("Match found" +ruleEntry.getValue());
			}
			//Let the searcher see if it found a hit in the index
			TopDocs hits = srcher.search(query, 1);

			//Does our results contain at least one document [well there is only one
			//document anyway]; has that result been matched by our rule
			
			if (hits == null || hits.totalHits < 1)
				continue; // No value found; Proceed to apply the next rule
			
			else

                return ruleEntry.getKey();

Thank you
Danesh


(Danesh) #2

Pls help...


(system) #3