lucene:索引维护(删除、更新、查询)

1、删除全部索引

(1)删除索引前执行查询:

1.txt
null
null
E:\test\1.txt
1.txt
null
null
E:\test\1.txt

(2)执行删除索引的代码:

   @Test
    public void test() throws Exception {
        //指定一个分析器,对文档内容进行分析。
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        //指定索引库的存放位置Directory对象,保存索引到内存中 (内存索引库)
        Directory directory = FSDirectory.open(new File("E:\\test1").toPath());
        //创建一个indexwriter对象
        IndexWriter indexWriter = new IndexWriter(directory, config);
        indexWriter.deleteAll();
        indexWriter.close();
    }

(3)再次执行查询索引的代码,没有查询结果

 

2、根据条件删除

(1)创建并查询索引:

java a.txt
null
null
E:\test\java a.txt
java b.txt
null
null
E:\test\java b.txt

(2)执行根据条件删除索引的程序:

    @Test
    public void test() throws Exception {
        //指定一个分析器,对文档内容进行分析。
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        //指定索引库的存放位置Directory对象,保存索引到内存中 (内存索引库)
        Directory directory = FSDirectory.open(new File("E:\\test1").toPath());
        //创建一个indexwriter对象
        IndexWriter indexWriter = new IndexWriter(directory, config);
        Query query = new TermQuery(new Term("fileName","java"));
        indexWriter.deleteDocuments(query);
        indexWriter.close();
    }

(3)再次查询索引,无查询结果

 

3、更新索引

    @Test
    public void testUpdate() throws Exception {
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        Directory directory = FSDirectory.open(new File("E:\\test1").toPath());
        IndexWriter indexWriter = new IndexWriter(directory, config);
        Document doc = new Document();
        doc.add(new TextField("test_update_name", "test_name.txt", Field.Store.YES));
        indexWriter.updateDocument(new Term("fileName","java"),doc);
        indexWriter.close();
    }

先将文件名为java的索引删除,再将名为test_name.txt的索引添加进去。

 

4、查询所有

  @Test
    public void testSelect() throws IOException {
        Directory directory = FSDirectory.open(new File("E:\\test1").toPath());// 磁盘
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        Query query = new MatchAllDocsQuery();
        System.out.println(query);
        TopDocs topDocs = indexSearcher.search(query, 10);
        //返回查询结果,遍历查询结果并输出
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            int doc = scoreDoc.doc;
            Document document = indexSearcher.doc(doc);
            // 文件名称
            String fileName = document.get("fileName");
            System.out.println(fileName);
            // 文件内容
            String fileContent = document.get("fileContent");
            System.out.println(fileContent);
            // 文件大小
            String fileSize = document.get("fileSize");
            System.out.println(fileSize);
            // 文件路径
            String filePath = document.get("filePath");
            System.out.println(filePath);
        }
        indexSearcher.getIndexReader().close();
    }

查询结果:

*:*
001.txt
null
null
E:\test\001.txt
1.txt
null
null
E:\test\1.txt
2.txt
null
null
E:\test\2.txt
3.txt
null
null
E:\test\3.txt
abc.txt
null
null
E:\test\abc.txt
java.txt
null
null
E:\test\java.txt
js.txt
null
null
E:\test\js.txt
2.txt
null
null
E:\test\2.txt
3.txt
null
null
E:\test\3.txt
abc.txt
null
null
E:\test\abc.txt

 

5、根据数值范围查询

 @Test
    public void testSelect() throws IOException {
        Directory directory = FSDirectory.open(new File("E:\\test1").toPath());// 磁盘
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        //false和true表示不包含47但是包含200
        Query query = NumericRangeQuery.newLongRange("fileSize", 47L, 200L, false, true);
        System.out.println(query);

        TopDocs topDocs = indexSearcher.search(query, 10);
        //返回查询结果,遍历查询结果并输出
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            int doc = scoreDoc.doc;
            Document document = indexSearcher.doc(doc);
            // 文件名称
            String fileName = document.get("fileName");
            System.out.println(fileName);
            // 文件内容
            String fileContent = document.get("fileContent");
            System.out.println(fileContent);
            // 文件大小
            String fileSize = document.get("fileSize");
            System.out.println(fileSize);
            // 文件路径
            String filePath = document.get("filePath");
            System.out.println(filePath);
        }
        indexSearcher.getIndexReader().close();
    }

