Search in sources :

Example 86 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project ddf by codice.

the class GeoNamesQueryLuceneIndex method doGetNearestCities.

protected List<NearbyLocation> doGetNearestCities(final Shape shape, final int radiusInKm, final int maxResults, final Directory directory) throws GeoEntryQueryException {
    notNull(shape, "GeoNamesQueryLuceneIndex.doGetNearestCities(): argument 'shape' may not be null.");
    if (radiusInKm <= 0) {
        throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetNearestCities(): radiusInKm must be positive.");
    }
    if (maxResults <= 0) {
        throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetNearestCities(): maxResults must be positive.");
    }
    if (directory == null) {
        return Collections.emptyList();
    }
    try (final IndexReader indexReader = createIndexReader(directory)) {
        final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
        final List<NearbyLocation> closestCities = new ArrayList<>();
        final Point center = shape.getCenter();
        final Query filter = createSpatialQuery(center, radiusInKm);
        // Query for all the documents in the index that are cities, then filter those
        // results for the ones that are in the search area.
        final BooleanQuery booleanQuery = new BooleanQuery.Builder().add(PPL_QUERY, BooleanClause.Occur.MUST).add(filter, BooleanClause.Occur.FILTER).build();
        final TopDocs topDocs = indexSearcher.search(booleanQuery, maxResults, SORT);
        if (topDocs.totalHits > 0) {
            for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                final double lat = Double.parseDouble(indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.LATITUDE_FIELD));
                final double lon = Double.parseDouble(indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.LONGITUDE_FIELD));
                final String name = indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.NAME_FIELD);
                final NearbyLocation city = new NearbyLocationImpl(center, new PointImpl(lon, lat, SPATIAL_CONTEXT), name);
                closestCities.add(city);
            }
        }
        return closestCities;
    } catch (IOException e) {
        throw new GeoEntryQueryException("Error reading the index", e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) ArrayList(java.util.ArrayList) Point(org.locationtech.spatial4j.shape.Point) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) IndexReader(org.apache.lucene.index.IndexReader) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 87 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project ddf by codice.

the class GeoNamesQueryLuceneIndex method doGetCountryCode.

