lucene_7.1_demo

lucene 7.1 demo

1、基于maven;废话不多说,如下

<dependencies>
    <dependency>
        <groupId>org.apache.lucenegroupId>
        <artifactId>lucene-coreartifactId>
        <version>7.1.0version>
    dependency>

    <dependency>
        <groupId>org.apache.lucenegroupId>
        <artifactId>lucene-analyzers-smartcnartifactId>
        <version>7.1.0version>
    dependency>

    <dependency>
        <groupId>org.apache.lucenegroupId>
        <artifactId>lucene-queryparserartifactId>
        <version>7.1.0version>
    dependency>

    <dependency>
        <groupId>org.apache.lucenegroupId>
        <artifactId>lucene-highlighterartifactId>
        <version>7.1.0version>
    dependency>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>

dependencies>

2、IndexWriter(可以是实体,也可以是读取的文件)

2.1实体

@Test
public void add() throws IOException {

    Article article = new Article();
    article.setId(1);
    article.setTitle("Lucene全文检索");
    article.setContent("Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言)。");

    final Path path = Paths.get("./article/");

    Directory directory = FSDirectory.open(path);
    Analyzer analyzer = new StandardAnalyzer();

    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
    indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);

    IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

    Document document = new Document();
    document.add(new TextField("id", article.getId().toString(), Field.Store.YES));
    document.add(new TextField("title", article.getTitle(), Field.Store.YES));
    document.add(new TextField("content", article.getContent(), Field.Store.YES));

    indexWriter.addDocument(document);
    indexWriter.close();

}

2.2 文件

@Test
public void addFile() throws IOException {

    final Path path = Paths.get("./article/");

    Directory directory = FSDirectory.open(path);
    Analyzer analyzer = new StandardAnalyzer();

    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
    indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);

    IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
    BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("e:/2017_10_17.stderrout.log")));
    String content = "";
    while ((content = bufferedReader.readLine())!=null){
        System.out.println(content);
        Document document = new Document();
        document.add(new TextField("logs", content, Field.Store.YES));
        indexWriter.addDocument(document);
    }

    indexWriter.close();

}

3、Search(查询)

@Test
    public void SearchFiles() throws IOException, ParseException {
//        String queryString = "全文检索";
        String queryString  = "error";

        //多条件
//        Query q = MultiFieldQueryParser.parse(new String[]{},new String[]{},new StandardAnalyzer());

        final Path path = Paths.get("./article/");
        Directory directory = FSDirectory.open(path);
        Analyzer analyzer = new StandardAnalyzer();

        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);

        //单条件
        QueryParser queryParser = new QueryParser("logs",analyzer);
        Query query = queryParser.parse(queryString);

        TopDocs topDocs = indexSearcher.search(query,10);

        long conut = topDocs.totalHits;
        System.out.println("检索总条数:"+conut);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            Document document = indexSearcher.doc(scoreDoc.doc);
            System.out.print("相关度:"+scoreDoc.score+"-----time:"+document.get("time"));
            System.out.println(document.get("logs"));
        }
    }


你可能感兴趣的:(lucene入门demo)