package jules;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import edu.jhu.nlp.wikipedia.*;

public class Indexer {
    private static String wikiFile = "./sewiki-20141104-pages-meta-current.xml";
    private static String indexDir = "./merisnuc/";

    public static String query(String querystr) {
        System.out.println("Querying: " + querystr);
        Analyzer analyzer = new StandardAnalyzer();

        QueryParser qp = new QueryParser("title", analyzer);
        Query query = null;
        try {
            query = qp.parse(querystr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 3. search
        int hitsPerPage = 10;
        IndexReader reader = null;

        try {
            reader = DirectoryReader.open((FSDirectory.open(new File(indexDir))));
        } catch (IOException e) {
            e.printStackTrace();
        }

        IndexSearcher searcher = new IndexSearcher(reader);
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
        try {
            searcher.search(query, collector);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ScoreDoc[] hits = collector.topDocs().scoreDocs;

        // 4. display results
        System.out.println("Found " + hits.length + " hits.");
        for(int i=0;i<hits.length;++i) {
            int docId = hits[i].doc;
            Document d = null;
            try {
                d = searcher.doc(docId);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("=========================================================================================");
            System.out.println("Score: " + hits[i].score);
            for (IndexableField field : d.getFields()) {
                System.out.println(field.name() + ": " + d.getField(field.name()).stringValue());
            }
            System.out.println("=========================================================================================");
        }

        return "hej";
    }

    public static void index(){
        //WikiXMLParser wxp = WikiXMLParserFactory.getDOMParser(wikiFile);
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LATEST, analyzer);
        iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        try{
            final IndexWriter writer = new IndexWriter(FSDirectory.open(new File(indexDir)), iwc);
            WikiXMLParser wxsp = WikiXMLParserFactory.getSAXParser(wikiFile);
            wxsp.setPageCallback(new PageCallbackHandler() {
                @Override
                public void process(WikiPage page) {
                    // We don't want to store unnecessary pages.
                    // text starts with #OMDIRIGERING
                    if (page.isDisambiguationPage() || page.isRedirect() || page.isSpecialPage()
                        || page.isStub() || page.getTitle().startsWith("Användare:") || page.getTitle().startsWith("Användardiskussion:")
                        || page.getText().trim().toLowerCase().startsWith("#redirect") || page.getText().trim().startsWith("#omdirig")
                        || page.getID() == null || page.getID().length() == 0
                    ) {
                    	System.out.println(page.getTitle());
                        return;
                    }
                    Document doc = new Document();
                    doc.add(new IntField("id", Integer.parseInt(page.getID()), Field.Store.YES));
                    doc.add(new TextField("title", page.getTitle(), Field.Store.YES));
                    doc.add(new TextField("text", page.getText(), Field.Store.YES));

                    if (doc.getField("text").stringValue().toLowerCase().startsWith("#omdirigering"))
                        return;

                    try {
                        writer.addDocument(doc);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            wxsp.parse();
            writer.commit();
            writer.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

@@ -53,24 +53,123 @@
 IOException;
+%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;
 %0A%0Aimport org
@@ -893,16 +893,16 @@
  %22./
-merisnuc
+indexDir
 /%22;%0A
@@ -920,22 +920,41 @@
  static 
-String
+List%3CMap%3CString, String%3E%3E
  query(S
@@ -1347,18 +1347,17 @@
 rPage = 
-10
+3
 ;%0A      
@@ -1936,16 +1936,98 @@
 results%0A
+        List%3CMap%3CString, String%3E%3E results = new ArrayList%3CMap%3CString, String%3E%3E();%0A
         
@@ -2330,32 +2330,107 @@
 ;%0A            %7D%0A
+            Map%3CString, String%3E tmpResult = new HashMap%3CString, String%3E();%0A
             Syst
@@ -2593,24 +2593,91 @@
 %5Bi%5D.score);%0A
+            tmpResult.put(%22Score%22, Float.toString(hits%5Bi%5D.score));%0A
             
@@ -2815,24 +2815,109 @@
 ngValue());%0A
+                tmpResult.put(field.name(), d.getField(field.name()).stringValue());%0A
             
@@ -3035,32 +3035,68 @@
 ============%22);%0A
+            results.add(tmpResult);%0A
         %7D%0A%0A     
@@ -3109,13 +3109,15 @@
 urn 
-%22hej%22
+results
 ;%0A  

make indexer return results