初试 Lucene 2

编程技术  /  houtizong 发布于 3年前   79
其他代码与添加一致,在添加 doc 时改为如下代码即可
indexWriter.updateDocument(new Term("id",index.getId()), document);
4、搜索 按照单个单词搜索
        List<IndexModel> indexModels = new ArrayList<IndexModel>();
        Directory directory = null;
        try{
            directory = FSDirectory.open(indexDir);
            IndexSearcher indexSearcher = new IndexSearcher(directory);
            //下面的是进行title,content 两个范围内进行收索.
            BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD,BooleanClause.Occur.SHOULD ,BooleanClause.Occur.SHOULD};
            Query queryOBJ = MultiFieldQueryParser.parse(Version.LUCENE_34, primarkStr, new String[]{"name","password","id"}, clauses, new StandardAnalyzer(Version.LUCENE_34));//parser.parse(query);
            Filter filter = null;
           
            //################# 搜索相似度最高的记录 ###################
            TopDocs topDocs = indexSearcher.search(queryOBJ, filter, 1000);
            //TopDocs topDocs = indexSearcher.search(queryOBJ , 10000);
            System.out.println("*** 共匹配:" + topDocs.totalHits + "个 ***");
            IndexModel indexModel = null;
            //输出结果
            for (ScoreDoc scoreDoc : topDocs.scoreDocs){
                    Document targetDoc = indexSearcher.doc(scoreDoc.doc);
                    indexModel = new IndexModel();
                    //设置高亮显示格式
                    SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='red'><strong>", "</strong></font>");
                    /* 语法高亮显示设置 */
                    Highlighter highlighter = new Highlighter(simpleHTMLFormatter,new QueryScorer(queryOBJ));
                    highlighter.setTextFragmenter(new SimpleFragmenter(100));
                    String name;
                    name = targetDoc.get("name");
                    String createTime;
                    createTime = targetDoc.get("createTime");
                    String id;
                    id = targetDoc.get("id");
                    if(createTime == null){
                        createTime = "";
                    }
                    if(name == null){
                        name = "";
                    }
                    if(id == null){
                        id = "";
                    }
                    TokenStream titleTokenStream = analyzer.tokenStream("name",new StringReader(name));
                    TokenStream contentTokenStream = analyzer.tokenStream("createTime",new StringReader(createTime));
                    TokenStream idTokenStream = analyzer.tokenStream("id", new StringReader(id));
                    String highLightTitle = highlighter.getBestFragment(titleTokenStream, name);
                    String highLightContent = highlighter.getBestFragment(contentTokenStream, createTime);
                    String highLightid = highlighter.getBestFragment(idTokenStream, id);
                     if(highLightTitle == null)
                         highLightTitle = name;
   
                     if(highLightContent == null)
                         highLightContent = createTime;
                    
                     if(highLightid == null)
                         highLightid = id;
                    indexModels.add(indexModel);
            }
            indexSearcher.close();
            return indexModels;
           
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
上一篇:java

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客