import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.document.*;

import java.util.*;
import java.io.*;

public class SearchTest{
    
    public int search ()
        throws Exception{
        List x_res = new ArrayList();

        // create the lucene query from the first channel analyzer
        Analyzer x_analyzer = new org.apache.lucene.analysis.standard.StandardAnalyzer();

        Query x_query = QueryParser.parse("one OR two OR three OR four OR six OR seven OR eight OR nine OR ten",
                "unstored", x_analyzer);

        Hits x_hits;

        Searcher x_msearcher = new IndexSearcher("index");
        x_hits = x_msearcher.search(x_query);

        for (int i=0; i<x_hits.length(); i++ ) {
            x_res.add(x_hits.doc(i).get("document_id"));
        }
        x_msearcher.close();
        return x_hits.length();
    }
    

    public static void main(String[] args) throws Exception{
        IndexWriter writer = new IndexWriter("index", new StandardAnalyzer(), true);
        for(int i=0; i<99; i++){
            Document doc = new Document();
            doc.add(Field.UnIndexed("document_id", ""+i));
            doc.add(Field.UnStored("unstored", DocumentGenerator.randomText(500, 500)));
            writer.addDocument(doc);
        }
        writer.close();


        final SearchTest ss = new SearchTest();
        for(int i=0; i<50; i++){
            new Thread(){
                public void run(){
                    try{
                        for(int i=0; i<50; i++){
                            System.out.println("found " + ss.search() + " docs");
                        }
                    }catch(Exception ex){ex.printStackTrace();}
                }
            }.start();
        }
    }

    private static class DocumentGenerator{
        private static final Random s_generator = new Random(System.currentTimeMillis());
        // array of possible keywords in the document
        private static final String[] s_keywords = {
            "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"
        };
        private static final String s_abc = "qwertyuiopasdfghjklzxcvbnm";

        // create array of possible words in the document
        private static String[] s_allwords = createAllWords(10000);

        /**
         *  Initialises array of random words
         */
        private static String[] createAllWords(int p_size) {
            String[] x_res = new String[p_size];
            for (int i=0;i<p_size;i++)
                x_res[i] = randomWord();
            return x_res;
        }


        /**
         *  Generates a string with random text containing some of specified words.
         *  Array s_allwords should be initialized beforehand.
         *@param p_words - some of these words will be in the text
         *@param p_min - the minimal number of words in the text
         *@param p_max - the maximal number of words in the text
         *@return - resulting random text
         */
        private static String randomText (int p_min, int p_max) {
            StringBuffer x_buf = new StringBuffer();
            int x_cnt = p_min + s_generator.nextInt(p_max-p_min+1);
            int x_rnd, x_wrd;
            for (int i=0;i<x_cnt-1;i++) {
                x_wrd = s_generator.nextInt(100);
                // assume the text contains 5% of keywords
                if ( x_wrd<5 ) {
                    x_rnd = s_generator.nextInt(s_keywords.length);
                    x_buf.append(" "+ s_keywords[x_rnd]);
                } else {
                    x_rnd = s_generator.nextInt(s_allwords.length);
                    x_buf.append(" "+ s_allwords[x_rnd]);
                }
            }
            // ensure that text contains at least one of specified words
            x_rnd = s_generator.nextInt(s_keywords.length);
            x_buf.append(" "+s_keywords[x_rnd]);
            return x_buf.toString();
        }


        /**
         *  Generates random word
         */
        private static String randomWord() {
            int x_len = 1 + s_generator.nextInt(15);
            StringBuffer x_buf = new StringBuffer();
            for (int i=0;i<x_len;i++)
                x_buf.append(s_abc.charAt(s_generator.nextInt(s_abc.length())));
            return x_buf.toString();
        }

    }
}
