Search in sources :

Example 1 with SimpleAnalyzer

use of org.apache.lucene.analysis.SimpleAnalyzer in project collect by openforis.

the class RecordIndexManager method createQuery.

protected Query createQuery(SearchType searchType, String indexFieldKey, String searchText) throws ParseException {
    String escapedSearchText = QueryParser.escape(searchText.trim().toLowerCase(Locale.ENGLISH));
    String queryText = escapedSearchText;
    if (StringUtils.isNotBlank(queryText)) {
        switch(searchType) {
            case STARTS_WITH:
                queryText = escapedSearchText + "*";
                break;
            case CONTAINS:
                // queryText = "*" + escapedSearchText + "*"; TODO support CONTAINS query
                queryText = escapedSearchText + "*";
                break;
            default:
                queryText = escapedSearchText;
        }
    }
    SimpleAnalyzer analyzer = new SimpleAnalyzer(LUCENE_VERSION);
    QueryParser queryParser = new QueryParser(LUCENE_VERSION, indexFieldKey, analyzer);
    queryParser.setDefaultOperator(Operator.AND);
    Query query = queryParser.parse(queryText);
    return query;
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) Query(org.apache.lucene.search.Query) SimpleAnalyzer(org.apache.lucene.analysis.SimpleAnalyzer)

Example 2 with SimpleAnalyzer

use of org.apache.lucene.analysis.SimpleAnalyzer in project collect by openforis.

the class RecordIndexManager method createIndexWriter.

protected IndexWriter createIndexWriter() throws RecordIndexException {
    try {
        SimpleAnalyzer analyzer = new SimpleAnalyzer(LUCENE_VERSION);
        IndexWriterConfig conf = new IndexWriterConfig(LUCENE_VERSION, analyzer);
        conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
        IndexWriter indexWriter = new IndexWriter(indexDirectory, conf);
        return indexWriter;
    } catch (IOException e) {
        throw new RecordIndexException(e.getMessage(), e);
    }
}
Also used : SimpleAnalyzer(org.apache.lucene.analysis.SimpleAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) IOException(java.io.IOException) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

SimpleAnalyzer (org.apache.lucene.analysis.SimpleAnalyzer)2 IOException (java.io.IOException)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 QueryParser (org.apache.lucene.queryParser.QueryParser)1 Query (org.apache.lucene.search.Query)1