lucene 7.4.0

整体架构

常用field

常用query

1、引入maven支持

<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-core</artifactId>
    <version>7.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
<dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-queryparser</artifactId>
    <version>7.4.0</version>
</dependency>

2、添加索引库、文档库

/创建分词器
        Analyzer analyzer = new StandardAnalyzer();

        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
        IndexWriter writer=null;

        Path path=new File("C:\\Users\\star\\Desktop\\leceneData").toPath();
        try {
            Directory directory = FSDirectory.open(path);
            //创建IndexWriter
            writer=new IndexWriter(directory,indexWriterConfig);
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<GoodsEntity> goodsEntityList=goodsBLL.selectList(null);
        for (GoodsEntity goodsEntity : goodsEntityList) {
            Document document=new Document();

            Field id=new NumericDocValuesField("id",goodsEntity.getId().longValue());
            Field name=new TextField("name",goodsEntity.getName(), Field.Store.YES);
            Field price=new NumericDocValuesField("price",goodsEntity.getPrice().longValue());
            document.add(id);
            document.add(name);
            document.add(price);

            try {
                writer.addDocument(document);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

3、根据Query查询索引库得到文档库

/**
     * 通过query查询
     * @param query query子类对象
     * @param length 每页数量
     * @return
     */
    public static Map<String,Object> queryLucene(Query query,int length){
        Map<String,Object> rtnMap=new HashMap<>();
        List<Document> documentList=new ArrayList<>();

        try {
            //lucene存储的位置
            Path path=new File("C:\\Users\\star\\Desktop\\leceneData").toPath();
            //存储在文件中(可选内存)
            Directory directory = FSDirectory.open(path);
            //打开查询流
            IndexReader reader= DirectoryReader.open(directory);
            IndexSearcher searcher=new IndexSearcher(reader);
            //获取排序后的数据
            TopDocs topDocs = searcher.search(query,length);
            //总记录数
            long count=topDocs.totalHits;

            ScoreDoc[] scoreDocs=topDocs.scoreDocs;
            for (ScoreDoc scoreDoc : Arrays.asList(scoreDocs)) {
                //获取document
                int docId = scoreDoc.doc;
                Document document=searcher.doc(docId);
                documentList.add(document);
            }

            rtnMap.put("totalCount",count);
            rtnMap.put("data",documentList);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return rtnMap;
    }

4、创建Query、QueryParser查询索引库得到文档库

public static void query(){
        Query query=new TermQuery(new Term("name","联"));
        //Query query = FloatPoint.newRangeQuery("price",0F,200F);
        //Query query = IntPoint.newRangeQuery("id",1,100);
        BooleanQuery.Builder builder=new BooleanQuery.Builder();
        builder.add(query, BooleanClause.Occur.MUST);
        BooleanQuery booleanQuery=builder.build();

        Map<String,Object> dataMap=queryLucene(booleanQuery,1,10);
        showQueryResult(dataMap);
    }
public static void queryParser(){
        //Analyzer analyzer = new StandardAnalyzer();
        Analyzer analyzer = new IKAnalyzer();

        //设置field的boost值,默认1.0f (即相关度排序)
        Map<String,Float> boostMap=new HashMap<>();
        boostMap.put("name",200f);
        QueryParser queryParser= new MultiFieldQueryParser(new String[]{"name","price"},analyzer,boostMap);
        Query query=null;
        try {
            query=queryParser.parse("联想");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Map<String,Object> dataMap=queryLucene(query,1,10);
        showQueryResult(dataMap);
    }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值