Lucene的简单使用

1.相关jar包

2.简单查询

 //查询sql
    private static String sql="select * from book";
    public List<Book> findAllBook() throws Exception {
        //使用jdbc连接数据库
        Connection connection;

        //预编译statement
        PreparedStatement preparedStatement;

        //结果集
        ResultSet resultSet;

        ArrayList<Book> books = new ArrayList<Book>();
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //连接数据库
        connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/lucence", "root", "123456");
        preparedStatement = connection.prepareStatement(sql);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            Book book = new Book();
            book.setId(resultSet.getInt("id"));
            book.setName(resultSet.getString("name"));
            book.setPrice(resultSet.getFloat("price"));
            book.setPic(resultSet.getString("pic"));
            book.setDescription(resultSet.getString("description"));
            books.add(book);
        }
        return books;


3.简单测试

 @Test
    public void creatIndex() throws Exception {
        //一个document对应一条book记录
        //新建一个list用于存储document
        ArrayList<Document> list = new ArrayList<Document>();
        //得到所有的book
        List<Book> books = new BookDaoImpl().findAllBook();
        //遍历集合,然后对每一条数据进行操作
        for (Book book : books) {
            Document document = new Document();
            //创建field域
            //参数:域名,field域中存储的value值,是否存储
            //图片id,不分词,要索引,要存储
            StringField id = new StringField("id", book.getId().toString(), Field.Store.YES);
            //图书名称的field,要分词,索引,存储
            TextField name = new TextField("name", book.getName(), Field.Store.YES);
            //图书价格,要分词,索引,存储
            FloatField price = new FloatField("price", book.getPrice(), Field.Store.YES);
            //图书图片,不分词,不要索引,要存储
            StoredField pic = new StoredField("pic", book.getPic());
            //图书的描述,要分词,要索引,不存储
            TextField description = new TextField("description", book.getDescription(), Field.Store.NO);
            //将field加入到document中
            document.add(id);
            document.add(name);
            document.add(price);
            document.add(pic);
            document.add(description);
            //将document加入到list集合中
            list.add(document);
        }

        //开始创建索引
        //需要一个分词器
        StandardAnalyzer analyzer = new StandardAnalyzer();
        //创建索引目录的流对象,指定索引目录的位置
        Directory d = FSDirectory.open(new File("D:\\develop\\projects\\springMVC\\indexdata"));
        //IndexWriter配置对象
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_4, analyzer);

        //创建索引操作对象,起到承上启下的作用,通过这个可以看到需要什么东西
        IndexWriter indexWriter = new IndexWriter(d, config);
        //通过indexWriter创建索引
        for (Document document : list) {
            //创建索引
            indexWriter.addDocument(document);
        }
        //提交
        indexWriter.commit();
        //关闭资源
        indexWriter.close();
    }

    //搜索
    @Test
    public void searchIndex() throws ParseException, IOException {
        //分词,搜索过程使用的分词器要和索引时使用的分词器一致
        StandardAnalyzer analyzer = new StandardAnalyzer();

        //查询分词器
        //第一个参数:指定默认搜索的域,第二个:分词器
        QueryParser queryParser = new QueryParser("description", analyzer);
        //创建查询对象()
        Query query = queryParser.parse("description:java");
        //组合条件查询,Occur.MUST 查询条件必须满足,相当于and	+(加号);Occur.SHOULD 查询条件可选,相当于or 空(不用符号);
        //Occur.MUST_NOT 查询条件不能满足,相当于not非	-(减号)
       // Query query = queryParser.parse("name:+java description:+spring");
        //根据数字范围搜索,参数:域名,最小值,最大值,是否包含最小值,是否包含最大值
        //NumericRangeQuery<Float> query = NumericRangeQuery.newFloatRange("price", 0f, 60f, true, true);
       /* 组合查询
       String[] fields={"name","description"};
        MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(fields, analyzer);
        //从name,description匹配
        Query query1 = multiFieldQueryParser.parse("lucene");*/
        //创建索引目录 的流对象,指定索引目录 的位置
        Directory d = FSDirectory.open(new File("D:\\develop\\projects\\springMVC\\indexdata"));
        //索引读取对象,自定索引读取的目录
        DirectoryReader indexReader = DirectoryReader.open(d);

        //创建索引搜索对象
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);

        //执行搜索
        //第一个参数:query查询对象,第二个:取出匹配度高的前100条
        TopDocs topDocs = indexSearcher.search(query, 100);

        //取出匹配的文档
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            //获取document的id
            int docId = scoreDoc.doc;
            //通过docId获取document,通过indexReader获取
            Document document = indexReader.document(docId);

            //取出doc中的field域的内容
            //参数指定field域名
            String id = document.get("id");
            String name = document.get("name");
            float price = Float.parseFloat(document.get("price"));
            String pic = document.get("pic");

            System.out.println("=====================");
            System.out.println("图书的id:" + id);
            System.out.println("图书的name:" + name);
            System.out.println("图书的价格:" + price);
            System.out.println("图书的图片:" + pic);

        }
        //关闭资源
        indexReader.close();
    }

    //删除索引
    @Test
    public void deleteIndex() throws IOException, ParseException {
        //需要一个分词器
        StandardAnalyzer analyzer = new StandardAnalyzer();
        //创建索引目录的流对象,指定索引目录的位置
        Directory d = FSDirectory.open(new File("D:\\develop\\projects\\springMVC\\indexdata"));
        //IndexWriter配置对象
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_4, analyzer);

        //创建索引操作对象,起到承上启下的作用,通过这个可以看到需要什么东西
        IndexWriter indexWriter = new IndexWriter(d, config);

        //删除所有索引
        //indexWriter.deleteAll();

        //查询分析器
        //参数:第一个--默认搜索的域,第二个--分词器
        QueryParser queryParser = new QueryParser("description", analyzer);
        //创建查询对象
        Query query = queryParser.Query("description;java");
        //删除符合查询条件的索引,删除符合查询条件的所有document
        indexWriter.deleteDocuments(query);
        //提交
        indexWriter.commit();
        //关闭资源
        indexWriter.close();
    }

    //更新索引(更新思路:先查询、再删除、再添加。)
    @Test
    public void updateIndex() throws IOException {
        //需要一个分词器
        StandardAnalyzer analyzer = new StandardAnalyzer();
        //创建索引目录的流对象,指定索引目录的位置
        Directory d = FSDirectory.open(new File("D:\\develop\\projects\\springMVC\\indexdata"));
        //IndexWriter配置对象
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_4, analyzer);

        //创建索引操作对象,起到承上启下的作用,通过这个可以看到需要什么东西
        IndexWriter indexWriter = new IndexWriter(d, config);

        Term term = new Term("id", "1");
        //创建新的document,替换id为1的document
        Document doc = new Document();
        //图书id,不要分词,要索引,要存储
        StringField id = new StringField("id", "1".toString(), Field.Store.YES);
        //图书名称:要分词,要索引,要存储
        TextField name = new TextField("name", "java编程思想第三版", Field.Store.YES);
        doc.add(id);
        doc.add(name);

        //通过这个方法可以看出需要term和doc
        // 思路:先查询,再删除,在添加(意思就是旧的被完全替换)
        indexWriter.updateDocument(term,doc);

        //提交
        indexWriter.commit();
        //关闭资源
        indexWriter.close();
    }



