Example usage for org.apache.lucene.analysis.standard StandardAnalyzer StandardAnalyzer

List of usage examples for org.apache.lucene.analysis.standard StandardAnalyzer StandardAnalyzer

Introduction

In this page you can find the example usage for org.apache.lucene.analysis.standard StandardAnalyzer StandardAnalyzer.

Prototype

public StandardAnalyzer() 

Source Link

Document

Builds an analyzer with no stop words.

Usage

From source file:SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);/*from  ww  w.  j  a va2  s  .  co m*/
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) {
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();

    BufferedReader in = null;
    if (queries != null) {
        in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8);
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
    }
    QueryParser parser = new QueryParser(field, analyzer);
    while (true) {
        if (queries == null && queryString == null) { // prompt the user
            System.out.println("Enter query: ");
        }

        String line = queryString != null ? queryString : in.readLine();

        if (line == null || line.length() == -1) {
            break;
        }

        line = line.trim();
        if (line.length() == 0) {
            break;
        }

        Query query = parser.parse(line);
        System.out.println("Searching for: " + query.toString(field));

        if (repeat > 0) { // repeat & time as benchmark
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                searcher.search(query, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }

        doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

        if (queryString != null) {
            break;
        }
    }
    reader.close();
}

From source file:MyServlet.java

public void init() throws ServletException {
    try {/*  w w w  .  j  a va 2s .  c o m*/
        //   Specify the analyzer for tokenizing text.
        //   The same analyzer should be used for indexing and searching
        analyzer = new StandardAnalyzer();
        //   Code to create the index
        index = new RAMDirectory();
        config = new IndexWriterConfig(analyzer);
        w = new IndexWriter(index, config);
        addDoc(w, " Software Engineering 2", "CMPE 133", "Mon.", "Computer Engineering");
        addDoc(w, " Software Engineering 1", "CMPE 131", "Mon.", "Computer Engineering");
        addDoc(w, " Object Oriented Design", "CS 151", "Mon.", "Computer Science");
        addDoc(w, " Advance Data Structures with Java ", "CS 146:", "Mon.", "Computer Science");
        addDoc(w, " System Security with Java", "CS 166:", "Mon.", "Computer Science");
        addDoc(w, "Liner math", "ME 123", "Mon.", "Math");
        w.close();
        log = new searchHistory();
        for (int i = 1; i <= 10; i++) {
            Student std = new Student();
            std.setUserName("std" + i);
            std.setPassword("123");
            stds.add(std);
            Teacher tch = new Teacher();
            tch.setUserName("tch" + i);
            tch.setPassword("123");
            tchs.add(tch);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    System.out.println("init");

}

From source file:SearcherTest.java

/**
 * ??/* ww  w. ja  v a  2  s .c  o m*/
 * QueryParser?????Query?
 *
 * @throws Exception
 */
@Test
public void testQueryParser() throws Exception {
    Analyzer analyzer = new StandardAnalyzer(); // ?
    String searchField = "contents";
    String q = "xxxxxxxxx$";
    //??
    QueryParser parser = new QueryParser(searchField, analyzer);
    //
    Query query = parser.parse(q);
    TopDocs hits = is.search(query, 100);
    System.out.println("? " + q + "" + hits.totalHits + "");
    for (ScoreDoc scoreDoc : hits.scoreDocs) {
        Document doc = is.doc(scoreDoc.doc);
        System.out.println(doc.get("fullPath"));
    }
}

From source file:FileIndexer.java

License:Apache License

public static void main(String[] args) {
    String usage = "java FileIndexer" + " [-index INDEX_PATH] [-docs DOCS_PATH] [-excludes FILE] [-update]\n\n"
            + "This indexes the documents in DOCS_PATH, creating a Lucene index"
            + "in INDEX_PATH that can be searched with SearchFiles\n"
            + "excludes is an optional list of files to be excluded, one per line.";
    String indexPath = "index";
    String docsPath = null;//from www  .  j ava2  s. co m
    boolean create = true;
    List<String> excludes = new ArrayList<String>();
    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            indexPath = args[i + 1];
            i++;
        } else if ("-docs".equals(args[i])) {
            docsPath = args[i + 1];
            i++;
        } else if ("-excludes".equals(args[i])) {
            Scanner sc = null;
            try {
                sc = new Scanner(new File(args[i + 1]));
                i++;
            } catch (FileNotFoundException fnfe) {
                System.err.println(fnfe.getMessage());
                System.exit(1);
            }
            while (sc.hasNext()) {
                excludes.add(sc.next());
            }
            sc.close();
        } else if ("-update".equals(args[i])) {
            create = false;
        }
    }

    if (docsPath == null) {
        System.err.println("Usage: " + usage);
        System.exit(1);
    }

    final Path docDir = Paths.get(docsPath);
    if (!Files.isReadable(docDir)) {
        System.out.println("Document directory '" + docDir.toAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(Paths.get(indexPath));
        Analyzer analyzer = new LimitTokenCountAnalyzer(new StandardAnalyzer(), 1000000);
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

        if (create) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer.  But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);

        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir, excludes);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here.  This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);

        writer.close();

        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }

}