是根据fileSize域来查询的

 

6、组合查询

  @Test
    public void testSelect() throws Exception {
        Directory directory = FSDirectory.open(new File("E:\\test1").toPath());// 磁盘
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
        Query query1 = new TermQuery(new Term("fileName","java"));
        Query query2 = new TermQuery(new Term("fileName","a"));
        //  select * from user where id =1 or name = ‘safdsa‘
        booleanQuery.add(query1, BooleanClause.Occur.MUST);
        booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
        TopDocs topDocs = indexSearcher.search(booleanQuery.build(), 10);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            int doc = scoreDoc.doc;
            Document document = indexSearcher.doc(doc);
            // 文件名称
            String fileName = document.get("fileName");
            System.out.println(fileName);
            // 文件内容
            String fileContent = document.get("fileContent");
            System.out.println(fileContent);
            // 文件大小
            String fileSize = document.get("fileSize");
            System.out.println(fileSize);
            // 文件路径
            String filePath = document.get("filePath");
            System.out.println(filePath);
        }
        indexSearcher.getIndexReader().close();
    }

测试结果:

java a.txt
null
null
E:\test\java a.txt
java b.txt
null
null
E:\test\java b.txt
  • MUST 必须 相当于and, 并且
  • MUST_NOT 必须不满足,相当于not, 非
  • SHOULD 应该 相当于or,或者
  • 可以类比于sq语句的多条件查询

 

7、解析的方式查询

(1)*:*  查询所有:表示域和值都不添加限制条件,但是添加了默认的filename:

 @Test
    public void testSelect() throws Exception {
        Directory directory = FSDirectory.open(new File("E:\\test1").toPath());// 磁盘
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        QueryParser queryParser=new QueryParser("fileName",new IKAnalyzer());
        Query query=queryParser.parse("*:*");
        TopDocs topDocs = indexSearcher.search(query, 10);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            int doc = scoreDoc.doc;
            Document document = indexSearcher.doc(doc);
            // 文件名称
            String fileName = document.get("fileName");
            System.out.println(fileName);
            // 文件内容
            String fileContent = document.get("fileContent");
            System.out.println(fileContent);
            // 文件大小
            String fileSize = document.get("fileSize");
            System.out.println(fileSize);
            // 文件路径
            String filePath = document.get("filePath");
            System.out.println(filePath);
        }
        indexSearcher.getIndexReader().close();
    }
001.txt
null
null
E:\test\001.txt
1.txt
null
null
E:\test\1.txt
2.txt
null
null
E:\test\2.txt
3.txt
null
null
E:\test\3.txt
abc.txt
null
null
E:\test\abc.txt
java.txt
null
null
E:\test\java.txt
js.txt
null
null
E:\test\js.txt
2.txt
null
null
E:\test\2.txt
3.txt
null
null
E:\test\3.txt
abc.txt
null
null
E:\test\abc.txt

(2)添加条件,不执行默认的条件:

Directory directory = FSDirectory.open(new File("E:\\test1").toPath());// 磁盘
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        QueryParser queryParser=new QueryParser("fileName",new StandardAnalyzer());
        Query query=queryParser.parse("fileName:java");

此时默认的条件已经不起作用了,执行的是自定义的条件

(3)多域默认查询

 String[] fields = {"fileName","fileContent"};
 MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields,new IKAnalyzer());
  • 参数1: 默认查询的域
  • 参数2:采用的分析器

 

lucene:索引维护(删除、更新、查询)

上一篇:Android Studio的初体验


下一篇:jQuery之返回顶部