protected String doGetCountryCode(Shape shape, int radiusInKm, Directory directory) throws GeoEntryQueryException {
    notNull(shape, "GeoNamesQueryLuceneIndex.doGetCountryCode(): argument 'shape' may not be null.");
    notNull(directory, "GeoNamesQueryLuceneIndex.doGetCountryCode(): argument 'directory' may not be null.");
    if (radiusInKm <= 0) {
        throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetCountryCode(): radiusInKm must be positive.");
    }
    try (final IndexReader indexReader = createIndexReader(directory)) {
        final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
        final Point center = shape.getCenter();
        final Query filter = createSpatialQuery(center, radiusInKm);
        final BooleanQuery booleanQuery = new BooleanQuery.Builder().add(filter, BooleanClause.Occur.FILTER).build();
        final TopDocs topDocs = indexSearcher.search(booleanQuery, 1, SORT);
        String countryCode = null;
        if (topDocs.totalHits > 0) {
            countryCode = indexSearcher.doc(topDocs.scoreDocs[0].doc).get(GeoNamesLuceneConstants.COUNTRY_CODE_FIELD);
        }
        return countryCode;
    } catch (IOException e) {
        throw new GeoEntryQueryException("Error reading the index", e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) IndexReader(org.apache.lucene.index.IndexReader) Point(org.locationtech.spatial4j.shape.Point) IOException(java.io.IOException)

Example 88 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project HongsCORE by ihongs.

the class LuceneRecord method getQuery.

/**
 * 查询分析
 * @param rd
 * @return
 * @throws HongsException
 */
public Query getQuery(Map rd) throws HongsException {
    Map<String, Map> fields = getFields();
    BooleanQuery.Builder qr = new BooleanQuery.Builder();
    for (Object o : rd.entrySet()) {
        Map.Entry e = (Map.Entry) o;
        Object fv = e.getValue();
        String fn = (String) e.getKey();
        // 自定义查询
        if (queried(qr, fn, fv)) {
            continue;
        }
        Map m = (Map) fields.get(fn);
        if (m == null) {
            continue;
        }
        if (fitrable(m) == false) {
            continue;
        }
        IQuery aq;
        String t = datatype(m);
        if ("int".equals(t)) {
            aq = new IntQuery();
        } else if ("long".equals(t)) {
            aq = new LongQuery();
        } else if ("float".equals(t)) {
            aq = new FloatQuery();
        } else if ("double".equals(t)) {
            aq = new DoubleQuery();
        } else if ("date".equals(t)) {
            aq = new LongQuery();
        } else if ("string".equals(t)) {
            aq = new StringQuery();
        } else if ("search".equals(t)) {
            aq = new SearchQuery();
        } else {
            continue;
        }
        qryAdd(qr, fn, fv, aq);
    }
    // 关键词
    if (rd.containsKey(Cnst.WD_KEY)) {
        Object fv = rd.get(Cnst.WD_KEY);
        fv = Synt.declare(fv, "");
        Set<String> fs = getSrchable();
        if (fv != null && !"".equals(fv)) {
            if (fs.size() > 1) {
                // 当设置了多个搜索字段时
                // 将条件整理为: +(fn1:xxx fn2:xxx)
                Map fw = new HashMap();
                fw.put(Cnst.SE_REL, fv);
                BooleanQuery.Builder qx = new BooleanQuery.Builder();
                for (String fk : fs) {
                    qryAdd(qx, fk, fw, new SearchQuery());
                }
                qr.add(qx.build(), BooleanClause.Occur.MUST);
            } else {
                for (String fk : fs) {
                    qryAdd(qr, fk, fv, new SearchQuery());
                }
            }
        }
    }
    // 或条件
    if (rd.containsKey(Cnst.OR_KEY)) {
        BooleanQuery.Builder qx = new BooleanQuery.Builder();
        Set<Map> set = Synt.asSet(rd.get(Cnst.OR_KEY));
        for (Map map : set) {
            qx.add(getQuery(map), BooleanClause.Occur.SHOULD);
        }
        qr.add(qx.build(), BooleanClause.Occur.MUST);
    }
    // 附条件
    if (rd.containsKey(Cnst.SR_KEY)) {
        Set<Map> set = Synt.asSet(rd.get(Cnst.SR_KEY));
        for (Map map : set) {
            qr.add(getQuery(map), BooleanClause.Occur.SHOULD);
        }
    }
    // 并条件
    if (rd.containsKey(Cnst.AR_KEY)) {
        Set<Map> set = Synt.asSet(rd.get(Cnst.AR_KEY));
        for (Map map : set) {
            qr.add(getQuery(map), BooleanClause.Occur.MUST);
        }
    }
    // 没有条件则查询全部
    BooleanQuery query = qr.build();
    if (query.clauses().isEmpty()) {
        return new MatchAllDocsQuery();
    }
    return query;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 89 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project jackrabbit-oak by apache.

the class LucenePropertyIndex method addReferenceConstraint.

private static void addReferenceConstraint(String uuid, List<Query> qs, IndexReader reader) {
    if (reader == null) {
        // getPlan call
        qs.add(new TermQuery(new Term("*", uuid)));
        return;
    }
    // reference query
    BooleanQuery bq = new BooleanQuery();
    Collection<String> fields = MultiFields.getIndexedFields(reader);
    for (String f : fields) {
        bq.add(new TermQuery(new Term(f, uuid)), SHOULD);
    }
    qs.add(bq);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Term(org.apache.lucene.index.Term) TermFactory.newAncestorTerm(org.apache.jackrabbit.oak.plugins.index.lucene.TermFactory.newAncestorTerm) FullTextTerm(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm) TermFactory.newPathTerm(org.apache.jackrabbit.oak.plugins.index.lucene.TermFactory.newPathTerm)

Example 90 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project jackrabbit-oak by apache.

the class LucenePropertyIndex method performAdditionalWraps.

/**
 * Perform additional wraps on the list of queries to allow, for example, the NOT CONTAINS to
 * play properly when sent to lucene.
 *
 * @param qs the list of queries. Cannot be null.
 * @return
 */
@Nonnull
public static LuceneRequestFacade<Query> performAdditionalWraps(@Nonnull List<Query> qs) {
    checkNotNull(qs);
    if (qs.size() == 1) {
        Query q = qs.get(0);
        if (q instanceof BooleanQuery) {
            BooleanQuery ibq = (BooleanQuery) q;
            boolean onlyNotClauses = true;
            for (BooleanClause c : ibq.getClauses()) {
                if (c.getOccur() != BooleanClause.Occur.MUST_NOT) {
                    onlyNotClauses = false;
                    break;
                }
            }
            if (onlyNotClauses) {
                // if we have only NOT CLAUSES we have to add a match all docs (*.*) for the
                // query to work
                ibq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
            }
        }
        return new LuceneRequestFacade<Query>(qs.get(0));
    }
    BooleanQuery bq = new BooleanQuery();
    for (Query q : qs) {
        boolean unwrapped = false;
        if (q instanceof BooleanQuery) {
            unwrapped = unwrapMustNot((BooleanQuery) q, bq);
        }
        if (!unwrapped) {
            bq.add(q, MUST);
        }
    }
    return new LuceneRequestFacade<Query>(bq);
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Nonnull(javax.annotation.Nonnull)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)297 TermQuery (org.apache.lucene.search.TermQuery)176 Term (org.apache.lucene.index.Term)144 Query (org.apache.lucene.search.Query)129 BooleanClause (org.apache.lucene.search.BooleanClause)89 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)70 BoostQuery (org.apache.lucene.search.BoostQuery)58 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)52 PhraseQuery (org.apache.lucene.search.PhraseQuery)50 ArrayList (java.util.ArrayList)47 TopDocs (org.apache.lucene.search.TopDocs)47 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)43 WildcardQuery (org.apache.lucene.search.WildcardQuery)42 IndexSearcher (org.apache.lucene.search.IndexSearcher)40 IndexReader (org.apache.lucene.index.IndexReader)39 PrefixQuery (org.apache.lucene.search.PrefixQuery)39 Test (org.junit.Test)39 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)38 Document (org.apache.lucene.document.Document)36 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)33