• (六)lucene之其他查询方式(组合查询,制定数字范围、指定字符串开头)


    • 本章使用的是lucene5.3.0
    • 指定数字范围查询
    package com.shyroke.test;
    
    import java.io.IOException;
    import java.nio.file.Paths;
    
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.document.IntField;
    import org.apache.lucene.document.StringField;
    import org.apache.lucene.index.DirectoryReader;
    import org.apache.lucene.index.IndexReader;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.index.Term;
    import org.apache.lucene.search.BooleanClause;
    import org.apache.lucene.search.BooleanQuery;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.NumericRangeQuery;
    import org.apache.lucene.search.PrefixQuery;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.TopDocs;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.junit.Before;
    import org.junit.Test;
    
    public class IndexTest {
    
        private Integer ids[]={1,2,3};
        private String citys[]={"aingdao","banjing","changhai"};
        private String descs[]={
                "Qingdao is a beautiful city.",
                "Nanjing is a city of culture.",
                "Shanghai is a bustling city."
        };
    
        private IndexReader reader;
        private IndexSearcher searcher;
        private String indexDir="E:\lucene5\index";
        
        
        @Before
        public void setUp() throws IOException {
            IndexWriter indexWriter = getIndexWiter();
    
            for (int i = 0; i < ids.length; i++) {
                Document document = new Document();
                document.add(new IntField("id", ids[i], Field.Store.YES));
                document.add(new StringField("city", citys[i], Field.Store.YES));
                document.add(new StringField("desc", descs[i], Field.Store.NO));
                indexWriter.addDocument(document);
            }
    
            indexWriter.close();
            
            Directory dir=FSDirectory.open(Paths.get(indexDir));
            reader=DirectoryReader.open(dir);
            searcher=new IndexSearcher(reader);
            
        
        }
    
        /**
         * 实例化IndexWiter
         * 
         * @return
         * @throws IOException
         */
        private IndexWriter getIndexWiter() throws IOException {
            Directory dir = FSDirectory.open(Paths.get(indexDir));
            Analyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig conf = new IndexWriterConfig(analyzer);
            IndexWriter indexWriter = new IndexWriter(dir, conf);
    
            return indexWriter;
        }
    
        
        /**
         * 指定数字范围,其中id必须是整型的,如:document.add(new IntField("id", ids[i], Field.Store.YES));
         * newIntRange方法第二、三个参数是查询的起点值和终点值,即区间,最后两个参数是指是否包含起点值和终点值。
         * @throws Exception
         */
        @Test
        public void testNumericRangeQuery()throws Exception{
            NumericRangeQuery<Integer> query=NumericRangeQuery.newIntRange("id", 1, 2, true, true);
            TopDocs hits=searcher.search(query, 10);
            for(ScoreDoc scoreDoc:hits.scoreDocs){
                Document doc=searcher.doc(scoreDoc.doc);
                System.out.println(doc.get("id"));
                System.out.println(doc.get("city"));
                System.out.println(doc.get("desc"));
                System.out.println("---------------------");
            }        
        }
    
    }
    •  结果:

    解析:newIntRange("id", 1, 2, true, true); 该方法查询出id值为1和2之间的document,且包含1和2.因为desc设置为“Field.Store.NO”所以这里的值为null

    •  指定字符串开头搜索

        /**
         * 指定字符串开头搜索
         * @throws Exception
         */
        @Test
        public void testPrefixQuery()throws Exception{
            PrefixQuery query=new PrefixQuery(new Term("city","a"));
            TopDocs hits=searcher.search(query, 10);
            for(ScoreDoc scoreDoc:hits.scoreDocs){
                Document doc=searcher.doc(scoreDoc.doc);
                System.out.println(doc.get("id"));
                System.out.println(doc.get("city"));
                System.out.println(doc.get("desc"));
            }    
        }
        
    结果:

    解:查询出city中以a为开头的document

    • 多条件查询

        /**
         * 多条件查询
         * @throws Exception
         */
        @Test
        public void testBooleanQuery()throws Exception{
            NumericRangeQuery<Integer> query1=NumericRangeQuery.newIntRange("id", 1, 2, true, true);
            PrefixQuery query2=new PrefixQuery(new Term("city","a"));
            BooleanQuery.Builder booleanQuery=new BooleanQuery.Builder();
            booleanQuery.add(query1,BooleanClause.Occur.MUST);
            booleanQuery.add(query2,BooleanClause.Occur.MUST);
            TopDocs hits=searcher.search(booleanQuery.build(), 10);
            for(ScoreDoc scoreDoc:hits.scoreDocs){
                Document doc=searcher.doc(scoreDoc.doc);
                System.out.println(doc.get("id"));
                System.out.println(doc.get("city"));
                System.out.println(doc.get("desc"));
            }    
        }

    解:查询出id值为1和2之间且city值以a开头的document

  • 相关阅读:
    SQL Server创建复合索引时,复合索引列顺序对查询的性能影响
    SQL 查询性能优化----解决书签查找
    从源码分析 Spring 基于注解的事务
    jQuery最佳实践(不断更新中...)
    Java 8 LongAdders:管理并发计数器的正确方式
    Java中的显示锁 ReentrantLock 和 ReentrantReadWriteLock
    在IE8等不支持placeholder属性的浏览器中模拟placeholder效果
    颠覆式前端UI开发框架:React
    Whitecoin区块链钱包高级功能使用命令
    消息队列使用的四种场景介绍
  • 原文地址:https://www.cnblogs.com/shyroke/p/7930588.html
Copyright © 2020-2023  润新知