Lucene搜索引擎

1.maven添加依赖包

        <dependency>

            <groupId>org.apache.lucene</groupId>

            <artifactId>lucene-core</artifactId>

            <version>4.0.0</version>

        </dependency>

        <dependency>

            <groupId>org.apache.lucene</groupId>

            <artifactId>lucene-analyzers-common</artifactId>

            <version>4.0.0</version>

        </dependency>

        <dependency>

            <groupId>org.apache.lucene</groupId>

            <artifactId>lucene-queryparser</artifactId>

            <version>4.0.0</version>

        </dependency>

        <dependency>

            <groupId>org.apache.lucene</groupId>

            <artifactId>lucene-queries</artifactId>

            <version>4.0.0</version>

        </dependency>

2.索引基类

public void searchCreate(School school){
    content = "";
    try {
        Document document = new Document();
        content = school.getName()+school.getRegion().getName();
        document.add(new TextField("schoolIndex",content, Field.Store.YES));
        document.add(new TextField("schoolName",school.getName(), Field.Store.YES));
        document.add(new TextField("schoolId",school.getId().toString(), Field.Store.YES));
        document.add(new TextField("region",school.getRegion().getName().toString(),Field.Store.YES));
        document.add(new TextField("parentRegion",school.getRegion().getParent().getName().toString(),Field.Store.YES));
        indexWriter.addDocument(document);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

3.创建索引数据

public static String content = "";

private static String INDEX_DIR = "indexfile";
private static Analyzer analyzer = null;
private static Directory directory = null;
private static IndexWriter indexWriter = null;


/**
 * 创建索引
 * @param listSchool
 * @return
 */
public String createIndex(List<School> listSchool){

    StringBuilder sb = new StringBuilder();
    Date date = new Date();
    Long total = 0l;
    try {
        deletedIndex();
        analyzer = new StandardAnalyzer(Version.LUCENE_40);
        directory = FSDirectory.open(new File(INDEX_DIR));
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40,analyzer);
        indexWriter = new IndexWriter(directory,config);
        for(School school:listSchool){
            searchCreate(school);
            total++;
            System.out.println("create index number "+total);
        }
        indexWriter.commit();
        closeWriter();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Date date1 = new Date();
    sb.append("create index number "+total+" time cost "+(date1.getTime()-date.getTime()));
    System.out.println("create index number "+total+" time cost "+(date1.getTime()-date.getTime()));
    return sb.toString();
}

4.拼装数据返回结果

/**
     * 查找索引,返回符合条件的学校名称组合字符串
     * @param text   查找的学校名称
     */
    public String searchIndex(String text){

        JsonArray jsonArray = new JsonArray();
        try {
            directory = FSDirectory.open(new File(INDEX_DIR));
            analyzer = new StandardAnalyzer(Version.LUCENE_40);
            DirectoryReader directoryReader = DirectoryReader.open(directory);
            IndexSearcher indexSearcher = new IndexSearcher(directoryReader);

            QueryParser parser = new QueryParser(Version.LUCENE_40,"schoolIndex",analyzer);
            parser.setDefaultOperator(QueryParser.Operator.OR);
            Query query = parser.parse(text);

            Sort sort=new Sort();

            ScoreDoc[] hits = indexSearcher.search(query,null,50,sort).scoreDocs;

            for(int i=0;i<hits.length;i++){
                Document hitDoc = indexSearcher.doc(hits[i].doc);
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("schoolName",hitDoc.get("schoolName"));
                jsonObject.addProperty("region",hitDoc.get("region"));
                jsonObject.addProperty("schoolId",hitDoc.get("schoolId"));
                jsonObject.addProperty("parentRegion",hitDoc.get("parentRegion"));
                jsonArray.add(jsonObject);
            }
            directoryReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return jsonArray.toString();
    }

5.删除索引

/**
 * 删除索引
 * @throws IOException
 */
public void deletedIndex(){
    Date date = new Date();
    try {
        analyzer = new StandardAnalyzer(Version.LUCENE_40);
        directory = FSDirectory.open(new File(INDEX_DIR));
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
        indexWriter = new IndexWriter(directory, config);

        indexWriter.deleteAll();
        indexWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Date date1 = new Date();
    System.out.println("delete index cost time "+(date1.getTime()-date.getTime())+"ms\n");
}

转载于:https://my.oschina.net/u/2546238/blog/968135

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值