From source file:SearchHelpDocs.java

License:Open Source License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {

    String usage = "Usage: java SearchFiles index-dir";
    if (args.length != 1) {
        System.out.println(usage);
        System.exit(0);/*from   w  ww .j  av  a  2 s . c om*/
    }
    String index = args[0];
    String field = LUC_KEY_CONTENT;
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String normsField = null;

    System.out.println("INFO: index-directory=" + index);
    IndexReader reader = IndexReader.open(index);
    if (normsField != null)
        reader = new OneNormsReader(reader, normsField);

    Searcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();

    BufferedReader in = null;
    in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));

    String[] fields = { LUC_KEY_CONTENT, LUC_KEY_FULL_PATH, LUC_KEY_FILE_NAME };
    MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer);

    printHelpInformation();
    while (true) {
        // prompt the user         
        System.out.print(CMDLINE_PREFIX);
        System.out.flush();
        String line = in.readLine();
        if (line == null || line.length() < 0)
            break;
        if (line.trim().length() == 0) {
            continue;
        }
        // Exit gracefully.
        if (line.trim().equalsIgnoreCase(":quit")) {
            System.out.println("INFO: quit successful");
            break;
        }

        // Modify for fuzzy query (E.g. ~0.58), also use wildcard postfix (*)
        line = line + "~";
        Object obj = parser.parse(line);
        Query query = parser.parse(line);
        System.out.println(CMDLINE_PREFIX + "Searching for: [" + line + "] query=" + query.toString(field));
        System.out.flush();
        // Search and also add the sort element
        Hits hits = searcher.search(query, createSort());
        if (repeat > 0) {
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                hits = searcher.search(query);
            }
            Date end = new Date();
            System.out.println(CMDLINE_PREFIX + "Time: " + (end.getTime() - start.getTime()) + "ms");
        }
        System.out.println(hits.length() + " total matching documents");
        for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
            int end = Math.min(hits.length(), start + HITS_PER_PAGE);
            for (int i = start; i < end; i++) {

                System.out.println(CMDLINE_PREFIX + "doc=" + hits.id(i) + " score=" + hits.score(i));

                // Ignore scores based on a certain threshold
                if (hits.score(i) < 0.09)
                    continue;

                Document doc = hits.doc(i);
                String path = doc.get(LUC_KEY_CONTENT);
                if (path != null) {
                    // Attempt to pretty print help document information
                    System.out.println("\n == Help Document Found; docid=" + hits.id(i));
                    System.out.println("*************************");
                    String fullpath = doc.get(LUC_KEY_FULL_PATH);
                    String filename = doc.get(LUC_KEY_FILE_NAME);
                    String content = doc.get(LUC_KEY_CONTENT);
                    String id = doc.get(LUC_KEY_IDENTITY);
                    if (filename != null) {
                        System.out.println("   +Filename: " + doc.get(filename));
                    }
                    if (fullpath != null) {
                        System.out.println("   +Path: " + doc.get(fullpath));
                    }
                    System.out.println("   id: " + id);
                    System.out.println(" == Content:");
                    System.out.println(prettyPrintContent(content));
                    System.out.println("-------------------------");

                    System.out.println();
                } else {
                    System.out.println((i + 1) + ". " + "No content for this document");
                }
            }
            if (queries != null) // non-interactive
                break;
            if (hits.length() > end) {
                System.out.print("more (y/n) ? ");
                line = in.readLine();
                if (line.length() == 0 || line.charAt(0) == 'n')
                    break;
            }
        }
    }
    reader.close();
}

