Search in sources :

Example 1 with ParseException

use of org.apache.lucene.queryparser.classic.ParseException in project zeppelin by apache.

the class LuceneSearch method query.

/* (non-Javadoc)
   * @see org.apache.zeppelin.search.Search#query(java.lang.String)
   */
@Override
public List<Map<String, String>> query(String queryStr) {
    if (null == ramDirectory) {
        throw new IllegalStateException("Something went wrong on instance creation time, index dir is null");
    }
    List<Map<String, String>> result = Collections.emptyList();
    try (IndexReader indexReader = DirectoryReader.open(ramDirectory)) {
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        Analyzer analyzer = new StandardAnalyzer();
        MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] { SEARCH_FIELD_TEXT, SEARCH_FIELD_TITLE }, analyzer);
        Query query = parser.parse(queryStr);
        LOG.debug("Searching for: " + query.toString(SEARCH_FIELD_TEXT));
        SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
        Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
        result = doSearch(indexSearcher, query, analyzer, highlighter);
        indexReader.close();
    } catch (IOException e) {
        LOG.error("Failed to open index dir {}, make sure indexing finished OK", ramDirectory, e);
    } catch (ParseException e) {
        LOG.error("Failed to parse query " + queryStr, e);
    }
    return result;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) Query(org.apache.lucene.search.Query) WildcardQuery(org.apache.lucene.search.WildcardQuery) QueryScorer(org.apache.lucene.search.highlight.QueryScorer) IOException(java.io.IOException) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) IndexReader(org.apache.lucene.index.IndexReader) ParseException(org.apache.lucene.queryparser.classic.ParseException) SimpleHTMLFormatter(org.apache.lucene.search.highlight.SimpleHTMLFormatter) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Highlighter(org.apache.lucene.search.highlight.Highlighter)

Example 2 with ParseException

use of org.apache.lucene.queryparser.classic.ParseException in project OpenGrok by OpenGrok.

the class SearchHelper method prepareExec.

/**
     * Create the searcher to use wrt. to currently set parameters and the given
     * projects. Does not produce any {@link #redirect} link. It also does
     * nothing if {@link #redirect} or {@link #errorMsg} have a
     * none-{@code null} value.
     * <p>
     * Parameters which should be populated/set at this time: <ul>
     * <li>{@link #builder}</li> <li>{@link #dataRoot}</li>
     * <li>{@link #order} (falls back to relevance if unset)</li>
     * <li>{@link #parallel} (default: false)</li> </ul> Populates/sets: <ul>
     * <li>{@link #query}</li> <li>{@link #searcher}</li> <li>{@link #sort}</li>
     * <li>{@link #projects}</li> <li>{@link #errorMsg} if an error occurs</li>
     * </ul>
     *
     * @param projects project to use query. If empty, a no-project setup
     * is assumed (i.e. DATA_ROOT/index will be used instead of possible
     * multiple DATA_ROOT/$project/index).
     * @return this instance
     */
public SearchHelper prepareExec(SortedSet<String> projects) {
    if (redirect != null || errorMsg != null) {
        return this;
    }
    // the Query created by the QueryBuilder
    try {
        indexDir = new File(dataRoot, IndexDatabase.INDEX_DIR);
        query = builder.build();
        if (projects == null) {
            errorMsg = "No project selected!";
            return this;
        }
        this.projects = projects;
        if (projects.isEmpty()) {
            // no project setup
            FSDirectory dir = FSDirectory.open(indexDir.toPath());
            searcher = new IndexSearcher(DirectoryReader.open(dir));
            closeOnDestroy = true;
        } else {
            // We use MultiReader even for single project. This should
            // not matter given that MultiReader is just a cheap wrapper
            // around set of IndexReader objects.
            closeOnDestroy = false;
            MultiReader multireader = RuntimeEnvironment.getInstance().getMultiReader(projects, searcherList);
            if (multireader != null) {
                searcher = new IndexSearcher(multireader);
            } else {
                errorMsg = "Failed to initialize search. Check the index.";
            }
        }
        // Most probably they are not reused. SearcherLifetimeManager might help here.
        switch(order) {
            case LASTMODIFIED:
                sort = new Sort(new SortField(QueryBuilder.DATE, SortField.Type.STRING, true));
                break;
            case BY_PATH:
                sort = new Sort(new SortField(QueryBuilder.FULLPATH, SortField.Type.STRING));
                break;
            default:
                sort = Sort.RELEVANCE;
                break;
        }
        checker = new DirectSpellChecker();
    } catch (ParseException e) {
        errorMsg = PARSE_ERROR_MSG + e.getMessage();
    } catch (FileNotFoundException e) {
        //          errorMsg = "Index database(s) not found: " + e.getMessage();
        errorMsg = "Index database(s) not found.";
    } catch (IOException e) {
        errorMsg = e.getMessage();
    }
    return this;
}
Also used : SuperIndexSearcher(org.opensolaris.opengrok.configuration.SuperIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) MultiReader(org.apache.lucene.index.MultiReader) FileNotFoundException(java.io.FileNotFoundException) Sort(org.apache.lucene.search.Sort) FSDirectory(org.apache.lucene.store.FSDirectory) SortField(org.apache.lucene.search.SortField) ParseException(org.apache.lucene.queryparser.classic.ParseException) IOException(java.io.IOException) File(java.io.File) DirectSpellChecker(org.apache.lucene.search.spell.DirectSpellChecker)

