Lucene使用Java代码查看索引库

查询索引库

步骤:
    1.创建一个Director对象,指定索引库的位置
    
    2.创建一个IndexReader对象
    
    3.创建一个IndexSearcher对象,构造方法中的参数indexReader对象
    
    4.创建一个Query对象.TermQuery
    
    5.执行查询,得到一个TopDocs对象
    
    6.取查询结果的总记录数
    
    7.取文档列表
    
    8.打印文档中的内容
    
    9.关闭IndexReader对象

代码如下:

package com.itheima;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

import java.io.File;

/**
 * @ClassName luceneTwo
 * @Description TODO
 * @Author gkz
 * @Date 2019/8/21 23:46
 * @Version 1.0
 **/
public class luceneTwo {

    @Test
    public void SearchIndex() throws Exception{
//        1.创建一个Director对象,指定索引库的位置
        Directory directory=FSDirectory.open(new File("E:\\Desktop").toPath());
//        2.创建一个IndexReader对象
        IndexReader indexReader= DirectoryReader.open(directory);
//        3.创建一个IndexSearcher对象,构造方法中的参数indexReader对象
        IndexSearcher indexSearcher=new IndexSearcher(indexReader);
//        4.创建一个Query对象.TermQuery
        Query query=new TermQuery(new Term("context","spring"));
//        5.执行查询,得到一个TopDocs对象
        //参数一查询对象,参数二:查询结果返回的最大记录数
        TopDocs topDocs =indexSearcher.search(query,10);
//        6.取查询结果的总记录数
        System.out.println("查询总记录数"+topDocs.totalHits);
//        7.取文档列表
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//        8.打印文档中的内容
        for (ScoreDoc scoreDoc : scoreDocs) {
            //取文档id
            int doc = scoreDoc.doc;
            //根据id取文档
            Document doc1 = indexSearcher.doc(doc);
            System.out.println(doc1.get("name"));
            System.out.println(doc1.get("path"));
            System.out.println(doc1.get("context"));
            System.out.println(doc1.get("size"));
        }

//        9.关闭IndexReader对象
        indexReader.close();
    }
}

可以看到控制台已经输出了context域包含spring的文件名,路径,内容和大小:

D:\Java\jdk1.8.0_181\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=3208:D:\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar;D:\IntelliJ IDEA 2019.1.3\plugins\junit\lib\junit-rt.jar;D:\IntelliJ IDEA 2019.1.3\plugins\junit\lib\junit5-rt.jar;D:\Java\jdk1.8.0_181\jre\lib\charsets.jar;D:\Java\jdk1.8.0_181\jre\lib\deploy.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_181\jre\lib\javaws.jar;D:\Java\jdk1.8.0_181\jre\lib\jce.jar;D:\Java\jdk1.8.0_181\jre\lib\jfr.jar;D:\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_181\jre\lib\jsse.jar;D:\Java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_181\jre\lib\plugin.jar;D:\Java\jdk1.8.0_181\jre\lib\resources.jar;D:\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\IdeaProject\luceneproject\target\classes;D:\repository\commons-io\commons-io\2.6\commons-io-2.6.jar;D:\repository\org\apache\lucene\lucene-core\7.7.2\lucene-core-7.7.2.jar;D:\repository\org\apache\lucene\lucene-analyzers-common\7.7.2\lucene-analyzers-common-7.7.2.jar;D:\repository\junit\junit\4.12\junit-4.12.jar;D:\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.itheima.luceneTwo,SearchIndex
查询总记录数6
spring_README.txt
E:\Desktop\87.lucene\lucene\02.参考资料\searchsource\spring_README.txt
## Spring Framework  
springmvc.txt
The Spring Framework provides a comprehensive programming and configuration model for modern
Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is
infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise
applications so that teams can focus on application-level business logic, without unnecessary ties
to specific deployment environments.