From source file:Get_Top_Documents_Based_on_Lucene.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);/*from w  w w .  ja  va2 s.com*/
    }

    //String index = "index";
    //String index = "index_wiki_2";
    String index = "index_external_links_v1/";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 20;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) {
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();

    BufferedReader in = null;
    if (queries != null) {
        in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8);
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
    }
    QueryParser parser = new QueryParser(field, analyzer);

    // Read Question in Training Data line by line
    //String path_train = "data/training_set.tsv";
    //String path_output = "data/lucene_search_result_train.txt";
    //String path_train = "data/validation_set.tsv";
    //String path_output = "data/lucene_search_result_validation_index_wiki_2.txt";
    String path_train = "data/training_set.tsv";
    String path_output = "data/lucene_search_result_train_index_wiki_external_links_v1.txt";
    Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path_output), "UTF-8"));
    try (BufferedReader br = new BufferedReader(new FileReader(path_train))) {
        String line;
        while ((line = br.readLine()) != null) {
            line = line.trim();
            String[] lst = line.split("\t");
            String query_s = lst[1];
            if (query_s == "question") {
                continue;
            }
            System.out.println("query_s: " + query_s);
            writer.write(query_s + "\t");
            try {
                Query query = parser.parse(query_s);
                System.out.println("Searching for: " + query.toString(field));
                doPagingSearch(in, writer, searcher, query, hitsPerPage, raw,
                        queries == null && queryString == null);
            } catch (org.apache.lucene.queryparser.classic.ParseException e) {
                continue;
            }
        } // while
    }
    writer.close();
    /*
    while (true) {
      if (queries == null && queryString == null) {                        // prompt the user
        System.out.println("Enter query: ");
      }
            
      String line = queryString != null ? queryString : in.readLine();
            
      if (line == null || line.length() == -1) {
        break;
      }
            
      line = line.trim();
      if (line.length() == 0) {
        break;
      }
              
      Query query = parser.parse(line);
      System.out.println("Searching for: " + query.toString(field));
            
      if (repeat > 0) {                           // repeat & time as benchmark
        Date start = new Date();
        for (int i = 0; i < repeat; i++) {
          searcher.search(query, 100);
        }
        Date end = new Date();
        System.out.println("Time: "+(end.getTime()-start.getTime())+"ms");
      }
            
      doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);
            
      if (queryString != null) {
        break;
      }
    } // while (True)
    */
    reader.close();
}

