package play.learn.lucene.termrangequery;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.TopDocs;

public class LearnTermRangeQuery {
	
	String indexDir = "clean/index.termrange/";
	String dataDir = "src/resources/samples.termrange";
	Searcher searcher;
	Indexer indexer;
	
	public static void main(String[] args) {
		LearnTermRangeQuery tester;
		
		try {
			tester = new LearnTermRangeQuery();
			//tester.createIndex();
			tester.searchUsingTermRangeQuery("record2.txt", "record6.txt");
		} catch (Exception e) {
			
		}
	}

	private void createIndex() throws IOException {
		indexer = new Indexer(indexDir);
		int numIndexed;
		long startTime = System.currentTimeMillis();
		
		numIndexed = indexer.createIndex(dataDir, new TextFileFilter());
		long endTime = System.currentTimeMillis();
		indexer.close();
		System.out.println(numIndexed+" File indexed, time taken: " +(endTime-startTime)+" ms");
	}

	private void searchUsingTermRangeQuery(String searchQueryMin, String searchQueryMax) throws IOException, ParseException {
		
		searcher = new Searcher(indexDir);
		long startTime = System.currentTimeMillis();
		// create the term query object
		Query query = TermRangeQuery.newStringRange(LuceneConstants.FILE_NAME, searchQueryMin, searchQueryMax, true, false);
		// NumericRangeQuery query = NumericRangeQuery.newIntRange(LuceneConstants.FILE_NAME, 2, 6, true, false);
		
		// do the search
		TopDocs hits = searcher.search(query);
		long endTime = System.currentTimeMillis();
		
		System.out.println(hits.totalHits + " documents found. Time :" + (endTime - startTime) + "ms");
		
		for (ScoreDoc scoreDoc : hits.scoreDocs) {
			Document doc = searcher.getDocument(scoreDoc);
			System.out.println("File : " + doc.get(LuceneConstants.FILE_PATH));
		}
		
		searcher.close();
		
		
	}

}

@@ -36,16 +36,64 @@
 query;%0A%0A
+import java.io.File;%0Aimport java.io.FileFilter;%0A
 import j
@@ -112,16 +112,16 @@
 eption;%0A
-
 %0Aimport 
@@ -222,59 +222,8 @@
 on;%0A
-import org.apache.lucene.search.NumericRangeQuery;%0A
 impo
@@ -689,18 +689,16 @@
 y();%0A%09%09%09
-//
 tester.c
@@ -1016,28 +1016,86 @@
 ir, 
-new TextFileFilter()
+x -%3E %7BSystem.out.println(x);return x.getName().toLowerCase().endsWith(%22txt%22);%7D
 );%0A%09
@@ -1496,16 +1496,16 @@
  object%0A
+
 %09%09Query 
@@ -1619,116 +1619,8 @@
 e);%0A
-%09%09// NumericRangeQuery query = NumericRangeQuery.newIntRange(LuceneConstants.FILE_NAME, 2, 6, true, false);%0A
 %09%09%0A%09

use lambda.