private OpenBitSet searchRequestToBitSet(@Nullable final Search searchreq, IndexSearcher searcher, IndexReader reader) throws IOException { if( searchreq != null ) { Filter filters = getFilter(searchreq); Query query = getQuery(searchreq, null, false); BitSetCollector collector = new BitSetCollector(); searcher.search(query, filters, collector); return collector.getBitSet(); } else { return (OpenBitSet) new InstitutionFilter().getDocIdSet(reader); } }
@Override public Filter getFilter(Element e) throws ParserException { List<BytesRef> terms = new ArrayList<>(); String text = DOMUtils.getNonBlankTextOrFail(e); String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName"); TokenStream ts = null; try { ts = analyzer.tokenStream(fieldName, text); TermToBytesRefAttribute termAtt = ts.addAttribute(TermToBytesRefAttribute.class); BytesRef bytes = termAtt.getBytesRef(); ts.reset(); while (ts.incrementToken()) { termAtt.fillBytesRef(); terms.add(BytesRef.deepCopyOf(bytes)); } ts.end(); } catch (IOException ioe) { throw new RuntimeException("Error constructing terms from index:" + ioe); } finally { IOUtils.closeWhileHandlingException(ts); } return new TermsFilter(fieldName, terms); }
@Override public Filter getFilter(Element e) throws ParserException { BooleanFilter bf = new BooleanFilter(); NodeList nl = e.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node.getNodeName().equals("Clause")) { Element clauseElem = (Element) node; BooleanClause.Occur occurs = BooleanQueryBuilder.getOccursValue(clauseElem); Element clauseFilter = DOMUtils.getFirstChildOrFail(clauseElem); Filter f = factory.getFilter(clauseFilter); bf.add(new FilterClause(f, occurs)); } } return bf; }
@Override public Filter getFilter(Element e) throws ParserException { String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName"); DuplicateFilter df = new DuplicateFilter(fieldName); String keepMode = DOMUtils.getAttribute(e, "keepMode", "first"); if (keepMode.equalsIgnoreCase("first")) { df.setKeepMode(DuplicateFilter.KeepMode.KM_USE_FIRST_OCCURRENCE); } else if (keepMode.equalsIgnoreCase("last")) { df.setKeepMode(DuplicateFilter.KeepMode.KM_USE_LAST_OCCURRENCE); } else { throw new ParserException("Illegal keepMode attribute in DuplicateFilter:" + keepMode); } String processingMode = DOMUtils.getAttribute(e, "processingMode", "full"); if (processingMode.equalsIgnoreCase("full")) { df.setProcessingMode(DuplicateFilter.ProcessingMode.PM_FULL_VALIDATION); } else if (processingMode.equalsIgnoreCase("fast")) { df.setProcessingMode(DuplicateFilter.ProcessingMode.PM_FAST_INVALIDATION); } else { throw new ParserException("Illegal processingMode attribute in DuplicateFilter:" + processingMode); } return df; }
@Override protected Filter construct(LindenFilter lindenFilter, LindenConfig config) throws Exception { List<LindenBooleanSubFilter> booleanSubFilterList = lindenFilter.getBooleanFilter().getFilters(); BooleanFilter booleanFilter = new BooleanFilter(); for (LindenBooleanSubFilter booleanSubFilter : booleanSubFilterList) { LindenFilter subFilter = booleanSubFilter.getFilter(); switch (booleanSubFilter.clause) { case MUST: booleanFilter.add(FilterConstructor.constructFilter(subFilter, config), BooleanClause.Occur.MUST); continue; case SHOULD: booleanFilter.add(FilterConstructor.constructFilter(subFilter, config), BooleanClause.Occur.SHOULD); continue; case MUST_NOT: booleanFilter.add(FilterConstructor.constructFilter(subFilter, config), BooleanClause.Occur.MUST_NOT); } } return booleanFilter; }
public static Filter constructFilter(LindenFilter lindenFilter, LindenConfig config) throws Exception { if (lindenFilter == null) { return null; } if (lindenFilter.isSetTermFilter()) { return TERM_FILTER_CONSTRUCTOR.construct(lindenFilter, config); } else if (lindenFilter.isSetRangeFilter()) { return RANGE_FILTER_CONSTRUCTOR.construct(lindenFilter, config); } else if (lindenFilter.isSetQueryFilter()) { return QUERY_FILTER_CONSTRUCTOR.construct(lindenFilter, config); } else if (lindenFilter.isSetBooleanFilter()) { return BOOLEAN_FILTER_CONSTRUCTOR.construct(lindenFilter, config); } else if (lindenFilter.isSetSpatialFilter()) { return SPATIAL_FILTER_CONSTRUCTOR.construct(lindenFilter, config); } else if (lindenFilter.isSetNotNullFieldFilter()) { return NOT_NULL_FIELD_FILTER_CONSTRUCTOR.construct(lindenFilter, config); } return null; }
/** * Returns a filter that forces a given field to be one of the given values */ public static Filter terms(final String field, final Collection<?> values) { if (CollectionUtils.isEmpty(values)) { return null; } final TermsFilter filter = new TermsFilter(); int count = 0; for (final Object object : values) { final String term = object == null ? null : StringUtils.trimToNull("" + object); if (term != null) { /* todo addterm*/ //filter.addTerm(new Term(field, term)); count++; } } return count == 0 ? null : filter; }
private Optional<Query> limitingFilter(Query query, boolean isNegated) { if (query instanceof SpanQuery) { return limitingFilterForSpan((SpanQuery) query, isNegated); } else if (query instanceof Filter) { return Optional.of(query); } else if (query instanceof BooleanQuery) { return boolQuery((BooleanQuery) query, isNegated); } else if (query instanceof TermQuery) { return Optional.of(query); } else if (query instanceof PhraseQuery) { return phraseFilter((PhraseQuery) query, isNegated); } else if (query instanceof MultiTermQuery) { return Optional.of(query); } else if (query instanceof WildcardPhraseQuery) { return wildcardPhraseFilter((WildcardPhraseQuery) query, isNegated); } else if (query instanceof ToParentBlockJoinQuery) { //This can be really bad for performance, if the nested query contains expensive operations (phrases/spans) //On the other hand, it is only slow if the field actually has any data, and we currently do not have // any data in the only nested text field (enrichments.sentences) return Optional.of(query); } else { //This should never happen, but if it does, it might be really bad for performance //logger.warn("failed to limit query, this should never happen. Query : [{}]", query.toString()); return Optional.of(query); } }
/** * Returns a list of location near a certain coordinate. * @param latitude, @param longitude - Center of search area * @param distanceInMiles - Search Radius in miles * @param indexerPath - Path to Lucene index * @param count - Upper bound to number of results * @return - List of locations sorted by population * @throws IOException */ public List<Location> searchNearby(Double latitude, Double longitude, Double distanceInMiles, String indexerPath, int count) throws IOException { double distanceInDeg = DistanceUtils.dist2Degrees(distanceInMiles,DistanceUtils.EARTH_EQUATORIAL_RADIUS_MI); SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.IsWithin, ctx.makeCircle(longitude,latitude, distanceInDeg)); String key = latitude+"-"+longitude; Filter filter = strategy.makeFilter(spatialArgs); IndexSearcher searcher = new IndexSearcher(createIndexReader(indexerPath)); Sort sort = new Sort(populationSort); TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), filter, count, sort); ScoreDoc[] scoreDocs = topDocs.scoreDocs; HashMap<String, List<Location>> allCandidates = new HashMap<String, List<Location>>(); getMatchingCandidates(searcher, allCandidates, key, scoreDocs); List<Location> results = allCandidates.get(key); return results; }
@Override public SpatialQueryContext build(Map<String, Object> query) throws Exception { Shape shape = parseShape(query); System.out.println("qui:: " + shape); SpatialStrategy strategy = manager.strategy(); if (isOnlyBB(strategy)) { shape = shape.getBoundingBox(); } SpatialArgs args1 = new SpatialArgs(SpatialOperation.IsWithin, shape); // SpatialArgs args2 = new SpatialArgs(SpatialOperation., shape); Geometry geo = OShapeFactory.INSTANCE.toGeometry(shape); Filter filter = strategy.makeFilter(args1); return new SpatialQueryContext(null, manager.searcher(), new MatchAllDocsQuery(), filter); }
public void testGetFilterHandleNumericParseError() throws Exception { NumericRangeFilterBuilder filterBuilder = new NumericRangeFilterBuilder(); filterBuilder.setStrictMode(false); String xml = "<NumericRangeFilter fieldName='AGE' type='int' lowerTerm='-1' upperTerm='NaN'/>"; Document doc = getDocumentFromString(xml); Filter filter = filterBuilder.getFilter(doc.getDocumentElement()); Directory ramDir = newDirectory(); IndexWriter writer = new IndexWriter(ramDir, newIndexWriterConfig(null)); writer.commit(); try { AtomicReader reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(ramDir)); try { assertNull(filter.getDocIdSet(reader.getContext(), reader.getLiveDocs())); } finally { reader.close(); } } finally { writer.commit(); writer.close(); ramDir.close(); } }
protected Query makeQueryFromShape(Shape shape) { SpatialArgs args = new SpatialArgs(operation, shape); if (!Double.isNaN(distErrPct)) args.setDistErrPct(distErrPct); if (score) { ValueSource valueSource = strategy.makeDistanceValueSource(shape.getCenter()); return new CustomScoreQuery(strategy.makeQuery(args), new FunctionQuery(valueSource)); } else { //strategy.makeQuery() could potentially score (isn't well defined) so instead we call // makeFilter() and wrap Filter filter = strategy.makeFilter(args); if (filter instanceof QueryWrapperFilter) { return ((QueryWrapperFilter)filter).getQuery(); } else { return new ConstantScoreQuery(filter); } } }
public void testSingleFilter() throws Exception { ChainedFilter chain = getChainedFilter(new Filter[] {dateFilter}, null); int numHits = searcher.search(query, chain, 1000).totalHits; assertEquals(MAX, numHits); chain = new ChainedFilter(new Filter[] {bobFilter}); numHits = searcher.search(query, chain, 1000).totalHits; assertEquals(MAX / 2, numHits); chain = getChainedFilter(new Filter[] {bobFilter}, new int[] {ChainedFilter.AND}); TopDocs hits = searcher.search(query, chain, 1000); numHits = hits.totalHits; assertEquals(MAX / 2, numHits); assertEquals("bob", searcher.doc(hits.scoreDocs[0].doc).get("owner")); chain = getChainedFilter(new Filter[] {bobFilter}, new int[] {ChainedFilter.ANDNOT}); hits = searcher.search(query, chain, 1000); numHits = hits.totalHits; assertEquals(MAX / 2, numHits); assertEquals("sue", searcher.doc(hits.scoreDocs[0].doc).get("owner")); }
public void testANDNOT() throws Exception { ChainedFilter chain = getChainedFilter( new Filter[]{dateFilter, sueFilter}, new int[] {ChainedFilter.AND, ChainedFilter.ANDNOT}); TopDocs hits = searcher.search(query, chain, 1000); assertEquals("ANDNOT matches just bob", MAX / 2, hits.totalHits); assertEquals("bob", searcher.doc(hits.scoreDocs[0].doc).get("owner")); chain = getChainedFilter( new Filter[]{bobFilter, bobFilter}, new int[] {ChainedFilter.ANDNOT, ChainedFilter.ANDNOT}); hits = searcher.search(query, chain, 1000); assertEquals("ANDNOT bob ANDNOT bob matches all sues", MAX / 2, hits.totalHits); assertEquals("sue", searcher.doc(hits.scoreDocs[0].doc).get("owner")); }
public void testWithCachingFilter() throws Exception { Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir); IndexReader reader = writer.getReader(); writer.close(); IndexSearcher searcher = newSearcher(reader); Query query = new TermQuery(new Term("none", "none")); QueryWrapperFilter queryFilter = new QueryWrapperFilter(query); CachingWrapperFilter cachingFilter = new CachingWrapperFilter(queryFilter); searcher.search(query, cachingFilter, 1); CachingWrapperFilter cachingFilter2 = new CachingWrapperFilter(queryFilter); Filter[] chain = new Filter[2]; chain[0] = cachingFilter; chain[1] = cachingFilter2; ChainedFilter cf = new ChainedFilter(chain); // throws java.lang.ClassCastException: org.apache.lucene.util.OpenBitSet cannot be cast to java.util.BitSet searcher.search(new MatchAllDocsQuery(), cf, 1); reader.close(); dir.close(); }
@Override public Filter makeFilter(SpatialArgs args) { final SpatialOperation op = args.getOperation(); if (op != SpatialOperation.Intersects) throw new UnsupportedSpatialOperation(op); Shape shape = args.getShape(); int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, distErrPct)); List<Cell> cells = grid.getCells(shape, detailLevel, false,//no parents true);//simplify BytesRef[] terms = new BytesRef[cells.size()]; int i = 0; for (Cell cell : cells) { terms[i++] = new BytesRef(cell.getTokenString());//TODO use cell.getTokenBytes() } return new TermsFilter(getFieldName(), terms); }
public void testQueries() throws Exception { int n = atLeast(4); for (int i = 0; i < n; i++) { Filter odd = new QueryWrapperFilter(new TermQuery(new Term("oddeven", "odd"))); assertQuery(new MatchAllDocsQuery(), null); assertQuery(new TermQuery(new Term("english", "one")), null); assertQuery(new MatchAllDocsQuery(), odd); assertQuery(new TermQuery(new Term("english", "four")), odd); BooleanQuery bq = new BooleanQuery(); bq.add(new TermQuery(new Term("english", "one")), BooleanClause.Occur.SHOULD); bq.add(new TermQuery(new Term("oddeven", "even")), BooleanClause.Occur.SHOULD); assertQuery(bq, null); // force in order bq.add(new TermQuery(new Term("english", "two")), BooleanClause.Occur.SHOULD); bq.setMinimumNumberShouldMatch(2); assertQuery(bq, null); } }
void assertQuery(Query query, Filter filter) throws Exception { for (int i = 0; i < 10; i++) { boolean reversed = random().nextBoolean(); SortField fields[] = new SortField[] { new SortField("int", SortField.Type.INT, reversed), new SortField("long", SortField.Type.LONG, reversed), new SortField("float", SortField.Type.FLOAT, reversed), new SortField("double", SortField.Type.DOUBLE, reversed), new SortField("intdocvalues", SortField.Type.INT, reversed), new SortField("floatdocvalues", SortField.Type.FLOAT, reversed), new SortField("score", SortField.Type.SCORE) }; Collections.shuffle(Arrays.asList(fields), random()); int numSorts = TestUtil.nextInt(random(), 1, fields.length); assertQuery(query, filter, new Sort(Arrays.copyOfRange(fields, 0, numSorts))); } }
/** * Set up the filters for collections - this is for searching within collections. * * @param collection - to search within * @return - created filter * @throws ParseException */ private List<Filter> getCollectionFilters(InstitutionalCollection collection) throws ParseException { List<Filter> filters = new LinkedList<Filter>(); //isolate the collection root Term t = new Term("collection_root_id", NumericUtils.longToPrefixCoded(collection.getTreeRoot().getId())); Query subQuery = new TermQuery( t ); filters.add(new QueryWrapperFilter(subQuery)); //isolate the range of children subQuery = NumericRangeQuery.newLongRange("collection_left_value", collection.getLeftValue(), collection.getRightValue(), true, true); filters.add(new QueryWrapperFilter(subQuery)); return filters; }
/** * Execute the sub query facets and return the search results * @throws ParseException * @throws IOException */ private List<Filter> getSubQueryFilters( List<FacetFilter> filters, IndexSearcher searcher) throws ParseException, IOException { List<Filter> luceneFilters = new LinkedList<Filter>(); for(FacetFilter filter : filters) { if(log.isDebugEnabled()) { log.debug("adding filter for field " + filter.getField() + " and query " + filter.getQuery()); } QueryParser subQueryParser = new QueryParser(Version.LUCENE_35, filter.getField(), analyzer); subQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR); String fixedQuery = SearchHelper.prepareFacetSearchString(filter.getQuery(), false); fixedQuery = "\"" + fixedQuery + "\""; Query subQuery = subQueryParser.parse(fixedQuery); if(log.isDebugEnabled()) { log.debug("sub query ing getSubQueryFilters is " + fixedQuery); } luceneFilters.add(new QueryWrapperFilter(subQuery)); } return luceneFilters; }
/** Used by DrillSideways */ DrillDownQuery(FacetsConfig config, Filter filter, DrillDownQuery other) { query = new BooleanQuery(true); // disable coord BooleanClause[] clauses = other.query.getClauses(); if (clauses.length == other.drillDownDims.size()) { throw new IllegalArgumentException("cannot apply filter unless baseQuery isn't null; pass ConstantScoreQuery instead"); } assert clauses.length == 1+other.drillDownDims.size(): clauses.length + " vs " + (1+other.drillDownDims.size()); drillDownDims.putAll(other.drillDownDims); query.add(new FilteredQuery(clauses[0].getQuery(), filter), Occur.MUST); for(int i=1;i<clauses.length;i++) { query.add(clauses[i].getQuery(), Occur.MUST); } this.config = config; }
/** Expert: add a custom drill-down Filter, e.g. when * drilling down after range faceting. */ public void add(String dim, Filter subFilter) { if (drillDownDims.containsKey(dim)) { throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down"); } // TODO: we should use FilteredQuery? // So scores of the drill-down query don't have an // effect: final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subFilter); drillDownQuery.setBoost(0.0f); query.add(drillDownQuery, Occur.MUST); drillDownDims.put(dim, drillDownDims.size()); }
/** * Search, sorting by {@link Sort}, and computing * drill down and sideways counts. */ public DrillSidewaysResult search(DrillDownQuery query, Filter filter, FieldDoc after, int topN, Sort sort, boolean doDocScores, boolean doMaxScore) throws IOException { if (filter != null) { query = new DrillDownQuery(config, filter, query); } if (sort != null) { int limit = searcher.getIndexReader().maxDoc(); if (limit == 0) { limit = 1; // the collector does not alow numHits = 0 } topN = Math.min(topN, limit); final TopFieldCollector hitCollector = TopFieldCollector.create(sort, topN, after, true, doDocScores, doMaxScore, true); DrillSidewaysResult r = search(query, hitCollector); return new DrillSidewaysResult(r.facets, hitCollector.topDocs()); } else { return search(after, query, topN); } }
@Override public Filter getFilter(Element n) throws ParserException { FilterBuilder builder = builders.get(n.getNodeName()); if (builder == null) { throw new ParserException("No FilterBuilder defined for node " + n.getNodeName()); } return builder.getFilter(n); }
@Override public Filter getFilter(Element e) throws ParserException { String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName"); String lowerTerm = DOMUtils.getAttributeOrFail(e, "lowerTerm"); String upperTerm = DOMUtils.getAttributeOrFail(e, "upperTerm"); boolean lowerInclusive = DOMUtils.getAttribute(e, "includeLower", true); boolean upperInclusive = DOMUtils.getAttribute(e, "includeUpper", true); int precisionStep = DOMUtils.getAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT); String type = DOMUtils.getAttribute(e, "type", "int"); try { Filter filter; if (type.equalsIgnoreCase("int")) { filter = NumericRangeFilter.newIntRange(field, precisionStep, Integer .valueOf(lowerTerm), Integer.valueOf(upperTerm), lowerInclusive, upperInclusive); } else if (type.equalsIgnoreCase("long")) { filter = NumericRangeFilter.newLongRange(field, precisionStep, Long .valueOf(lowerTerm), Long.valueOf(upperTerm), lowerInclusive, upperInclusive); } else if (type.equalsIgnoreCase("double")) { filter = NumericRangeFilter.newDoubleRange(field, precisionStep, Double .valueOf(lowerTerm), Double.valueOf(upperTerm), lowerInclusive, upperInclusive); } else if (type.equalsIgnoreCase("float")) { filter = NumericRangeFilter.newFloatRange(field, precisionStep, Float .valueOf(lowerTerm), Float.valueOf(upperTerm), lowerInclusive, upperInclusive); } else { throw new ParserException("type attribute must be one of: [long, int, double, float]"); } return filter; } catch (NumberFormatException nfe) { if (strictMode) { throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe); } return NO_MATCH_FILTER; } }
@Override public Filter getFilter(Element e) throws ParserException { String fieldName = DOMUtils.getAttributeWithInheritance(e, "fieldName"); String lowerTerm = e.getAttribute("lowerTerm"); String upperTerm = e.getAttribute("upperTerm"); boolean includeLower = DOMUtils.getAttribute(e, "includeLower", true); boolean includeUpper = DOMUtils.getAttribute(e, "includeUpper", true); return TermRangeFilter.newStringRange(fieldName, lowerTerm, upperTerm, includeLower, includeUpper); }
@Override public Query getQuery(Element e) throws ParserException { Element filterElement = DOMUtils.getChildByTagOrFail(e, "Filter"); filterElement = DOMUtils.getFirstChildOrFail(filterElement); Filter f = filterFactory.getFilter(filterElement); Element queryElement = DOMUtils.getChildByTagOrFail(e, "Query"); queryElement = DOMUtils.getFirstChildOrFail(queryElement); Query q = queryFactory.getQuery(queryElement); FilteredQuery fq = new FilteredQuery(q, f); fq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f)); return fq; }
public ChildrenQuery(ParentChildIndexFieldData ifd, String parentType, String childType, Filter parentFilter, Query childQuery, ScoreType scoreType, int minChildren, int maxChildren, int shortCircuitParentDocSet, BitSetProducer nonNestedDocsFilter) { this.ifd = ifd; this.parentType = parentType; this.childType = childType; this.parentFilter = parentFilter; this.childQuery = childQuery; this.scoreType = scoreType; this.shortCircuitParentDocSet = shortCircuitParentDocSet; this.nonNestedDocsFilter = nonNestedDocsFilter; assert maxChildren == 0 || minChildren <= maxChildren; this.minChildren = minChildren > 1 ? minChildren : 0; this.maxChildren = maxChildren; }
protected ParentWeight(Query query, Weight childWeight, Filter parentFilter, long remaining, ParentCollector collector, int minChildren, int maxChildren) { super(query); this.childWeight = childWeight; this.parentFilter = parentFilter; this.remaining = remaining; this.collector = collector; this.minChildren = minChildren; this.maxChildren = maxChildren; }
private ChildWeight(Query query, Weight parentWeight, Filter childrenFilter, ParentOrdAndScoreCollector collector, IndexParentChildFieldData globalIfd) { super(query); this.parentWeight = parentWeight; this.childrenFilter = childrenFilter; this.parentIdxs = collector.parentIdxs; this.scores = collector.scores; this.globalIfd = globalIfd; }
public ChildrenConstantScoreQuery(IndexParentChildFieldData parentChildIndexFieldData, Query childQuery, String parentType, String childType, Filter parentFilter, int shortCircuitParentDocSet, BitSetProducer nonNestedDocsFilter) { this.parentChildIndexFieldData = parentChildIndexFieldData; this.parentFilter = parentFilter; this.parentType = parentType; this.childType = childType; this.childQuery = childQuery; this.shortCircuitParentDocSet = shortCircuitParentDocSet; this.nonNestedDocsFilter = nonNestedDocsFilter; }
@Override public Weight doCreateWeight(IndexSearcher searcher, boolean needsScores) throws IOException { SearchContext sc = SearchContext.current(); IndexParentChildFieldData globalIfd = parentChildIndexFieldData.loadGlobal((DirectoryReader)searcher.getIndexReader()); final long valueCount; List<LeafReaderContext> leaves = searcher.getIndexReader().leaves(); if (globalIfd == null || leaves.isEmpty()) { return new BooleanQuery.Builder().build().createWeight(searcher, needsScores); } else { AtomicParentChildFieldData afd = globalIfd.load(leaves.get(0)); SortedDocValues globalValues = afd.getOrdinalsValues(parentType); valueCount = globalValues.getValueCount(); } if (valueCount == 0) { return new BooleanQuery.Builder().build().createWeight(searcher, needsScores); } ParentOrdCollector collector = new ParentOrdCollector(globalIfd, valueCount, parentType); searcher.search(childQuery, collector); final long remaining = collector.foundParents(); if (remaining == 0) { return new BooleanQuery.Builder().build().createWeight(searcher, needsScores); } Filter shortCircuitFilter = null; if (remaining <= shortCircuitParentDocSet) { shortCircuitFilter = ParentIdsFilter.createShortCircuitFilter( nonNestedDocsFilter, sc, parentType, collector.values, collector.parentOrds, remaining ); } return new ParentWeight(this, parentFilter, globalIfd, shortCircuitFilter, collector, remaining); }
public ParentWeight(Query query, Filter parentFilter, IndexParentChildFieldData globalIfd, Filter shortCircuitFilter, ParentOrdCollector collector, long remaining) { super(query); this.parentFilter = parentFilter; this.globalIfd = globalIfd; this.shortCircuitFilter = shortCircuitFilter; this.collector = collector; this.remaining = remaining; }
public LindenResultParser(LindenConfig config, LindenSearchRequest request, IndexSearcher indexSearcher, LindenSnippetGenerator snippetGenerator, Query query, Filter filter, Sort sort) { this.config = config; this.request = request; this.indexSearcher = indexSearcher; this.snippetGenerator = snippetGenerator; this.query = query; this.filter = filter; this.sort = sort; this.sortScoreFieldPos = getSortScoreFieldPos(sort); this.leaves = indexSearcher.getIndexReader().leaves(); }
@Override protected Filter construct(LindenFilter lindenFilter, LindenConfig config) throws Exception { LindenNotNullFieldFilter lindenNotNullFieldFilter = lindenFilter.getNotNullFieldFilter(); NotNullFieldFilter notNullFieldFilter = new NotNullFieldFilter(lindenNotNullFieldFilter.getField()); if (!lindenNotNullFieldFilter.isReverse()) { return notNullFieldFilter; } BooleanFilter booleanFilter = new BooleanFilter(); booleanFilter.add(notNullFieldFilter, BooleanClause.Occur.MUST_NOT); return booleanFilter; }
@Override protected Filter construct(LindenFilter lindenFilter, LindenConfig config) throws IOException { LindenSpatialFilter spatialFilter = lindenFilter.getSpatialFilter(); SpatialArgs spatialArgs = new SpatialArgs( SpatialOperation.Intersects, spatialContext.makeCircle( spatialFilter.getSpatialParam().coordinate.getLongitude(), spatialFilter.getSpatialParam().coordinate.getLatitude(), DistanceUtils .dist2Degrees(spatialFilter.getSpatialParam().getDistanceRange(), DistanceUtils.EARTH_MEAN_RADIUS_KM))); return spatialStrategy.makeFilter(spatialArgs); }
@Test public void testFlexibleFilter() throws Exception { String bql = "select name from linden where flexible_query is 'qq音乐' full_match in (name^1.5) \n" + "USING MODEL test \n" + "begin\n" + " return score();\n" + "end\n" + "order by score,id limit $offset, $length source"; LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest(); Filter filter = FilterConstructor.constructFilter(request.getFilter(), lindenConfig); Assert.assertEquals("QueryWrapperFilter(FlexibleQuery([name^1.5]:[qq,音,乐]fullMatch))", filter.toString()); bql = "select name from linden \n" + "by flexible_query is \"lucene\" in (title^1.2) \n" + "USING MODEL test1 \n" + "begin \n" + " return score() + 1;\n" + "end\n" + "where flexible_query is 'ddd' full_match in (field1)\n" + "USING MODEL filter_func begin return 1f; end \n" + "order by score,id limit $offset, $length source"; request = bqlCompiler.compile(bql).getSearchRequest(); LindenResult result = lindenCore.search(request); Assert.assertEquals(1, result.getHitsSize()); Assert.assertEquals(1f, result.getHits().get(0).getScore(), DELTA5); Assert.assertEquals("4", result.getHits().get(0).getId()); }