From source file:SearchFiles11.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);//from w  w w.  j a  v  a2  s .c  o m
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) {
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();

    StandardQueryParser queryParserHelper = new StandardQueryParser();

    Query query = queryParserHelper.parse(
            "Physical OR tests OR for OR shoulder OR impingements OR and OR local OR lesions OR of OR bursa, OR tendon OR labrum OR that OR may OR accompany OR impingement",
            field);

    TopDocs results = searcher.search(query, 100);
    Date end = new Date();
    ScoreDoc[] hits = results.scoreDocs;
    int numTotalHits = results.totalHits;

    String FILENAME = "/home/devil/research/CLEF/ehealth/task2/dataset/pubmed11.res";

    int i = 1;
    try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME))) {
        String content = "";
        for (ScoreDoc h : hits) {
            Document doc = searcher.doc(h.doc);
            String path = doc.get("path");
            String[] path_words = path.split("/");
            System.out.println(path_words[path_words.length - 1] + " score=" + h.score);

            content = "CD007427 " + "NF " + path_words[path_words.length - 1] + " " + i++ + " " + h.score
                    + " pubmed\n";

            bw.write(content);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    //doPagingSearch(in, searcher, bQuery.build(), hitsPerPage, raw, queries == null && queryString == null);
    reader.close();

}

From source file:CFX_GoatSearch.java

License:Open Source License

/**
 * Classes that implement this interface can be specified in the CLASS attribute of the Java CFX tag. For example, in a class MyCustomTag, which implements this interface, the following CFML code calls the MyCustomTag.processRequest method.
 *
 * @param request// w ww .  ja  v  a 2 s  . com
 * @param response
 * @throws Exception
 */
public void processRequest(Request request, Response response) throws Exception {

    Date startTime = new Date();
    String indexPath = null;
    String queryName = null;
    String searchString = null;
    String sortField = null;
    String sortDirection = null;
    int hitsPerPage = 0;
    int pageNumber = 0;
    Vector errors = new Vector();

    if (request.attributeExists("INDEXPATH")) {
        indexPath = request.getAttribute("INDEXPATH");
    } else {
        errors.add("The cfx_lucene tag requires an attribute called 'INDEXPATH'.");
    }

    if (request.attributeExists("HITSPERPAGE")) {
        hitsPerPage = request.getIntAttribute("HITSPERPAGE");
    }

    if (request.attributeExists("PAGENUMBER")) {
        pageNumber = request.getIntAttribute("PAGENUMBER");
    }

    if (request.attributeExists("QUERYNAME")) {
        queryName = request.getAttribute("QUERYNAME");
    } else {
        errors.add("The cfx_lucene tag requires an attribute called 'QUERYNAME'.");
    }

    if (request.attributeExists("SEARCHSTRING")) {
        searchString = request.getAttribute("SEARCHSTRING");
    } else {
        errors.add("The cfx_lucene tag requires an attribute called 'SEARCHSTRING'.");
    }

    //Sorting
    if (request.attributeExists("SORTFIELD")) {
        sortField = request.getAttribute("SORTFIELD");
    }

    if (request.attributeExists("SORTDIRECTION")) {
        sortDirection = request.getAttribute("SORTDIRECTION");
    }

    //Errors
    if (!errors.isEmpty()) {
        response.write("<h2 style=\"color: #FF0000\">CFX Goat Error:</h2>");
        for (int i = 0; i < errors.size(); i++) {
            response.write("<p>Error: " + errors.get(i) + "</p>\n");
        }
        //return;
    } else {

        try {

            IndexReader reader = IndexReader.open(indexPath);
            IndexSearcher searcher = new IndexSearcher(indexPath);
            if (searcher == null) {
                errors.add("Unable to open index");
            }

            XMLReader readerXML = new XMLReader(); //XML Reader Class
            String configFile = ConfigFiles.getSchemaFile(indexPath);
            String[] indexTypeArray = new String[Integer.parseInt(readerXML.getTotalNodes(configFile))];
            String[] columnNamesArray = new String[Integer.parseInt(readerXML.getTotalNodes(configFile))]; //Add Column Names
            int totalNodes = columnNamesArray.length;
            String nodeName = "";
            //Sort .:.  Index Type must be PrimaryKey,Keyword,Date
            Sort sortby = new Sort();
            if (sortField != null)
                sortField.trim().toLowerCase(); //Change Field TO LowerCase

            Analyzer analyzer = new StandardAnalyzer();
            QueryParser parser = new MultiFieldQueryParser(columnNamesArray, analyzer);
            Query query = parser.parse(searchString);
            if (query == null) {
                errors.add("Unable to build Query");
            }
            //Build Query Here             
            //Get Column Names
            for (int i = 0; i < totalNodes; i++) {
                columnNamesArray[i] = readerXML.getNodeValueByFile(configFile, i, "columnname");
                indexTypeArray[i] = readerXML.getNodeValueByFile(configFile, i, "indextype");

                /* Make Sure Field Can Be Seached */
                if (columnNamesArray[i].equalsIgnoreCase(sortField)
                        && (indexTypeArray[i].equalsIgnoreCase("PrimaryKey")
                                || indexTypeArray[i].equalsIgnoreCase("Keyword")
                                || indexTypeArray[i].equalsIgnoreCase("Date"))) {
                    //Sort Ascending 
                    if (sortDirection != null && sortDirection.equalsIgnoreCase("desc")) {
                        System.out.println("desc");
                        sortby = new Sort(sortField, true);
                    } else if (sortDirection != null && sortDirection.equalsIgnoreCase("asc")) {
                        System.out.println("asc");
                        sortby = new Sort(sortField, false);
                    }

                }

            }

            if (hitsPerPage < 1)
                hitsPerPage = 1;
            int pageNum = pageNumber;
            int recordSet = (pageNum * hitsPerPage) + 100;

            TopFieldDocs resultDocs = searcher.search(query, null, recordSet, sortby);
            ScoreDoc[] hits = resultDocs.scoreDocs;
            int numTotalHits = resultDocs.totalHits;

            //Start
            int start = (pageNum - 1);
            if (start < 0)
                start = 0;

            if (pageNum > 1) {
                start = (pageNum * hitsPerPage) - hitsPerPage;
            }

            int end = (pageNum * hitsPerPage);
            end = Math.min(hits.length, start + hitsPerPage);

            //Coldfusion Query
            com.allaire.cfx.Query goatQuery = response.addQuery(queryName, columnNamesArray);

            for (int i = start; i < end; i++) {
                int row = goatQuery.addRow(); //Add Row
                int docId = hits[i].doc;
                Document d = searcher.doc(docId);

                for (int x = 0; x < totalNodes; x++) {
                    nodeName = columnNamesArray[x];
                    goatQuery.setData(row, (x + 1), d.get(nodeName)); //Insert Values .:. Set Data starts with 1                  

                }
            }

            //reader.close();  
            searcher.close();
            Date endTime = new Date();

            //Set other Values
            response.setVariable("goat.totaltime", Long.toString(endTime.getTime() - startTime.getTime()));
            response.setVariable("goat.totalresults", Integer.toString(numTotalHits));
            response.setVariable("goat.totalpages", Integer.toString((numTotalHits / hitsPerPage)));
        }

        catch (Exception e) {
            errors.add("Failure caught a " + e.getClass() + " with message: " + e.getMessage());
        }
    }

    //Output Final Errors If Needed
    if (!errors.isEmpty()) {
        response.write("<h2 style=\"color: #FF0000\">CFX Goat Error:</h2>");
        for (int i = 0; i < errors.size(); i++) {
            response.write("<p>Error: " + errors.get(i) + "</p>\n");
        }
    }

}

From source file:BlosxomIndexer.java

License:Apache License

public static void main(String[] argv) {
    try {//from  ww  w  .  ja  va  2 s .c  o  m
        String index = "index";
        boolean create = false;
        File root = null;

        String usage = "BlosxomIndexer [-create] [-index <index>] <root_directory>";

        if (argv.length == 0) {
            System.err.println("Usage: " + usage);
            return;
        }

        for (int i = 0; i < argv.length; i++) {
            if (argv[i].equals("-index")) { // parse -index option
                index = argv[++i];
            } else if (argv[i].equals("-create")) { // parse -create option
                create = true;
            } else if (i != argv.length - 1) {
                System.err.println("Usage: " + usage);
                return;
            } else
                root = new File(argv[i]);
        }

        Date start = new Date();

        if (!create) { // delete stale docs
            deleting = true;
            indexDocs(root, index, create);
        }

        writer = new IndexWriter(index, new StandardAnalyzer(), create);
        writer.maxFieldLength = 1000000;

        indexDocs(root, index, create); // add new docs

        System.out.println("Optimizing index...");
        writer.optimize();
        writer.close();

        Date end = new Date();

        System.out.print(end.getTime() - start.getTime());
        System.out.println(" total milliseconds");

    } catch (Exception e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:app.finder.topicsource.service.SearchFiles.java

License:Apache License

public List<TopicSource> getTopicSources(String queryString) throws IOException, ParseException {
    String field = "contents";
    String queries = null;/* w w  w .  j  a v  a 2 s .  c o m*/
    int repeat = 0;
    boolean raw = false;

    int hitsPerPage = SEARCH_MAX_SIZE; // 100;
    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(indexDir)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();

    BufferedReader in = null;
    QueryParser parser = new QueryParser(field, analyzer);

    Query query = parser.parse(queryString);

    //System.out.println("Searching for: " + query.toString(field));
    searcher.search(query, null, SEARCH_MAX_SIZE);

    List<String> list = doSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

    reader.close();

    List<TopicSource> topicSourceList = new ArrayList<TopicSource>();
    TopicSource topicSource = null;
    int counter = 0;
    for (String fileName : list) {
        topicSource = new TopicSource();
        File file = new File(fileName);

        topicSource.setFileName("" + (++counter) + ". " + file.getName());
        topicSource.setPath(file.getCanonicalPath());
        topicSource.setText(readFile(file));
        topicSourceList.add(topicSource);
    }

    return topicSourceList;
}