The framework also serves as the foundation for
[Spring Integration](https://github.com/SpringSource/spring-integration),
[Spring Batch](https://github.com/SpringSource/spring-batch) and the rest of the Spring
[family of projects](http://springsource.org/projects). Browse the repositories under the
[SpringSource organization](https://github.com/SpringSource) on GitHub for a full list.

[.NET](https://github.com/SpringSource/spring-net) and
[Python](https://github.com/SpringSource/spring-python) variants are available as well.

## Downloading artifacts
Instructions on
[downloading Spring artifacts](https://github.com/SpringSource/spring-framework/wiki/Downloading-Spring-artifacts)
via Maven and other build systems are available via the project wiki.

## Documentation
See the current [Javadoc](http://static.springsource.org/spring-framework/docs/current/api)
and [Reference docs](http://static.springsource.org/spring-framework/docs/current/reference).

## Getting support
Check out the [Spring forums](http://forum.springsource.org) and the
[Spring tag](http://stackoverflow.com/questions/tagged/spring) on StackOverflow.
[Commercial support](http://springsource.com/support/springsupport) is available too.

## Issue Tracking
Spring's JIRA issue tracker can be found [here](http://jira.springsource.org/browse/SPR). Think
you've found a bug? Please consider submitting a reproduction project via the
[spring-framework-issues](https://github.com/springsource/spring-framework-issues) repository. The
[readme](https://github.com/springsource/spring-framework-issues#readme) provides simple
step-by-step instructions.

## Building from source
Instructions on
[building Spring from source](https://github.com/SpringSource/spring-framework/wiki/Building-from-source)
are available via the project wiki.

## Contributing
[Pull requests](http://help.github.com/send-pull-requests) are welcome; you'll be asked to sign our
contributor license agreement ([CLA](https://support.springsource.com/spring_committer_signup)).
Trivial changes like typo fixes are especially appreciated (just
[fork and edit!](https://github.com/blog/844-forking-with-the-edit-button)). For larger changes,
please search through JIRA for similiar issues, creating a new one if necessary, and discuss your
ideas with the Spring team.

## Staying in touch
Follow [@springframework](http://twitter.com/springframework) and its
[team members](http://twitter.com/springframework/team/members) on Twitter. In-depth articles can be
found at the SpringSource [team blog](http://blog.springsource.org), and releases are announced via
our [news feed](http://www.springsource.org/news-events).

## License
The Spring Framework is released under version 2.0 of the
[Apache License](http://www.apache.org/licenses/LICENSE-2.0).

3257
springmvc.txt
E:\Desktop\87.lucene\lucene\02.参考资料\searchsource\springmvc.txt
Spring


2.  Web mvc

 

1、  用户发起request请求至控制器Spring(Controller)
控制接收用户请求的数据,委托给模型进行处理
2、  控制器通过模型(Model)处理数据并得到处理结果
模型通常是指业务逻辑
3、  控制器将模型数据在视图(View)中展示
web中模型无法将数据直接在视图上显示Spring,需要通过控制器完成。如果在C/S应用中模型是可以将数据在视图中展示的。
4、  控制器将视图response响应给用户
通过视图展示给用户要的数据或处理结果。




3.  Spring web mvc 架构
架构图

 

流程
1、  用户发送请求至前端控制器DispatcherServlet
2、  DispatcherServlet收到请求调用HandlerMapping处理器映射器。
3、  处理器映射器找到具体的处理器,生成处理器对象及处理器Spring拦截器(如果有则生成)一并返回给DispatcherServlet。
4、  DispatcherServlet调用HandlerAdapter处理器适配器
5、  HandlerAdapter经过适配调用具体的Spring处理器(Controller,也叫后端控制器)。
6、  Controller执行完成返回ModelAndView
7、  HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
8、  DispatcherServlet将ModelAndView传给ViewReslover视图解析器
9、  ViewReslover解析后返回具体View
10、 DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中)。
11、 DispatcherServlet响应用户

组件说明:
以下组件通常使用框架提供实现:
DispatcherServlet:作为前端控制器,整个流Spring程控制的中心,控制其它组Spring件执行,统一调度,降Spring低组件之间的耦合性,提高每个组件的扩展性。
HandlerMapping:通过扩展处理Spring器映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。
HandlAdapter:通过扩展处理器适配器,支持更Spring多类型的处理器。
ViewResolver:通过扩展视图Spring解析器,支持更多类型的视图解析,例如:jsp、freemarker、pdf、excel等。

2124
2.Serving Web Content.txt
E:\Desktop\87.lucene\lucene\02.参考资料\searchsource\2.Serving Web Content.txt
Serving Web Content with Spring MVC
35
1.create web page.txt
E:\Desktop\87.lucene\lucene\02.参考资料\searchsource\1.create web page.txt
Learn how to create a web page with Spring MVC.
47
spring.txt
E:\Desktop\87.lucene\lucene\02.参考资料\searchsource\spring.txt
The Spring Framework provides a comprehensive programming and configuration model.
82
cxf_README.txt
E:\Desktop\87.lucene\lucene\02.参考资料\searchsource\cxf_README.txt
Welcome to Apache CXF!
======================
Apache CXF is an open source services framework. CXF helps you build and 
develop services using frontend programming APIs, like JAX-WS and JAX-RS. 
These services can speak a variety of protocols such as SOAP, XML/HTTP, 
RESTful HTTP, or CORBA and work over a variety of transports such as HTTP,
JMS or JBI.

CXF includes a broad feature set, but it is primarily focused on the following 
areas:

    * Web Services Standards Support: CXF supports a variety of web service 
      standards including SOAP, the Basic Profile, WSDL, WS-Addressing, 
      WS-Policy, WS-ReliableMessaging, WS-Security, WS-SecurityPolicy,
      WS-SecureConversation, and WS-Trust.
    * Frontends: CXF supports a variety of "frontend" programming models. CXF
      implements the JAX-WS APIs. It also includes a "simple frontend" which 
      allows creation of clients and endpoints without annotations. CXF supports 
      both contract first development with WSDL and code first development 
      starting from Java.  There is also a JAX-RS frontend for providing 
      REST support.
    * Ease of use: CXF is designed to be intuitive and easy to use. There 
      are simple APIs to quickly build code-first services, Maven plug-ins to 
      make tooling integration easy, JAX-WS API support, Spring 2.x XML support 
      to make configuration a snap, and much more.
    * Binary and Legacy Protocol Support: CXF has been designed to provide a 
      pluggable architecture that supports not only XML but also non-XML type 
      bindings, such as JSON and CORBA, in combination with any type of transport.


Export Notice
============================
This distribution includes cryptographic software.  The country in 
which you currently reside may have restrictions on the import, 
possession, use, and/or re-export to another country, of 
encryption software.  BEFORE using any encryption software, please 
check your country's laws, regulations and policies concerning the
import, possession, or use, and re-export of encryption software, to 
see if this is permitted.  See  for more
information.

The U.S. Government Department of Commerce, Bureau of Industry and
Security (BIS), has classified this software as Export Commodity 
Control Number (ECCN) 5D002.C.1, which includes information security
software using or performing cryptographic functions with asymmetric
algorithms.  The form and manner of this Apache Software Foundation
distribution makes it eligible for export under the License Exception
ENC Technology Software Unrestricted (TSU) exception (see the BIS 
Export Administration Regulations, Section 740.13) for both object 
code and source code.

The following provides more details on the included cryptographic
software:
   http://xml.apache.org/security/
   http://www.bouncycastle.org/
   http://ws.apache.org/wss4j/



Getting Started
===============

For an Apache CXF source distribution, please read BUILDING.txt for 
instructions on building Apache CXF. 

For an Apache CXF binary distribution, please read release_notes.txt
for installation instructions and list of supported and unsupported 
features.

Alternatively, you can also find out how to get started here:
http://cxf.apache.org/

If you need more help try talking to us on our mailing lists:
http://cxf.apache.org/mailing-lists.html
 
If you find any issues with CXF, please submit reports with JIRA here:
https://issues.apache.org/jira/browse/CXF

We welcome contributions, and encourage you to get involved in the CXF
community. If you'd like to learn more about how you can contribute, please
see:
http://cxf.apache.org/getting-involved.html

Thank you for using CXF!

The Apache CXF Team
http://cxf.apache.org/

3770

Process finished with exit code 0

你可能感兴趣的:(Lucene使用Java代码查看索引库)