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);
}
}
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);
}
}
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;
}
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);
}
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);
}
Aggregations