private static final void getTerms(Query query, HashSet<WeightedTerm> terms, boolean prohibited, String fieldName) { try { if (query instanceof BooleanQuery) getTermsFromBooleanQuery((BooleanQuery) query, terms, prohibited, fieldName); else if (query instanceof FilteredQuery) getTermsFromFilteredQuery((FilteredQuery) query, terms, prohibited, fieldName); else { HashSet<Term> nonWeightedTerms = new HashSet<>(); query.extractTerms(nonWeightedTerms); for (Iterator<Term> iter = nonWeightedTerms.iterator(); iter.hasNext(); ) { Term term = iter.next(); if ((fieldName == null) || (term.field().equals(fieldName))) { terms.add(new WeightedTerm(query.getBoost(), term.text())); } } } } catch (UnsupportedOperationException ignore) { //this is non-fatal for our purposes } }
public static final FilterStrategy randomFilterStrategy(final Random random) { switch(random.nextInt(6)) { case 5: case 4: return new FilteredQuery.RandomAccessFilterStrategy() { @Override protected boolean useRandomAccess(Bits bits, int firstFilterDoc) { return LuceneTestCase.random().nextBoolean(); } }; case 3: return FilteredQuery.RANDOM_ACCESS_FILTER_STRATEGY; case 2: return FilteredQuery.LEAP_FROG_FILTER_FIRST_STRATEGY; case 1: return FilteredQuery.LEAP_FROG_QUERY_FIRST_STRATEGY; case 0: return FilteredQuery.QUERY_FIRST_FILTER_STRATEGY; default: return FilteredQuery.RANDOM_ACCESS_FILTER_STRATEGY; } }
private Query getQueryFromSpatialArgs(QParser parser, SchemaField field, SpatialArgs spatialArgs) { T strategy = getStrategy(field.getName()); SolrParams localParams = parser.getLocalParams(); String scoreParam = (localParams == null ? null : localParams.get(SCORE_PARAM)); //We get the valueSource for the score then the filter and combine them. ValueSource valueSource = getValueSourceFromSpatialArgs(parser, field, spatialArgs, scoreParam, strategy); if (valueSource == null) { //FYI Solr FieldType doesn't have a getFilter(). We'll always grab // getQuery() but it's possible a strategy has a more efficient getFilter // that could be wrapped -- no way to know. //See SOLR-2883 needScore return strategy.makeQuery(spatialArgs); //ConstantScoreQuery } FunctionQuery functionQuery = new FunctionQuery(valueSource); if (localParams != null && !localParams.getBool(FILTER_PARAM, true)) return functionQuery; Filter filter = strategy.makeFilter(spatialArgs); return new FilteredQuery(functionQuery, filter); }
private static final void getTerms(Query query, HashSet<WeightedTerm> terms, boolean prohibited, String fieldName) { try { if (query instanceof BooleanQuery) getTermsFromBooleanQuery((BooleanQuery) query, terms, prohibited, fieldName); else if (query instanceof FilteredQuery) getTermsFromFilteredQuery((FilteredQuery) query, terms, prohibited, fieldName); else { HashSet<Term> nonWeightedTerms = new HashSet<Term>(); query.extractTerms(nonWeightedTerms); for (Iterator<Term> iter = nonWeightedTerms.iterator(); iter.hasNext(); ) { Term term = iter.next(); if ((fieldName == null) || (term.field().equals(fieldName))) { terms.add(new WeightedTerm(query.getBoost(), term.text())); } } } } catch (UnsupportedOperationException ignore) { //this is non-fatal for our purposes } }
/** * This method is very important!!! It handles rewriting the real query (which * can be a {@link SuperQuery} to have document (record) level filtering or * access control. */ @Override protected Query wrapFilter(Query query, Filter filter) { if (filter == null) { return query; } else if (query instanceof SuperQuery) { SuperQuery superQuery = (SuperQuery) query; Query innerQuery = superQuery.getQuery(); Term primeDocTerm = superQuery.getPrimeDocTerm(); ScoreType scoreType = superQuery.getScoreType(); return new SuperQuery(wrapFilter(innerQuery, filter), scoreType, primeDocTerm); } else if (query instanceof BooleanQuery) { BooleanQuery booleanQuery = (BooleanQuery) query; List<BooleanClause> clauses = booleanQuery.clauses(); for (BooleanClause booleanClause : clauses) { booleanClause.setQuery(wrapFilter(booleanClause.getQuery(), filter)); } return booleanQuery; } else { return new FilteredQuery(query, filter); } }
public static FilteredQuery.FilterStrategy randomFilterStrategy(final Random random) { switch (random.nextInt(6)) { case 5: case 4: return new FilteredQuery.RandomAccessFilterStrategy() { @Override protected boolean useRandomAccess(Bits bits, int firstFilterDoc) { return random.nextBoolean(); } }; case 3: return FilteredQuery.RANDOM_ACCESS_FILTER_STRATEGY; case 2: return FilteredQuery.LEAP_FROG_FILTER_FIRST_STRATEGY; case 1: return FilteredQuery.LEAP_FROG_QUERY_FIRST_STRATEGY; case 0: return FilteredQuery.QUERY_FIRST_FILTER_STRATEGY; default: return FilteredQuery.RANDOM_ACCESS_FILTER_STRATEGY; } }
public void testFilteredQuery() throws Exception { String[] isbns = new String[] { "9780880105118" }; SpecialsAccessor accessor = new TestSpecialsAccessor(isbns); Filter filter = new SpecialsFilter(accessor); WildcardQuery educationBooks = new WildcardQuery(new Term("category", "*education*")); FilteredQuery edBooksOnSpecial = new FilteredQuery(educationBooks, filter); TermQuery logoBooks = new TermQuery(new Term("subject", "logo")); BooleanQuery logoOrEdBooks = new BooleanQuery(); logoOrEdBooks.add(logoBooks, BooleanClause.Occur.SHOULD); logoOrEdBooks.add(edBooksOnSpecial, BooleanClause.Occur.SHOULD); TopDocs hits = searcher.search(logoOrEdBooks, 10); LOGGER.info(logoOrEdBooks.toString()); assertEquals("Papert and Steiner", 2, hits.totalHits); }
@Override public Query createAllDocsQuery(final @NonNull String name) { if (name.length() == 0) { return new MatchAllDocsQuery(); } else { return new FilteredQuery(new MatchAllDocsQuery(), new HasFieldFilter(name)); } }
@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 void testFlattenFilteredQuery() throws Exception { initBoost(); Query query = new FilteredQuery(pqF( "A" ), new Filter() { @Override public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) throws IOException { return null; } }); query.setBoost(boost); FieldQuery fq = new FieldQuery( query, true, true ); Set<Query> flatQueries = new HashSet<>(); fq.flatten( query, reader, flatQueries ); assertCollectionQueries( flatQueries, tq( boost, "A" ) ); }
public void testFlattenFilteredQuery() throws Exception { Query query = new FilteredQuery(pqF( "A" ), new Filter() { @Override public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) throws IOException { return null; } }); FieldQuery fq = new FieldQuery( query, true, true ); Set<Query> flatQueries = new HashSet<Query>(); fq.flatten( query, reader, flatQueries ); assertCollectionQueries( flatQueries, tq( "A" ) ); }
private Query getQueryFromSpatialArgs(QParser parser, SchemaField field, SpatialArgs spatialArgs) { T strategy = getStrategy(field.getName()); SolrParams localParams = parser.getLocalParams(); String score = (localParams == null ? null : localParams.get(SCORE_PARAM)); if (score == null || "none".equals(score) || "".equals(score)) { //FYI Solr FieldType doesn't have a getFilter(). We'll always grab // getQuery() but it's possible a strategy has a more efficient getFilter // that could be wrapped -- no way to know. //See SOLR-2883 needScore return strategy.makeQuery(spatialArgs); //ConstantScoreQuery } //We get the valueSource for the score then the filter and combine them. ValueSource valueSource; if ("distance".equals(score)) valueSource = strategy.makeDistanceValueSource(spatialArgs.getShape().getCenter()); else if ("recipDistance".equals(score)) valueSource = strategy.makeRecipDistanceValueSource(spatialArgs.getShape()); else throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'score' local-param must be one of 'none', 'distance', or 'recipDistance'"); FunctionQuery functionQuery = new FunctionQuery(valueSource); if (localParams != null && !localParams.getBool(FILTER_PARAM, true)) return functionQuery; Filter filter = strategy.makeFilter(spatialArgs); return new FilteredQuery(functionQuery, filter); }
public static Query parseQuery(String query, boolean superQueryOn, FieldManager fieldManager, Filter postFilter, Filter preFilter, ScoreType scoreType, TableContext tableContext) throws ParseException { Query result = new SuperParser(LUCENE_VERSION, fieldManager, superQueryOn, preFilter, scoreType, tableContext.getDefaultPrimeDocTerm()).parse(query); if (postFilter == null) { return result; } return new FilteredQuery(result, postFilter); }
@Test public void testQueryFilterWrap1() throws IOException { IndexReader r = getIndexReader(); AccessControlFactory accessControlFactory = new FilterAccessControlFactory(); Collection<String> readAuthorizations = new ArrayList<String>(); Collection<String> discoverAuthorizations = new ArrayList<String>(); Set<String> discoverableFields = new HashSet<String>(Arrays.asList("rowid")); BlurSecureIndexSearcher blurSecureIndexSearcher = new BlurSecureIndexSearcher(r, null, accessControlFactory, readAuthorizations, discoverAuthorizations, discoverableFields, null); Query wrapFilter; Query query = new TermQuery(new Term("a", "b")); Filter filter = new Filter() { @Override public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) throws IOException { throw new RuntimeException("Not implemented."); } }; { Term primeDocTerm = new Term(BlurConstants.PRIME_DOC, BlurConstants.PRIME_DOC_VALUE); ScoreType scoreType = ScoreType.SUPER; SuperQuery superQuery = new SuperQuery(query, scoreType, primeDocTerm); wrapFilter = blurSecureIndexSearcher.wrapFilter(superQuery, filter); System.out.println(wrapFilter); } { assertTrue(wrapFilter instanceof SuperQuery); SuperQuery sq = (SuperQuery) wrapFilter; Query inner = sq.getQuery(); assertTrue(inner instanceof FilteredQuery); FilteredQuery filteredQuery = (FilteredQuery) inner; Query innerFilteredQuery = filteredQuery.getQuery(); assertEquals(innerFilteredQuery, query); assertTrue(filteredQuery.getFilter() == filter); } }
private Query getQueryFromSpatialArgs(QParser parser, SchemaField field, SpatialArgs spatialArgs) { T strategy = getStrategy(field.getName()); SolrParams localParams = parser.getLocalParams(); String score = (localParams == null ? null : localParams.get(SCORE_PARAM)); if (score == null || "none".equals(score) || "".equals(score)) { //FYI Solr FieldType doesn't have a getFilter(). We'll always grab // getQuery() but it's possible a strategy has a more efficient getFilter // that could be wrapped -- no way to know. //See SOLR-2883 needScore return strategy.makeQuery(spatialArgs); //ConstantScoreQuery } //We get the valueSource for the score then the filter and combine them. ValueSource valueSource; if ("distance".equals(score)) { double multiplier = 1.0;//TODO support units=kilometers valueSource = strategy.makeDistanceValueSource(spatialArgs.getShape().getCenter(), multiplier); } else if ("recipDistance".equals(score)) { valueSource = strategy.makeRecipDistanceValueSource(spatialArgs.getShape()); } else { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'score' local-param must be one of 'none', 'distance', or 'recipDistance'"); } FunctionQuery functionQuery = new FunctionQuery(valueSource); if (localParams != null && !localParams.getBool(FILTER_PARAM, true)) return functionQuery; Filter filter = strategy.makeFilter(spatialArgs); return new FilteredQuery(functionQuery, filter); }
@Override protected Query wrapFilter(Query query, Filter filter) { if (random.nextBoolean()) { return super.wrapFilter(query, filter); } return (filter == null) ? query : new FilteredQuery(query, filter, randomFilterStrategy(random)); }
@Override public Query createRegExpQuery(final @NonNull String name, final @NonNull String value, final boolean caseSensitive) { return new FilteredQuery(new MatchAllDocsQuery(), new RegexpFilter(name, value, caseSensitive)); }
@Override protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws Exception { Query query = QueryConstructor.constructQuery(lindenQuery.getFilteredQuery().getLindenQuery(), config); Filter filter = FilterConstructor.constructFilter(lindenQuery.getFilteredQuery().getLindenFilter(), config); return new FilteredQuery(query, filter); }
private static void getTermsFromFilteredQuery(FilteredQuery query, HashSet<WeightedTerm> terms, boolean prohibited, String fieldName) { getTerms(query.getQuery(),terms,prohibited,fieldName); }
public void testWildcardInFiltered() throws Exception { Directory dir = newDirectory(); // use simpleanalyzer for more natural tokenization (else "test." is a token) final Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true); IndexWriterConfig iwc = newIndexWriterConfig(analyzer); iwc.setMergePolicy(newLogMergePolicy()); RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc); FieldType offsetsType = new FieldType(TextField.TYPE_STORED); offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS); Field body = new Field("body", "", offsetsType); Document doc = new Document(); doc.add(body); body.setStringValue("This is a test."); iw.addDocument(doc); body.setStringValue("Test a one sentence document."); iw.addDocument(doc); IndexReader ir = iw.getReader(); iw.close(); IndexSearcher searcher = newSearcher(ir); PostingsHighlighter highlighter = new PostingsHighlighter() { @Override protected Analyzer getIndexAnalyzer(String field) { return analyzer; } }; FilteredQuery query = new FilteredQuery( new WildcardQuery(new Term("body", "te*")), new TermFilter(new Term("body", "test"))); TopDocs topDocs = searcher.search(query, null, 10, Sort.INDEXORDER); assertEquals(2, topDocs.totalHits); String snippets[] = highlighter.highlight("body", query, searcher, topDocs); assertEquals(2, snippets.length); assertEquals("This is a <b>test</b>.", snippets[0]); assertEquals("<b>Test</b> a one sentence document.", snippets[1]); ir.close(); dir.close(); }
@Override protected Query makeQuery(SpatialTestQuery q) { return new FilteredQuery(new MatchAllDocsQuery(), strategy.makeFilter(q.args), FilteredQuery.QUERY_FIRST_FILTER_STRATEGY); }
private Query wrapFilter(Query query) { if (_queryFilter == null) { return query; } return new FilteredQuery(query, _queryFilter, FilteredQuery.LEAP_FROG_QUERY_FIRST_STRATEGY); }