4.小参考

Field类

数据类型

Tokenized是否分词

Indexed

是否索引

Stored

是否存储

说明

StringField(FieldName, FieldValue,Store.YES))

字符串

N

Y

YN

这个Field用来构建一个字符串Field,但是不会进行分析,会将整个串存储在索引中,比如(订单号,姓名等)

是否存储在文档中用Store.YES或Store.NO决定

LongField(FieldName, FieldValue,Store.YES)

Long

Y

Y

YN

这个Field用来构建一个Long数字型Field,进行分析和索引,比如(价格)

是否存储在文档中用Store.YES或Store.NO决定

StoredField(FieldName, FieldValue)

重载方法,支持多种类型

N

N

Y

这个Field用来构建不同类型Field

不分析,不索引,但要Field存储在文档中

TextField(FieldName, FieldValue, Store.NO)

TextField(FieldName, reader)

字符串

Y

Y

YN

如果是一个Reader, lucene猜测内容比较多,会采用Unstored的策略.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lucene.net 是一个开源的全文检索引擎库,它提供了一些基本的 API 来创建和维护索引,并且可以通过这些 API 来搜索索引中的文档。下面是一些使用 Lucene.net 的基本步骤: 1. 创建索引:使用 Lucene.net 的 API,可以创建一个空的索引。可以将文档添加到索引中,以便后续搜索。 2. 添加文档:使用 Lucene.net 的 API,可以将文档添加到索引中。可以为每个文档定义一个或多个字段。 3. 搜索索引:使用 Lucene.net 的 API,可以搜索索引中的文档。可以使用查询对象来指定搜索条件,例如搜索某个字段中包含特定关键字的文档。 4. 处理搜索结果:搜索结果是一组匹配查询条件的文档。可以使用 Lucene.net 的 API 来访问每个文档的字段,以便将搜索结果呈现给用户。 以下是一个简单的示例代码,可用于创建索引、添加文档和搜索索引: ``` // 创建索引 var indexDirectory = FSDirectory.Open(@"C:\myindex"); var analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48); var indexConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer); var writer = new IndexWriter(indexDirectory, indexConfig); // 添加文档 var doc = new Document(); doc.Add(new TextField("title", "Lucene.net tutorial", Field.Store.YES)); doc.Add(new TextField("content", "This is a tutorial on how to use Lucene.net for full text search.", Field.Store.YES)); writer.AddDocument(doc); // 搜索索引 var searcher = new IndexSearcher(writer.GetReader(true)); var queryParser = new QueryParser(LuceneVersion.LUCENE_48, "content", analyzer); var query = queryParser.Parse("full text search"); var topDocs = searcher.Search(query, 10); foreach (var scoreDoc in topDocs.ScoreDocs) { var doc = searcher.Doc(scoreDoc.Doc); Console.WriteLine(doc.Get("title")); } ``` 此示例创建一个名为“myindex”的索引目录,添加一个文档,然后搜索包含“full text search”关键字的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值