创建jsp搜索表单以运行java Search程序

时间:2014-07-30 14:24:55

标签: java jsp

这里的背景信息是我有一个工作的索引器和搜索(在java中),它索引并搜索文件目录中的文件名,然后将文件复制到“结果”目录。

我需要/没有太多经验就是编写jsp文件。我需要jsp文件有一个文本搜索栏,然后是一个搜索按钮。当在栏中输入文本并单击该按钮时,我需要它以输入的文本作为arg运行我的搜索程序。

我添加了IndexFiles和SearchFiles类供参考。

如果你能提供帮助,请以一个很好的例子来解释!

   public class SearchFiles {
    static File searchDirectory = new File(
            "C:\\Users\\flood.j.2\\Desktop\\IndexSearch\\Results");
    static String v = new String();
    static String path = null;
    String title = null;
    File addedFile = null;
    OutputStream out = null;
    String dirName = "C:\\Users\\flood.j.2\\Desktop\\IndexSearch\\Results";

    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]";
        if (args.length > 0
                && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
            System.out.println(usage);
            System.exit(0);
        }
        for (int j = 5; j < args.length; j++) {
            v += args[j] + " ";
        }
        String index = "index";
        String field = "contents";
        String queries = null;
        boolean raw = false;
        String queryString = null;
        int hits = 100;

        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 = v;
                i++;

            }
        }
        IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(
                index)));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);

        BufferedReader in = null;
        if (queries != null) {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(
                    queries), "UTF-8"));
        } else {
            in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
        }
        QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer);
        for (int m = 0; m < 2; m++) {
            if (queries == null && queryString == null) {
                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));

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

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

    public static void doPagingSearch(BufferedReader in,
            IndexSearcher searcher, Query query, int hitsPerPage, boolean raw,
            boolean interactive) throws IOException {

        // Collect enough docs to show 500 pages
        TopDocs results = searcher.search(query, 5 * hitsPerPage);
        ScoreDoc[] hits = results.scoreDocs;

        int numTotalHits = results.totalHits;
        System.out.println(numTotalHits + " total matching documents");
        int start = 0;
        int end = Math.min(numTotalHits, hitsPerPage);
        FileUtils.deleteDirectory(searchDirectory);
        while (true) {

            for (int i = start; i < end; i++) {
                Document doc = searcher.doc(hits[i].doc);
                path = doc.get("path");
                if (path != null) {
                    System.out.println((i + 1) + ". " + path);

                    File addFile = new File(path);
                    try {
                        FileUtils.copyFileToDirectory(addFile, searchDirectory);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }

            if (!interactive || end == 0) {
                break;
            }
            System.exit(0);

        }

    }

}

public class IndexFiles {

    private IndexFiles() {
    }

    public static void main(String[] args) {
        String usage = "java org.apache.lucene.demo.IndexFiles"
                + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
                + "This indexes the documents in DOCS_PATH, creating a Lucene index"
                + "in INDEX_PATH that can be searched with SearchFiles";
        String indexPath = null;
        String docsPath = null;
        boolean create = true;
        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 ("-update".equals(args[i])) {
                create = false;
            }
        }

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

        final File docDir = new File(docsPath);
        if (!docDir.exists() || !docDir.canRead()) {
            System.out
                    .println("Document directory '"
                            + docDir.getAbsolutePath()
                            + "' 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(new File(indexPath));
            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
            IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40,
                    analyzer);

            if (create) {
                iwc.setOpenMode(OpenMode.CREATE);
            } else {

                iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
            }
            IndexWriter writer = new IndexWriter(dir, iwc);
            indexDocs(writer, docDir);
            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());
        }
    }

    static void indexDocs(IndexWriter writer, File file) throws IOException {
        if (file.canRead()) {
            if (file.isDirectory()) {
                String[] files = file.list();
                if (files != null) {
                    for (int i = 0; i < files.length; i++) {
                        indexDocs(writer, new File(file, files[i]));
                    }
                }
            } else {

                FileInputStream fis;
                try {
                    fis = new FileInputStream(file);
                } catch (FileNotFoundException fnfe) {
                    return;
                }
                try {
                    Document doc = new Document();
                    Field pathField = new StringField("path",
                            file.getAbsolutePath(), Field.Store.YES);
                    doc.add(pathField);
                    doc.add(new LongField("modified", file.lastModified(),
                            Field.Store.NO));
                    doc.add(new TextField("title", file.getName(), null));
                    System.out.println(pathField);

                    if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
                        System.out.println("adding " + file);
                        writer.addDocument(doc);
                    } else {
                        System.out.println("updating " + file);
                        writer.updateDocument(new Term("path", file.getPath()),
                                doc);
                    }

                } finally {
                    fis.close();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您绝对应该在 servlet 而不是JSP中执行此操作。在JSP中加入大量逻辑是不好的做法。 (见servlets info page)。

其次,使用cronjob(Linux)或任务(Windows)每小时运行搜索程序并将结果存储在数据库中并让你的servlet从那里拉出而不是允许用户启动搜索程序。