Example 3 with ParseException

use of org.apache.lucene.queryparser.classic.ParseException in project titan by thinkaurelius.

the class LuceneIndex method query.

@Override
public Iterable<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    Query q;
    try {
        q = new QueryParser("_all", analyzer).parse(query.getQuery());
    } catch (ParseException e) {
        throw new PermanentBackendException("Could not parse raw query: " + query.getQuery(), e);
    }
    try {
        IndexSearcher searcher = ((Transaction) tx).getSearcher(query.getStore());
        //Index does not yet exist
        if (searcher == null)
            return ImmutableList.of();
        long time = System.currentTimeMillis();
        //TODO: can we make offset more efficient in Lucene?
        final int offset = query.getOffset();
        int adjustedLimit = query.hasLimit() ? query.getLimit() : Integer.MAX_VALUE - 1;
        if (adjustedLimit < Integer.MAX_VALUE - 1 - offset)
            adjustedLimit += offset;
        else
            adjustedLimit = Integer.MAX_VALUE - 1;
        TopDocs docs = searcher.search(q, adjustedLimit);
        log.debug("Executed query [{}] in {} ms", q, System.currentTimeMillis() - time);
        List<RawQuery.Result<String>> result = new ArrayList<RawQuery.Result<String>>(docs.scoreDocs.length);
        for (int i = offset; i < docs.scoreDocs.length; i++) {
            result.add(new RawQuery.Result<String>(searcher.doc(docs.scoreDocs[i].doc).getField(DOCID).stringValue(), docs.scoreDocs[i].score));
        }
        return result;
    } catch (IOException e) {
        throw new TemporaryBackendException("Could not execute Lucene query", e);
    }
}
Also used : IOException(java.io.IOException) Point(com.spatial4j.core.shape.Point) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 4 with ParseException

use of org.apache.lucene.queryparser.classic.ParseException in project textdb by TextDB.

the class FuzzyTokenMatcherSourceOperator method createLuceneQueryObject.

public static Query createLuceneQueryObject(FuzzyTokenPredicate predicate) throws DataFlowException {
    try {
        /*
             * By default the boolean query takes 1024 # of clauses as the max
             * limit. Since our input query has no limitaion on the number of
             * tokens, we have to put a check.
             */
        if (predicate.getThreshold() > 1024)
            BooleanQuery.setMaxClauseCount(predicate.getThreshold() + 1);
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.setMinimumNumberShouldMatch(predicate.getThreshold());
        MultiFieldQueryParser qp = new MultiFieldQueryParser(predicate.getAttributeNames().stream().toArray(String[]::new), LuceneAnalyzerConstants.getLuceneAnalyzer(predicate.getLuceneAnalyzerStr()));
        for (String s : predicate.getQueryTokens()) {
            builder.add(qp.parse(s), Occur.SHOULD);
        }
        return builder.build();
    } catch (ParseException e) {
        throw new DataFlowException(e);
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) DataFlowException(edu.uci.ics.textdb.api.exception.DataFlowException) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 5 with ParseException

use of org.apache.lucene.queryparser.classic.ParseException in project geode by apache.

the class LuceneIndexCreationIntegrationTest method canCreateLuceneIndexForRegionWithEvictionWithOverflowToDisk.

@Test
public void canCreateLuceneIndexForRegionWithEvictionWithOverflowToDisk() throws IOException, ParseException {
    try {
        createIndex("field1", "field2", "field3");
        RegionFactory regionFactory = this.cache.createRegionFactory(RegionShortcut.PARTITION);
        regionFactory.setEvictionAttributes(EvictionAttributes.createLRUHeapAttributes(null, EvictionAction.OVERFLOW_TO_DISK));
        regionFactory.create(REGION_NAME);
    } catch (Exception e) {
        fail("Should have been able to create Lucene Index");
        e.printStackTrace();
    }
}
Also used : RegionFactory(org.apache.geode.cache.RegionFactory) ParseException(org.apache.lucene.queryparser.classic.ParseException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) BucketNotFoundException(org.apache.geode.internal.cache.BucketNotFoundException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

ParseException (org.apache.lucene.queryparser.classic.ParseException)20 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)12 Query (org.apache.lucene.search.Query)11 IOException (java.io.IOException)9 BooleanQuery (org.apache.lucene.search.BooleanQuery)6 ArrayList (java.util.ArrayList)5 TermQuery (org.apache.lucene.search.TermQuery)5 Analyzer (org.apache.lucene.analysis.Analyzer)4 IndexReader (org.apache.lucene.index.IndexReader)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 WildcardQuery (org.apache.lucene.search.WildcardQuery)4 Document (org.apache.lucene.document.Document)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 PrefixQuery (org.apache.lucene.search.PrefixQuery)3 Map (java.util.Map)2 FullTextExpression (org.apache.jackrabbit.oak.query.fulltext.FullTextExpression)2 PropertyRestriction (org.apache.jackrabbit.oak.spi.query.Filter.PropertyRestriction)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 CustomScoreQuery (org.apache.lucene.queries.CustomScoreQuery)2 MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)2