Lucene笔记27-Lucene的使用-自定义QueryParser解决日期和数字范围问题

一、需求说明

Lucene提供的getRangeQuery(String field, String part1, String part2, boolean inclusive)方法支持的是String,假设我们需要对数字范围查询,那么就需要我们自己来改写了。

二、代码实现

重写getRangeQuery()方法。searchByQuery()方法将传入的查询字符串进行parse,根据转化后的样式,调用到了被重写的getRangeQuery()方法,从而走了里面的逻辑完成查询。

package com.wsy;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class CustomParser extends QueryParser {
    private static Directory directory;
    private static IndexReader indexReader;
    private static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);

    static {
        try {
            directory = FSDirectory.open(new File("E:\\Lucene\\IndexLibrary"));
            indexReader = IndexReader.open(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public CustomParser(Version matchVersion, String f, Analyzer a) {
        super(matchVersion, f, a);
    }

    @Override
    protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException {
        if (field.equals("size")) {
            return NumericRangeQuery.newIntRange(field, Integer.parseInt(part1), Integer.parseInt(part2), inclusive, inclusive);
        } else if (field.equals("date")) {
            Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
            if (pattern.matcher(part1).matches() && pattern.matcher(part2).matches()) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    long start = simpleDateFormat.parse(part1).getTime();
                    long end = simpleDateFormat.parse(part2).getTime();
                    return NumericRangeQuery.newLongRange(field, start, end, inclusive, inclusive);
                } catch (java.text.ParseException e) {
                    e.printStackTrace();
                }
            } else {
                throw new ParseException("要检索的日期格式不对,请使用yyyy-MM-dd格式");
            }
        }
        return super.getRangeQuery(field, part1, part2, inclusive);
    }

    public void searchByQuery(String value) {
        try {
            CustomParser customParser = new CustomParser(Version.LUCENE_35, "content", analyzer);
            Query query = customParser.parse(value);
            System.out.println("query:\t" + query);
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            TopDocs topDocs = indexSearcher.search(query, 100);
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (ScoreDoc scoreDoc : scoreDocs) {
                Document document = indexSearcher.doc(scoreDoc.doc);
                // 依次输出文档id,文档得分,文档名字,文档路径,文档大小,文档修改时间
                System.out.println(scoreDoc.doc + "-->" + scoreDoc.score + "-->" + document.get("fileName") + "-->" + document.get("path") + "-->" + document.get("size") + "-->" + simpleDateFormat.format(new Date(Long.valueOf(document.get("date")))));
            }
            indexSearcher.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            System.err.println(e.getMessage());
        }
    }

    public static void main(String[] args) {
        CustomParser customParser = new CustomParser(Version.LUCENE_35, "content", analyzer);
        // 查询size是300到800的
        customParser.searchByQuery("size:[300 TO 800]");
        // 查询date是2018-01-01到2018-12-31的
        customParser.searchByQuery("date:[2018-01-01 TO 2018-12-31]");
    }
}

 

你可能感兴趣的:(Lucene)