static Query binaryNameQuery (final String resourceName) { final BooleanQuery query = new BooleanQuery (); int index = resourceName.lastIndexOf(BinaryName.PKG_SEPARATOR); // NOI18N String pkgName, sName; if (index < 0) { pkgName = ""; // NOI18N sName = resourceName; } else { pkgName = resourceName.substring(0,index); sName = resourceName.substring(index+1); } sName = sName + WILDCARD_QUERY_WILDCARD; //Type of type element (Enum, Class, Interface, Annotation) query.add (new TermQuery (new Term (FIELD_PACKAGE_NAME, pkgName)),BooleanClause.Occur.MUST); query.add (new WildcardQuery (new Term (FIELD_BINARY_NAME, sName)),BooleanClause.Occur.MUST); return query; }
private static BooleanQuery createFQNQuery(final String resourceName) { String pkgName; String sName; int index = resourceName.lastIndexOf(BinaryName.PKG_SEPARATOR); if (index < 0) { pkgName = ""; //NOI18N sName = resourceName; } else { pkgName = resourceName.substring(0, index); sName = resourceName.substring(index+1); } final BooleanQuery snQuery = new BooleanQuery(); snQuery.add (new WildcardQuery (new Term (DocumentUtil.FIELD_BINARY_NAME, sName + DocumentUtil.WILDCARD_QUERY_WILDCARD)),Occur.SHOULD); snQuery.add (new PrefixQuery (new Term (DocumentUtil.FIELD_BINARY_NAME, sName + '$')),Occur.SHOULD); //NOI18N if (pkgName.length() == 0) { return snQuery; } final BooleanQuery fqnQuery = new BooleanQuery(); fqnQuery.add(new TermQuery(new Term(DocumentUtil.FIELD_PACKAGE_NAME,pkgName)), Occur.MUST); fqnQuery.add(snQuery, Occur.MUST); return fqnQuery; }
/** * This method is eventually called from the middle of BinaryAnalyser work, after it flushes some data from memory. The method * must check & store information that the data is NOT visible to IndexReaders yet */ private void dataFlushed(Index index) throws IOException, InterruptedException { Collection<String> names = new LinkedList<String>(); // check using collected usages index.query( names, DocumentUtil.binaryNameConvertor(), DocumentUtil.declaredTypesFieldSelector(false, false), null, QueryUtil.createUsagesQuery("java.util.List", EnumSet.of(UsageType.TYPE_REFERENCE), Occur.SHOULD)); names.retainAll( Arrays.asList( "usages.ClassAnnotations", "usages.ClassArrayAnnotations", "usages.MethodAnnotations", "usages.MethodArrayAnnotations", "usages.FieldAnnotations", "usages.FieldArrayAnnotations") ); assertTrue(names.isEmpty()); flushCount++; }
/** * Adds a filter for the given search text to the given {@link BooleanQuery}. * If the search text is blank, than the query will not modified. * For each part of the search text {@link #buildSearchTermQuery(IIndexTypeConf, String, float)} * will be called. * * @param query the {@link BooleanQuery} to add the sub queries. * @param searchText the search text. May be blank (null or contain only white spaces). * @param indexType the type in which should be searched. * @param baseBoost highest possible boost of the query. The more the match is exact * than a bigger boost will be used. */ public static void addSearchText(final BooleanQuery query, final String searchText, final IIndexTypeConf indexType, final float baseBoost) { if(StringUtils.isBlank(searchText)) { return; } query.setMinimumNumberShouldMatch(1); // at least one "should" should match query.add(buildSearchTermQuery(indexType, searchText, baseBoost), Occur.SHOULD); final BooleanQuery partsQuery = new BooleanQuery(); query.add(partsQuery, Occur.SHOULD); final String[] searchTexts = searchText.toLowerCase().split("\\s"); for (final String search : searchTexts) { partsQuery.add(buildSearchTermQuery(indexType, search, baseBoost), Occur.MUST); // each part has to match } }
/** * Adds for every field in {@link IIndexTypeConf} a {@link TermQuery}, {@link PrefixQuery} and * a {@link FuzzyQuery} with a suitable boost relative to the given base boost. * * @param indexType the type in which should be searched. * @param search the search text. * @param subQuery the {@link BooleanQuery} to add the sub queries. * @param baseBoost highest possible boost of the query. The more the match is exact * than a bigger boost will be used. */ private static void addSearchTermQueries(final IIndexTypeConf indexType, final String search, final BooleanQuery subQuery, final float baseBoost) { for(final IIndexFieldConf<?> field : indexType.getFields()) { final Term term = new Term(field.getName(), search); final TermQuery exactQuery = new TermQuery(term); exactQuery.setBoost(baseBoost); subQuery.add(exactQuery, Occur.SHOULD); final PrefixQuery pfQuery = new PrefixQuery(term); pfQuery.setBoost(0.7f*baseBoost); subQuery.add(pfQuery, Occur.SHOULD); final FuzzyQuery fuzzyQuery = new FuzzyQuery(term); fuzzyQuery.setBoost(0.5f*baseBoost); subQuery.add(fuzzyQuery, Occur.SHOULD); } }
/** * Returns all {@link News} of the given news group except the {@link News} with the given id. * @param newsGroupId the news group id * @param exceptId the news which should not be returned * @return a {@link List} with all {@link News} of the requested news group except the {@link News} with the exceptId. */ private List<News> getNewsOfNewsGroup(final long newsGroupId, final long exceptId) { final BooleanQuery query = new BooleanQuery(); QueryUtil.addTypeConf(query, NewsIndexType.getInstance()); final NumericRangeQuery<Long> groupQuery = NumericRangeQuery.newLongRange( NewsIndexType.FIELD_NEWSGROUPID, newsGroupId, newsGroupId, true, true); query.add(groupQuery, Occur.MUST); // exclude news query.add(new TermQuery(new Term(IIndexElement.FIELD_ID, String.valueOf(exceptId))), Occur.MUST_NOT); final SearchOptions options = new SearchOptions(); options.setSort(new Sort(ESortField.PUBLISH_DATE.getSortField(ESortOrder.DESC))); final DocumentsSearchResult result = IndexSearch.getInstance().search(query, options); return NewsIndexType.docsToNews(result.getResults()); }
/** * Returns the amount of {@link News} which are assigned to news groups (news group id > 0) * for the given {@link Query}. */ private int getAmountOfNewsInNewsGroups(final Query filterQuery) { final BooleanQuery query = new BooleanQuery(); query.add(filterQuery, Occur.MUST); // get only news that are in real groups (newsGroupId > 0) final NumericRangeQuery<Long> newsGroupFilterQuery = NumericRangeQuery.newLongRange( NewsIndexType.FIELD_NEWSGROUPID, 0l, null, false, true); query.add(newsGroupFilterQuery, Occur.MUST); final SearchOptions options = new SearchOptions(); options.setMaxResults(0); // we only want the totalHits, not the results. final TopDocs topDocs = IndexSearch.getInstance().getTopDocs(query, options); return topDocs.totalHits; }
public void testExtractTerms() throws Exception { addQueryMapping(); BooleanQuery.Builder bq = new BooleanQuery.Builder(); TermQuery termQuery1 = new TermQuery(new Term("field", "term1")); bq.add(termQuery1, BooleanClause.Occur.SHOULD); TermQuery termQuery2 = new TermQuery(new Term("field", "term2")); bq.add(termQuery2, BooleanClause.Occur.SHOULD); DocumentMapper documentMapper = mapperService.documentMapper(typeName); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, mapperService.documentMapperParser(), documentMapper, null, null); fieldMapper.processQuery(bq.build(), parseContext); ParseContext.Document document = parseContext.doc(); PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType(); assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE)); List<IndexableField> fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.queryTermsField.name()))); Collections.sort(fields, (field1, field2) -> field1.binaryValue().compareTo(field2.binaryValue())); assertThat(fields.size(), equalTo(2)); assertThat(fields.get(0).binaryValue().utf8ToString(), equalTo("field\u0000term1")); assertThat(fields.get(1).binaryValue().utf8ToString(), equalTo("field\u0000term2")); }
private long getBackgroundFrequency(String value) throws IOException { Query query = fieldType.termQuery(value, context.getQueryShardContext()); if (query instanceof TermQuery) { // for types that use the inverted index, we prefer using a caching terms // enum that will do a better job at reusing index inputs Term term = ((TermQuery) query).getTerm(); FilterableTermsEnum termsEnum = getTermsEnum(term.field()); if (termsEnum.seekExact(term.bytes())) { return termsEnum.docFreq(); } else { return 0; } } // otherwise do it the naive way if (filter != null) { query = new BooleanQuery.Builder() .add(query, Occur.FILTER) .add(filter, Occur.FILTER) .build(); } return context.searcher().count(query); }
@Override protected Query doToQuery(QueryShardContext context) throws IOException { BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder(); booleanQueryBuilder.setDisableCoord(disableCoord); addBooleanClauses(context, booleanQueryBuilder, mustClauses, BooleanClause.Occur.MUST); addBooleanClauses(context, booleanQueryBuilder, mustNotClauses, BooleanClause.Occur.MUST_NOT); addBooleanClauses(context, booleanQueryBuilder, shouldClauses, BooleanClause.Occur.SHOULD); addBooleanClauses(context, booleanQueryBuilder, filterClauses, BooleanClause.Occur.FILTER); BooleanQuery booleanQuery = booleanQueryBuilder.build(); if (booleanQuery.clauses().isEmpty()) { return new MatchAllDocsQuery(); } final String minimumShouldMatch; if (context.isFilter() && this.minimumShouldMatch == null && shouldClauses.size() > 0) { minimumShouldMatch = "1"; } else { minimumShouldMatch = this.minimumShouldMatch; } Query query = Queries.applyMinimumShouldMatch(booleanQuery, minimumShouldMatch); return adjustPureNegative ? fixNegativeQueryIfNeeded(query) : query; }
private static void addBooleanClauses(QueryShardContext context, BooleanQuery.Builder booleanQueryBuilder, List<QueryBuilder> clauses, Occur occurs) throws IOException { for (QueryBuilder query : clauses) { Query luceneQuery = null; switch (occurs) { case MUST: case SHOULD: luceneQuery = query.toQuery(context); break; case FILTER: case MUST_NOT: luceneQuery = query.toFilter(context); break; } booleanQueryBuilder.add(new BooleanClause(luceneQuery, occurs)); } }
public static Query applyMinimumShouldMatch(BooleanQuery query, @Nullable String minimumShouldMatch) { if (minimumShouldMatch == null) { return query; } int optionalClauses = 0; for (BooleanClause c : query.clauses()) { if (c.getOccur() == BooleanClause.Occur.SHOULD) { optionalClauses++; } } int msm = calculateMinShouldMatch(optionalClauses, minimumShouldMatch); if (0 < msm) { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.setDisableCoord(query.isCoordDisabled()); for (BooleanClause clause : query) { builder.add(clause); } builder.setMinimumNumberShouldMatch(msm); return builder.build(); } else { return query; } }
public void testCrossFieldMultiMatchQuery() throws IOException { QueryShardContext queryShardContext = indexService.newQueryShardContext( randomInt(20), null, () -> { throw new UnsupportedOperationException(); }); queryShardContext.setAllowUnmappedFields(true); Query parsedQuery = multiMatchQuery("banon").field("name.first", 2).field("name.last", 3).field("foobar").type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext); try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { Query rewrittenQuery = searcher.searcher().rewrite(parsedQuery); BooleanQuery.Builder expected = new BooleanQuery.Builder(); expected.add(new TermQuery(new Term("foobar", "banon")), BooleanClause.Occur.SHOULD); Query tq1 = new BoostQuery(new TermQuery(new Term("name.first", "banon")), 2); Query tq2 = new BoostQuery(new TermQuery(new Term("name.last", "banon")), 3); expected.add(new DisjunctionMaxQuery(Arrays.<Query>asList(tq1, tq2), 0f), BooleanClause.Occur.SHOULD); assertEquals(expected.build(), rewrittenQuery); } }
public void testBlendNoTermQuery() { FakeFieldType ft1 = new FakeFieldType(); ft1.setName("foo"); FakeFieldType ft2 = new FakeFieldType() { @Override public Query termQuery(Object value, QueryShardContext context) { return new MatchAllDocsQuery(); } }; ft2.setName("bar"); Term[] terms = new Term[] { new Term("foo", "baz") }; float[] boosts = new float[] {2}; Query expectedClause1 = BlendedTermQuery.booleanBlendedQuery(terms, boosts, false); Query expectedClause2 = new BoostQuery(new MatchAllDocsQuery(), 3); Query expected = new BooleanQuery.Builder().setDisableCoord(true) .add(expectedClause1, Occur.SHOULD) .add(expectedClause2, Occur.SHOULD) .build(); Query actual = MultiMatchQuery.blendTerm( indexService.newQueryShardContext(randomInt(20), null, () -> { throw new UnsupportedOperationException(); }), new BytesRef("baz"), null, 1f, new FieldAndFieldType(ft1, 2), new FieldAndFieldType(ft2, 3)); assertEquals(expected, actual); }
private static BooleanQuery constructWildcardQuery(String searchPhrase, List<String> fieldNames, String locale, String defaultLocale, boolean isDefaultLocaleHandling) { String[] splitStr = searchPhrase.split("\\s+"); BooleanQuery booleanQuery = new BooleanQuery(); for (String token : splitStr) { booleanQuery.add( prepareWildcardQueryForSingleToken(token, fieldNames, locale, defaultLocale, isDefaultLocaleHandling), Occur.MUST); } return booleanQuery; }
private static BooleanQuery prepareWildcardQueryForSingleToken(String token, List<String> fieldNames, String locale, String defaultLocale, boolean isDefaultLocaleHandling) { BooleanQuery queryPart = new BooleanQuery(); for (String fieldName : fieldNames) { if (isDefaultLocaleHandling) { if (locale.equals(defaultLocale)) { throw new IllegalArgumentException( "For default locale handling, locale and default locale must be different"); } BooleanQuery localeHandlingQuery = constructDefaultLocaleHandlingQuery( fieldName, locale, defaultLocale, token); queryPart.add(localeHandlingQuery, Occur.SHOULD); } else { WildcardQuery wildcardQuery = new WildcardQuery(new Term( fieldName + locale, "*" + token.toLowerCase() + "*")); queryPart.add(wildcardQuery, Occur.SHOULD); } } return queryPart; }
private static BooleanQuery constructDefaultLocaleHandlingQuery( String fieldName, String locale, String defaultLocale, String searchPhrase) { BooleanQuery bq1 = new BooleanQuery(); TermQuery tq1 = new TermQuery( new Term(fieldName + ProductClassBridge.DEFINED_LOCALES_SUFFIX, defaultLocale)); TermQuery tq2 = new TermQuery(new Term( fieldName + ProductClassBridge.DEFINED_LOCALES_SUFFIX, locale)); bq1.add(tq1, Occur.MUST); bq1.add(tq2, Occur.MUST_NOT); BooleanQuery bq2 = new BooleanQuery(); WildcardQuery wq1 = new WildcardQuery( new Term(fieldName + defaultLocale, "*" + searchPhrase.toLowerCase() + "*")); bq2.add(wq1, Occur.SHOULD); BooleanQuery finalQuery = new BooleanQuery(); finalQuery.add(bq1, Occur.MUST); finalQuery.add(bq2, Occur.MUST); return finalQuery; }
/** * Returns the Lucene query for the given locale and query text. * * @param searchString * the text query for the Lucene query parser * @param mId * the marketplace id * @param locale * the locale for the analyzer to use * @param isDefaultLocaleHandling * @return the Lucene query for the given locale and query text * @throws ParseException * in case the query cannot be parsed */ private org.apache.lucene.search.Query getLuceneQuery(String searchString, String mId, String locale, boolean isDefaultLocaleHandling) throws SyntaxError, QueryNodeException { // construct wildcard query for the actual search part org.apache.lucene.search.Query textQuery = LuceneQueryBuilder .getServiceQuery(searchString, locale, DEFAULT_LOCALE, isDefaultLocaleHandling); // build mId part TermQuery mIdQuery = new TermQuery(new Term(ProductClassBridge.MP_ID, QueryParser.escape(mId).toLowerCase())); // now construct final query BooleanQuery query = new BooleanQuery(); query.add(mIdQuery, Occur.MUST); query.add(textQuery, Occur.MUST); return query; }
private BooleanQuery constructWildcardQuery(String searchPhrase) { String[] splitStr = searchPhrase.split("\\s+"); BooleanQuery booleanQuery = new BooleanQuery(); final List<String> fieldNames = Arrays.asList( SubscriptionClassBridge.NAME_SUBSCRIPTION_ID, SubscriptionClassBridge.NAME_REFERENCE, SubscriptionClassBridge.NAME_PARAMETER_VALUE, SubscriptionClassBridge.NAME_UDA_VALUE); for (String token : splitStr) { booleanQuery.add( prepareWildcardQueryForSingleToken(token, fieldNames), Occur.MUST); } return booleanQuery; }
@Override public Collection<IndexedItem> search(IndexSearcher searcher) throws IOException { BooleanQuery overall = new BooleanQuery(); BooleanQuery collections = new BooleanQuery(); for( Institution inst : institutions ) { collections.add( new TermQuery(new Term(FreeTextQuery.FIELD_INSTITUTION, Long.toString(inst.getUniqueId()))), Occur.SHOULD); } overall.add(collections, Occur.MUST); overall.add(NumericRangeQuery.newLongRange(FreeTextQuery.FIELD_ID_RANGEABLE, firstId, lastId, true, true), Occur.MUST); searcher.search(overall, compareDates); return compareDates.getModifiedDocs(); }
private Query addUniqueIdClauseToQuery(Query query, SearchResults<T> itemResults, IndexReader reader) { List<T> results = itemResults.getResults(); BooleanQuery orQuery = new BooleanQuery(); for( T t : results ) { ItemIdKey itemIdKey = t.getItemIdKey(); String[] split = itemIdKey.toString().split("/"); String uniqueId = split[1] + "/" + split[2]; FreeTextBooleanQuery bquery = new FreeTextBooleanQuery(false, true); bquery.add(new FreeTextFieldQuery(FreeTextQuery.FIELD_UNIQUE, uniqueId)); BooleanClause convertBoolean = convertToBooleanClause(bquery, reader); convertBoolean.setOccur(Occur.SHOULD); orQuery.add(convertBoolean); } BooleanQuery newQuery = new BooleanQuery(); newQuery.add(query, Occur.MUST); newQuery.add(orQuery, Occur.MUST); return newQuery; }
/** * @dytech.jira see Jira Review TLE-784 : * http://apps.dytech.com.au/jira/browse/TLE-784 */ protected Query addExtraQuery(@Nullable Query query, Search searchreq, IndexReader reader) { FreeTextQuery fullftQuery = searchreq.getFreeTextQuery(); if( fullftQuery == null ) { return query; } BooleanClause clause = convertToBooleanClause(fullftQuery, reader); if( !clause.isProhibited() && !clause.isRequired() ) { clause.setOccur(Occur.MUST); } BooleanQuery andThem = new BooleanQuery(); andThem.add(clause); if( query != null ) { andThem.add(query, Occur.MUST); } return andThem; }
@Override public void collect(int doc) throws IOException { final Query query = getQuery(doc); if (query == null) { // log??? return; } Query existsQuery = query; if (isNestedDoc) { existsQuery = new BooleanQuery.Builder() .add(existsQuery, Occur.MUST) .add(Queries.newNonNestedFilter(), Occur.FILTER) .build(); } // run the query try { if (Lucene.exists(searcher, existsQuery)) { topDocsLeafCollector.collect(doc); postMatch(doc); } } catch (IOException e) { logger.warn("[" + current.utf8ToString() + "] failed to execute query", e); } }
@Override public void collect(int doc) throws IOException { final Query query = getQuery(doc); if (query == null) { // log??? return; } Query existsQuery = query; if (isNestedDoc) { existsQuery = new BooleanQuery.Builder() .add(existsQuery, Occur.MUST) .add(Queries.newNonNestedFilter(), Occur.FILTER) .build(); } // run the query try { if (Lucene.exists(searcher, existsQuery)) { counter++; postMatch(doc); } } catch (IOException e) { logger.warn("[" + current.utf8ToString() + "] failed to execute query", e); } }
private void queryBasedPercolating(Engine.Searcher percolatorSearcher, PercolateContext context, QueryCollector percolateCollector) throws IOException { Query percolatorTypeFilter = context.indexService().mapperService().documentMapper(TYPE_NAME).typeFilter(); final Query filter; if (context.aliasFilter() != null) { BooleanQuery.Builder booleanFilter = new BooleanQuery.Builder(); booleanFilter.add(context.aliasFilter(), BooleanClause.Occur.MUST); booleanFilter.add(percolatorTypeFilter, BooleanClause.Occur.MUST); filter = booleanFilter.build(); } else { filter = percolatorTypeFilter; } Query query = Queries.filtered(context.percolateQuery(), filter); percolatorSearcher.searcher().search(query, percolateCollector); percolateCollector.aggregatorCollector.postCollection(); if (context.aggregations() != null) { aggregationPhase.execute(context); } }
private void innerDelete(DeleteByQuery delete) throws EngineException { try { Query query = delete.query(); if (delete.aliasFilter() != null) { query = new BooleanQuery.Builder() .add(query, Occur.MUST) .add(delete.aliasFilter(), Occur.FILTER) .build(); } if (delete.nested()) { query = new IncludeNestedDocsQuery(query, delete.parentFilter()); } indexWriter.deleteDocuments(query); translog.add(new Translog.DeleteByQuery(delete)); } catch (Throwable t) { maybeFailEngine("delete_by_query", t); throw new DeleteByQueryFailedEngineException(shardId, delete, t); } // TODO: This is heavy, since we refresh, but we must do this because we don't know which documents were in fact deleted (i.e., our // versionMap isn't updated), so we must force a cutover to a new reader to "see" the deletions: refresh("delete_by_query"); }
public Query createCommonTermsQuery(String field, String queryText, Occur highFreqOccur, Occur lowFreqOccur, float maxTermFrequency, MappedFieldType fieldType) { Query booleanQuery = createBooleanQuery(field, queryText, lowFreqOccur); if (booleanQuery != null && booleanQuery instanceof BooleanQuery) { BooleanQuery bq = (BooleanQuery) booleanQuery; ExtendedCommonTermsQuery query = new ExtendedCommonTermsQuery(highFreqOccur, lowFreqOccur, maxTermFrequency, ((BooleanQuery)booleanQuery).isCoordDisabled(), fieldType); for (BooleanClause clause : bq.clauses()) { if (!(clause.getQuery() instanceof TermQuery)) { return booleanQuery; } query.add(((TermQuery) clause.getQuery()).getTerm()); } return query; } return booleanQuery; }
public static BooleanQuery applyMinimumShouldMatch(BooleanQuery query, @Nullable String minimumShouldMatch) { if (minimumShouldMatch == null) { return query; } int optionalClauses = 0; for (BooleanClause c : query.clauses()) { if (c.getOccur() == BooleanClause.Occur.SHOULD) { optionalClauses++; } } int msm = calculateMinShouldMatch(optionalClauses, minimumShouldMatch); if (0 < msm) { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.setDisableCoord(query.isCoordDisabled()); for (BooleanClause clause : query) { builder.add(clause); } builder.setMinimumNumberShouldMatch(msm); BooleanQuery bq = builder.build(); bq.setBoost(query.getBoost()); query = bq; } return query; }
@Override protected void applyConstraints(BooleanQuery query) { if (fileNames != null) { BooleanQuery subQuery = new BooleanQuery(true); for (String pattern: Splitter.on(",").omitEmptyStrings().trimResults().split(fileNames.toLowerCase())) subQuery.add(new WildcardQuery(new Term(BLOB_NAME.name(), pattern)), Occur.SHOULD); if (subQuery.getClauses().length != 0) query.add(subQuery, Occur.MUST); } if (regex) query.add(new RegexLiterals(term).asNGramQuery(BLOB_TEXT.name(), NGRAM_SIZE), Occur.MUST); else if (term.length() >= NGRAM_SIZE) query.add(new NGramLuceneQuery(BLOB_TEXT.name(), term, NGRAM_SIZE), Occur.MUST); else throw new TooGeneralQueryException(); }
/** * @param fieldName * @param gramSize * @return * @throws TooGeneralQueryException */ public Query asNGramQuery(String fieldName, int gramSize) throws TooGeneralQueryException { BooleanQuery orQuery = new BooleanQuery(); for (List<LeafLiterals> row: rows) { BooleanQuery andQuery = new BooleanQuery(); for (LeafLiterals literals: row) { if (literals.getLiteral() != null && literals.getLiteral().length()>=NGRAM_SIZE) andQuery.add(new NGramLuceneQuery(fieldName, literals.getLiteral(), gramSize), Occur.MUST); } if (andQuery.getClauses().length != 0) orQuery.add(andQuery, Occur.SHOULD); } if (orQuery.getClauses().length != 0) return orQuery; else throw new TooGeneralQueryException(); }
@Override protected void applyConstraints(BooleanQuery query) { boolean tooGeneral = true; for (char ch: fileNames.toCharArray()) { if (ch != '?' && ch != '*' && ch != ',' && ch != '.') { tooGeneral = false; break; } } if (tooGeneral) throw new TooGeneralQueryException(); BooleanQuery subQuery = new BooleanQuery(true); for (String pattern: Splitter.on(",").omitEmptyStrings().trimResults().split(fileNames.toLowerCase())) subQuery.add(new WildcardQuery(new Term(BLOB_NAME.name(), pattern)), Occur.SHOULD); if (subQuery.getClauses().length != 0) query.add(subQuery, Occur.MUST); else throw new TooGeneralQueryException(); }
private void addListQuery(String location, PubMedIndexField field, QueryFactory queryFactory) throws IOException, URISyntaxException { BooleanQuery booleanQuery = new BooleanQuery(); StreamFactory streamFactory = new StreamFactory(); SourceStream source = streamFactory.getSourceStream(location); try (BufferedReader r = source.getBufferedReader()) { while (true) { String line = r.readLine(); if (line == null) { break; } Query q = queryFactory.createQuery(field, line.trim()); booleanQuery.add(q, Occur.SHOULD); } } addClause(booleanQuery); }
@Override public ScoreDoc[] prefixSearch(String keywords) throws IOException { if (StringUtils.isEmpty(keywords) || keywords.length() > appConfig.getKeywordMaxLength()) { logger.error("empty keywords or over-length! {}", keywords); return null; } Sort sort = new Sort(new SortField("downloadRank", SortField.INT, true)); Term nameFldTerm = new Term(fieldName, keywords); PrefixQuery nameFldQuery = new PrefixQuery(nameFldTerm); NumericRangeQuery<Integer> catalogQuery = NumericRangeQuery.newIntRange("catalog", (int) EnumCatalog.SOFT.getCatalog(), (int) EnumCatalog.GAME.getCatalog(), true, true); BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(catalogQuery, Occur.MUST); booleanQuery.add(nameFldQuery, Occur.MUST); TopDocs topDocs = quickTipsSearcher.search(booleanQuery, appConfig.getQuickTipsNum() * 2, sort); ScoreDoc[] docs = topDocs.scoreDocs; return docs; }
/** * Parses the query. Using this instead of a QueryParser in order * to avoid thread-safety issues with Lucene's query parser. * * @param fieldName the name of the field * @param value the value of the field * @return the parsed query */ private Query parseTokens(String fieldName, String value) { BooleanQuery searchQuery = new BooleanQuery(); if (value != null) { Analyzer analyzer = new KeywordAnalyzer(); try { TokenStream tokenStream = analyzer.tokenStream(fieldName, new StringReader(value)); tokenStream.reset(); CharTermAttribute attr = tokenStream.getAttribute(CharTermAttribute.class); while (tokenStream.incrementToken()) { String term = attr.toString(); Query termQuery = new TermQuery(new Term(fieldName, term)); searchQuery.add(termQuery, Occur.SHOULD); } } catch (IOException e) { throw new DukeException("Error parsing input string '" + value + "' " + "in field " + fieldName); } } return searchQuery; }
@Override public void addQueryTerms(BooleanQuery query, AdvancedSearchParams params) { try { ZipscriptQueryParams queryParams = params.getExtensionData(ZipscriptQueryParams.ZIPSCRIPTQUERYPARAMS); if (queryParams.getMinPresent() != null || queryParams.getMaxPresent() != null) { Query presentQuery = NumericRangeQuery.newIntRange("present", queryParams.getMinPresent(), queryParams.getMaxPresent(), true, true); query.add(presentQuery, Occur.MUST); } if (queryParams.getMinMissing() != null || queryParams.getMaxMissing() != null) { Query missingQuery = NumericRangeQuery.newIntRange("missing", queryParams.getMinMissing(), queryParams.getMaxMissing(), true, true); query.add(missingQuery, Occur.MUST); } if (queryParams.getMinPercent() != null || queryParams.getMaxPercent() != null) { Query percentQuery = NumericRangeQuery.newIntRange("percent", queryParams.getMinPercent(), queryParams.getMaxPercent(), true, true); query.add(percentQuery, Occur.MUST); } } catch (KeyNotFoundException e) { // No MP3 terms to include, return without amending query } }
/** * Returns a grouped facet count for the facet query * * @see FacetParams#FACET_QUERY */ public int getGroupedFacetQueryCount(Query facetQuery, DocSet docSet) throws IOException { // It is okay to retrieve group.field from global because it is never a local param String groupField = global.get(GroupParams.GROUP_FIELD); if (groupField == null) { throw new SolrException ( SolrException.ErrorCode.BAD_REQUEST, "Specify the group.field as parameter or local parameter" ); } TermAllGroupsCollector collector = new TermAllGroupsCollector(groupField); Filter mainQueryFilter = docSet.getTopFilter(); // This returns a filter that only matches documents matching with q param and fq params Query filteredFacetQuery = new BooleanQuery.Builder() .add(facetQuery, Occur.MUST) .add(mainQueryFilter, Occur.FILTER) .build(); searcher.search(filteredFacetQuery, collector); return collector.getGroupCount(); }
private Query createPhraseQuery(EntityDisambiguationDPO dpo, EntityCentricKnowledgeBase kb) { LearnToRankQuery query = new LearnToRankQuery(); List<LearnToRankClause> features = new LinkedList<LearnToRankClause>(); DefaultSimilarity defaultSim = new DefaultSimilarity(); LTRBooleanQuery bq = new LTRBooleanQuery(); bq.add(LuceneFeatures.queryLabelTerm(dpo.getSelectedText(), "UniqueLabelString", defaultSim), Occur.SHOULD); bq.add(LuceneFeatures.queryLabelTerm(dpo.getSelectedText(), "Label", defaultSim), Occur.SHOULD); // Feature 1 features.add(query.add(bq, "Feature1", true)); // Feature 2 features.add(query.add( LuceneFeatures.querySensePrior(dpo.getSelectedText(), kb.getFeatureDefinition()), "Feature2", false)); features.get(0).setWeight(1f); features.get(1).setWeight(1f); return query; }
private Query createFuzzyQuery(EntityDisambiguationDPO dpo, EntityCentricKnowledgeBase kb) { LearnToRankQuery query = new LearnToRankQuery(); List<LearnToRankClause> features = new LinkedList<LearnToRankClause>(); DefaultSimilarity defaultSim = new DefaultSimilarity(); // Feature 1 features.add(query.add(LuceneFeatures.queryStringTerm( dpo.getSelectedText(), "Label", defaultSim, Occur.SHOULD, DisambiguationMainService.MAXCLAUSECOUNT), "Feature1", true)); // Feature 2 features.add(query.add( LuceneFeatures.querySensePrior(dpo.getSelectedText(), kb.getFeatureDefinition()), "Feature2", false)); features.get(0).setWeight(0.0915161f); features.get(1).setWeight(0.350994f); return query; }
private Set<Document> queryLuceneLabel(String surfaceForm, List<String> candidates) { Set<Document> documents = new HashSet<Document>(); BooleanQuery query = new BooleanQuery(); String[] splitter = surfaceForm.toLowerCase().split(" "); for (int i = 0; i < splitter.length; i++) { query.add(new TermQuery(new Term("Label", splitter[i])), Occur.MUST); } final IndexSearcher searcher = eckb.getSearcher(); final IndexReader reader = searcher.getIndexReader(); try { final TopDocs top = searcher.search(query, 25000); final ScoreDoc[] score = top.scoreDocs; for (int i = 0; i < score.length; i++) { final Document doc = reader.document(score[i].doc); documents.add(doc); } } catch (IOException e) { Logger.getRootLogger().error("Lucene Searcher Error: ", e); } return documents; }