org.apache.lucene.search.BooleanQuery Java Examples

The following examples show how to use org.apache.lucene.search.BooleanQuery. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DBPediaCandidateType.java    From uncc2014watsonsim with GNU General Public License v2.0 6 votes vote down vote up
public List<String> query(String question_text) {
	List<String> results = new ArrayList<>();
	try {
		BooleanQuery q = new BooleanQuery();
		for (String word : question_text.split("\\W+")) {
			q.add(new TermQuery(new Term("text", word)), BooleanClause.Occur.SHOULD);
			q.add(new TermQuery(new Term("text", word.toLowerCase())), BooleanClause.Occur.SHOULD);
		}
		TopDocs topDocs = searcher.search(q, 1);
		
		ScoreDoc[] hits = topDocs.scoreDocs;
		// This isn't range based because we need the rank
		for (int i=0; i < hits.length; i++) {
			ScoreDoc s = hits[i];
			Document doc = searcher.doc(s.doc);
			results.add(doc.get("uri"));
		}
	} catch (IOException e) {
		System.out.println("Failed to query Lucene. Is the index in the correct location?");
		e.printStackTrace();
	}
	return results;
}
 
Example #2
Source File: TestHighlightingMatcher.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testWildcardCombinations() throws Exception {

    final BooleanQuery bq = new BooleanQuery.Builder()
        .add(new TermQuery(new Term(FIELD, "term1")), BooleanClause.Occur.MUST)
        .add(new PrefixQuery(new Term(FIELD, "term2")), BooleanClause.Occur.MUST)
        .add(new TermQuery(new Term(FIELD, "term3")), BooleanClause.Occur.MUST_NOT)
        .build();

    try (Monitor monitor = newMonitor()) {
      monitor.register(new MonitorQuery("1", bq));

      MatchingQueries<HighlightsMatch> matches = monitor.match(buildDoc("term1 term22 term4"), HighlightsMatch.MATCHER);
      HighlightsMatch m = matches.matches("1");
      assertNotNull(m);
      assertEquals(2, m.getHitCount());
    }

  }
 
Example #3
Source File: MoreLikeThisHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private Query getBoostedQuery(Query mltquery) {
  BooleanQuery boostedQuery = (BooleanQuery)mltquery;
  if (boostFields.size() > 0) {
    BooleanQuery.Builder newQ = new BooleanQuery.Builder();
    newQ.setMinimumNumberShouldMatch(boostedQuery.getMinimumNumberShouldMatch());
    for (BooleanClause clause : boostedQuery) {
      Query q = clause.getQuery();
      float originalBoost = 1f;
      if (q instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) q;
        q = bq.getQuery();
        originalBoost = bq.getBoost();
      }
      Float fieldBoost = boostFields.get(((TermQuery) q).getTerm().field());
      q = ((fieldBoost != null) ? new BoostQuery(q, fieldBoost * originalBoost) : clause.getQuery());
      newQ.add(q, clause.getOccur());
    }
    boostedQuery = newQ.build();
  }
  return boostedQuery;
}
 
Example #4
Source File: RegexLiterals.java    From onedev with MIT License 6 votes vote down vote up
/**
 * @param fieldName
 * @param gramSize
 * @return
 * @throws TooGeneralQueryException
 */
public Query asNGramQuery(String fieldName, int gramSize) throws TooGeneralQueryException {
	BooleanQuery.Builder orQueryBuilder = new BooleanQuery.Builder();
	for (List<LeafLiterals> row: rows) {
		BooleanQuery.Builder andQueryBuilder = new BooleanQuery.Builder();
		for (LeafLiterals literals: row) {
			if (literals.getLiteral() != null && literals.getLiteral().length()>=NGRAM_SIZE)
				andQueryBuilder.add(new NGramLuceneQuery(fieldName, literals.getLiteral(), gramSize), Occur.MUST);
		}
		BooleanQuery andQuery = andQueryBuilder.build();
		if (andQuery.clauses().size() != 0)
			orQueryBuilder.add(andQuery, Occur.SHOULD);
	}
	BooleanQuery orQuery = orQueryBuilder.build();
	if (orQuery.clauses().size() != 0)
		return orQuery;
	else
		throw new TooGeneralQueryException();
}
 
Example #5
Source File: ZipscriptQueryExtension.java    From drftpd with GNU General Public License v2.0 6 votes vote down vote up
@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
    }
}
 
Example #6
Source File: FieldQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
void saveTerms( Collection<Query> flatQueries, IndexReader reader ) throws IOException{
  for( Query query : flatQueries ){
    while (query instanceof BoostQuery) {
      query = ((BoostQuery) query).getQuery();
    }
    Set<String> termSet = getTermSet( query );
    if( query instanceof TermQuery )
      termSet.add( ((TermQuery)query).getTerm().text() );
    else if( query instanceof PhraseQuery ){
      for( Term term : ((PhraseQuery)query).getTerms() )
        termSet.add( term.text() );
    }
    else if (query instanceof MultiTermQuery && reader != null) {
      BooleanQuery mtqTerms = (BooleanQuery) query.rewrite(reader);
      for (BooleanClause clause : mtqTerms) {
        termSet.add (((TermQuery) clause.getQuery()).getTerm().text());
      }
    }
    else
      throw new RuntimeException( "query \"" + query.toString() + "\" must be flatten first." );
  }
}
 
Example #7
Source File: MoreLikeThisHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public DocListAndSet getMoreLikeThis( int id, int start, int rows, List<Query> filters, List<InterestingTerm> terms, int flags ) throws IOException
{
  Document doc = reader.document(id);
  rawMLTQuery = mlt.like(id);
  boostedMLTQuery = getBoostedQuery( rawMLTQuery );
  if( terms != null ) {
    fillInterestingTermsFromMLTQuery( boostedMLTQuery, terms );
  }

  // exclude current document from results
  BooleanQuery.Builder realMLTQuery = new BooleanQuery.Builder();
  realMLTQuery.add(boostedMLTQuery, BooleanClause.Occur.MUST);
  realMLTQuery.add(
      new TermQuery(new Term(uniqueKeyField.getName(), uniqueKeyField.getType().storedToIndexed(doc.getField(uniqueKeyField.getName())))), 
        BooleanClause.Occur.MUST_NOT);
  this.realMLTQuery = realMLTQuery.build();
  
  DocListAndSet results = new DocListAndSet();
  if (this.needDocSet) {
    results = searcher.getDocListAndSet(this.realMLTQuery, filters, null, start, rows, flags);
  } else {
    results.docList = searcher.getDocList(this.realMLTQuery, filters, null, start, rows, flags);
  }
  return results;
}
 
Example #8
Source File: DismaxSearchEngineRequestAdapter.java    From querqy with Apache License 2.0 6 votes vote down vote up
@Override
public Query applyMinimumShouldMatch(final BooleanQuery query) {

    final List<BooleanClause> clauses = query.clauses();
    if (clauses.size() < 2) {
        return query;
    }

    for (final BooleanClause clause : clauses) {
        if ((clause.getQuery() instanceof BooleanQuery) && (clause.getOccur() != BooleanClause.Occur.MUST)) {
            return query; // seems to be a complex query with sub queries - do not
            // apply mm
        }
    }

    return SolrPluginUtils.setMinShouldMatch(query, minShouldMatch);

}
 
Example #9
Source File: TestUnifiedHighlighterStrictPhrases.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testWithSameTermQuery() throws IOException {
  indexWriter.addDocument(newDoc("Yin yang, yin gap yang"));
  initReaderSearcherHighlighter();

  BooleanQuery query = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("body", "yin")), BooleanClause.Occur.MUST)
      .add(newPhraseQuery("body", "yin yang"), BooleanClause.Occur.MUST)
      // add queries for other fields; we shouldn't highlight these because of that.
      .add(new TermQuery(new Term("title", "yang")), BooleanClause.Occur.SHOULD)
      .build();

  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  dupMatchAllowed.set(false); // We don't want duplicates from "Yin" being in TermQuery & PhraseQuery.
  String[] snippets = highlighter.highlight("body", query, topDocs);
  if (highlighter.getFlags("body").contains(HighlightFlag.WEIGHT_MATCHES)) {
    assertArrayEquals(new String[]{"<b>Yin yang</b>, <b>yin</b> gap yang"}, snippets);
  } else {
    assertArrayEquals(new String[]{"<b>Yin</b> <b>yang</b>, <b>yin</b> gap yang"}, snippets);
  }
}
 
Example #10
Source File: LuceneQueryConverter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private Query toBooleanQuery(SearchQuery.Boolean booleanQuery) {
  final BooleanQuery.Builder builder = new BooleanQuery.Builder();
  final BooleanClause.Occur occur;
  switch(booleanQuery.getOp()) {
  case AND:
    occur = BooleanClause.Occur.MUST;
    break;
  case OR:
    occur = BooleanClause.Occur.SHOULD;
      break;
  default:
    throw new AssertionError("Unknown boolean operator: " + booleanQuery.getOp());
  }

  for(SearchQuery clause: booleanQuery.getClausesList()) {
    builder.add(toLuceneQuery(clause), occur);
  }
  return builder.build();
}
 
Example #11
Source File: TestQueryParser.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testEnableGraphQueries() throws Exception {
  QueryParser dumb = new QueryParser("field", new Analyzer1());
  dumb.setSplitOnWhitespace(false);
  dumb.setEnableGraphQueries(false);
  
  TermQuery pig = new TermQuery(new Term("field", "pig"));

  // A multi-word synonym source will just form a boolean query when graph queries are disabled:
  Query inner = new SynonymQuery.Builder("field")
      .addTerm(new Term("field", "cavy"))
      .addTerm(new Term("field", "guinea"))
      .build();
  BooleanQuery.Builder b = new BooleanQuery.Builder();
  b.add(inner, BooleanClause.Occur.SHOULD);
  b.add(pig, BooleanClause.Occur.SHOULD);
  BooleanQuery query = b.build();
  assertEquals(query, dumb.parse("guinea pig"));
}
 
Example #12
Source File: MultiMatchQueryBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Query combineGrouped(List<Query> queries) {
    if (queries == null || queries.isEmpty()) {
        return null;
    }
    if (queries.size() == 1) {
        return queries.get(0);
    }
    if (groupDismax) {
        return new DisjunctionMaxQuery(queries, tieBreaker);
    } else {
        BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
        for (Query query : queries) {
            booleanQuery.add(query, BooleanClause.Occur.SHOULD);
        }
        return booleanQuery.build();
    }
}
 
Example #13
Source File: SuperParserTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Test
public void test5() throws ParseException, IOException {
  parser = new SuperParser(LUCENE_VERSION, getFieldManager(new WhitespaceAnalyzer(LUCENE_VERSION)), true, null,
      ScoreType.SUPER, new Term("_primedoc_"));
  Query query = parser.parse("<a.a:a a.d:{e TO f} a.b:b a.test:hello\\<> -<g.c:c g.d:d>");

  BooleanQuery booleanQuery1 = new BooleanQuery();
  booleanQuery1.add(new TermQuery(new Term("a.a", "a")), Occur.SHOULD);
  booleanQuery1.add(new TermRangeQuery("a.d", new BytesRef("e"), new BytesRef("f"), false, false), Occur.SHOULD);
  booleanQuery1.add(new TermQuery(new Term("a.b", "b")), Occur.SHOULD);
  // std analyzer took the "<" out
  booleanQuery1.add(new TermQuery(new Term("a.test", "hello<")), Occur.SHOULD);

  BooleanQuery booleanQuery2 = new BooleanQuery();
  booleanQuery2.add(new TermQuery(new Term("g.c", "c")), Occur.SHOULD);
  booleanQuery2.add(new TermQuery(new Term("g.d", "d")), Occur.SHOULD);

  SuperQuery superQuery1 = new SuperQuery(booleanQuery1, ScoreType.SUPER, new Term("_primedoc_"));
  SuperQuery superQuery2 = new SuperQuery(booleanQuery2, ScoreType.SUPER, new Term("_primedoc_"));

  BooleanQuery booleanQuery = new BooleanQuery();
  booleanQuery.add(superQuery1, Occur.SHOULD);
  booleanQuery.add(superQuery2, Occur.MUST_NOT);

  assertQuery(booleanQuery, query);
}
 
Example #14
Source File: TestBooleanQueryExtractor.java    From solr-redis with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtractTwoSubqueryFields() {
  Query q1 = new TermQuery(new Term("field1", "value1"));
  Query q2 = new TermQuery(new Term("field2", "value2"));
  
  BooleanQuery.Builder builder = new BooleanQuery.Builder();
  builder.add(new BooleanClause(q1, BooleanClause.Occur.MUST));
  builder.add(new BooleanClause(q2, BooleanClause.Occur.MUST));
  BooleanQuery booleanQuery = builder.build();
  BooleanQueryExtractor booleanQueryExtractor = new BooleanQueryExtractor();

  Set<String> extractedFieldNames = new HashSet<>();

  booleanQueryExtractor.extractSubQueriesFields(booleanQuery, DEFAULT_EXTRACTORS, extractedFieldNames);
  assertEquals(2, extractedFieldNames.size());
  assertTrue(extractedFieldNames.contains("field1"));
  assertTrue(extractedFieldNames.contains("field2"));
}
 
Example #15
Source File: MultipassTermFilteredPresearcher.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Query build() {
  Map<String, BytesRef[]> collectedTerms = new HashMap<>();
  for (Map.Entry<String, BytesRefHash> entry : terms.entrySet()) {
    collectedTerms.put(entry.getKey(), convertHash(entry.getValue()));
  }
  BooleanQuery.Builder parent = new BooleanQuery.Builder();
  for (int i = 0; i < passes; i++) {
    BooleanQuery.Builder child = new BooleanQuery.Builder();
    for (String field : terms.keySet()) {
      child.add(new TermInSetQuery(field(field, i), collectedTerms.get(field)), BooleanClause.Occur.SHOULD);
    }
    parent.add(child.build(), BooleanClause.Occur.MUST);
  }
  return parent.build();
}
 
Example #16
Source File: QueryBuilder.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Creates graph phrase query from the tokenstream contents
 */
protected Query analyzeGraphPhrase(TokenStream source, String field, int phraseSlop)
    throws IOException {
  source.reset();
  GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(source);

  // Creates a boolean query from the graph token stream by extracting all the
  // finite strings from the graph and using them to create phrase queries with
  // the appropriate slop.
  BooleanQuery.Builder builder = new BooleanQuery.Builder();
  Iterator<TokenStream> it = graph.getFiniteStrings();
  while (it.hasNext()) {
    Query query = createFieldQuery(it.next(), BooleanClause.Occur.MUST, field, true, phraseSlop);
    if (query != null) {
      builder.add(query, BooleanClause.Occur.SHOULD);
    }
  }
  return builder.build();

}
 
Example #17
Source File: TestUnifiedHighlighterStrictPhrases.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Test it does *not* highlight the same term's not next to the span-near.  "charlie" in this case.
 * This particular example exercises "Rewrite" plus "MTQ" in the same query.
 */
public void testRewriteAndMtq() throws IOException {
  indexWriter.addDocument(newDoc("alpha bravo charlie - charlie bravo alpha"));
  initReaderSearcherHighlighter();

  SpanNearQuery snq = new SpanNearQuery(
      new SpanQuery[]{
          new SpanTermQuery(new Term("body", "bravo")),
          new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term("body", "ch")))}, // REWRITES
      0, true);

  BooleanQuery query = new BooleanQuery.Builder()
      .add(snq, BooleanClause.Occur.MUST)
      .add(new PrefixQuery(new Term("body", "al")), BooleanClause.Occur.MUST) // MTQ
      .add(newPhraseQuery("body", "alpha bravo"), BooleanClause.Occur.MUST)
      // add queries for other fields; we shouldn't highlight these because of that.
      .add(newPhraseQuery("title", "bravo alpha"), BooleanClause.Occur.SHOULD)
      .build();

  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  String[] snippets = highlighter.highlight("body", query, topDocs);

  if (highlighter.getFlags("body").contains(HighlightFlag.WEIGHT_MATCHES)) {
    assertArrayEquals(new String[]{"<b>alpha bravo</b> <b>charlie</b> - charlie bravo <b>alpha</b>"}, snippets);
  } else {
    assertArrayEquals(new String[]{"<b>alpha</b> <b>bravo</b> <b>charlie</b> - charlie bravo <b>alpha</b>"}, snippets);
  }

  // do again, this time with MTQ disabled.  We should only find "alpha bravo".
  highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
  highlighter.setHandleMultiTermQuery(false);//disable but leave phrase processing enabled

  topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  snippets = highlighter.highlight("body", query, topDocs);

  assertArrayEquals(new String[]{"<b>alpha</b> <b>bravo</b> charlie - charlie bravo alpha"},
      snippets);
}
 
Example #18
Source File: LuceneEventIndex.java    From nifi with Apache License 2.0 6 votes vote down vote up
private BooleanQuery buildLineageQuery(final Collection<String> flowFileUuids) {
    // Create a query for all Events related to the FlowFiles of interest. We do this by adding all ID's as
    // "SHOULD" clauses and then setting the minimum required to 1.
    final BooleanQuery lineageQuery;
    if (flowFileUuids == null || flowFileUuids.isEmpty()) {
        lineageQuery = null;
    } else {
        final BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
        for (final String flowFileUuid : flowFileUuids) {
            final TermQuery termQuery = new TermQuery(new Term(SearchableFields.FlowFileUUID.getSearchableFieldName(), flowFileUuid));
            queryBuilder.add(new BooleanClause(termQuery, BooleanClause.Occur.SHOULD));
        }

        lineageQuery = queryBuilder.build();
    }

    return lineageQuery;
}
 
Example #19
Source File: InternalEngine.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example #20
Source File: TestFunctionScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testScoreModifyingSource() throws Exception {

    BooleanQuery bq = new BooleanQuery.Builder()
        .add(new TermQuery(new Term(TEXT_FIELD, "first")), BooleanClause.Occur.SHOULD)
        .add(new TermQuery(new Term(TEXT_FIELD, "text")), BooleanClause.Occur.SHOULD)
        .build();
    TopDocs plain = searcher.search(bq, 1);

    FunctionScoreQuery fq = FunctionScoreQuery.boostByValue(bq, DoubleValuesSource.fromIntField("iii"));

    QueryUtils.check(random(), fq, searcher, rarely());

    int[] expectedDocs = new int[]{ 4, 7, 9, 8, 12 };
    TopDocs docs = searcher.search(fq, 5);
    assertEquals(plain.totalHits.value, docs.totalHits.value);
    for (int i = 0; i < expectedDocs.length; i++) {
      assertEquals(expectedDocs[i], docs.scoreDocs[i].doc);
    }

    Explanation expl = searcher.explain(fq, 4);
    assertTrue(expl.toString().contains("first"));
    assertTrue(expl.toString().contains("iii"));

  }
 
Example #21
Source File: PercolatorService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #22
Source File: LuceneMessageSearchIndex.java    From james-project with Apache License 2.0 6 votes vote down vote up
private void update(MailboxId mailboxId, MessageUid uid, Flags f) throws IOException {
    try (IndexSearcher searcher = new IndexSearcher(IndexReader.open(writer, true))) {
        BooleanQuery query = new BooleanQuery();
        query.add(new TermQuery(new Term(MAILBOX_ID_FIELD, mailboxId.serialize())), BooleanClause.Occur.MUST);
        query.add(createQuery(MessageRange.one(uid)), BooleanClause.Occur.MUST);
        query.add(new PrefixQuery(new Term(FLAGS_FIELD, "")), BooleanClause.Occur.MUST);

        TopDocs docs = searcher.search(query, 100000);
        ScoreDoc[] sDocs = docs.scoreDocs;
        for (ScoreDoc sDoc : sDocs) {
            Document doc = searcher.doc(sDoc.doc);

            doc.removeFields(FLAGS_FIELD);
            indexFlags(doc, f);

            writer.updateDocument(new Term(ID_FIELD, doc.get(ID_FIELD)), doc);
        }
    }
}
 
Example #23
Source File: MoreLikeThisQueryParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static void handleExclude(BooleanQuery boolQuery, List<Item> likeItems) {
    // artificial docs get assigned a random id and should be disregarded
    List<BytesRef> uids = new ArrayList<>();
    for (Item item : likeItems) {
        if (item.doc() != null) {
            continue;
        }
        uids.add(createUidAsBytes(item.type(), item.id()));
    }
    if (!uids.isEmpty()) {
        TermsQuery query = new TermsQuery(UidFieldMapper.NAME, uids.toArray(new BytesRef[0]));
        boolQuery.add(query, BooleanClause.Occur.MUST_NOT);
    }
}
 
Example #24
Source File: IndexTimeSynonymTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testFieldPhraseListIndex2w1wSearch1term1phrase() throws Exception {
  makeIndex2w1w();

  BooleanQuery.Builder bq = new BooleanQuery.Builder();
  bq.add( tq( "pc" ), Occur.SHOULD );
  bq.add( pqF( "personal", "computer" ), Occur.SHOULD );
  FieldQuery fq = new FieldQuery( bq.build(), true, true );
  FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
  FieldPhraseList fpl = new FieldPhraseList( stack, fq );
  assertEquals( 1, fpl.phraseList.size() );
  assertTrue( fpl.phraseList.get( 0 ).toString().indexOf( "(1.0)((3,20))" ) > 0 );
  assertEquals( 3, fpl.phraseList.get( 0 ).getStartOffset() );
  assertEquals( 20, fpl.phraseList.get( 0 ).getEndOffset() );
}
 
Example #25
Source File: KNearestFuzzyClassifier.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private TopDocs knnSearch(String text) throws IOException {
  BooleanQuery.Builder bq = new BooleanQuery.Builder();
  NearestFuzzyQuery nearestFuzzyQuery = new NearestFuzzyQuery(analyzer);
  for (String fieldName : textFieldNames) {
    nearestFuzzyQuery.addTerms(text, fieldName);
  }
  bq.add(nearestFuzzyQuery, BooleanClause.Occur.MUST);
  Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*"));
  bq.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST));
  if (query != null) {
    bq.add(query, BooleanClause.Occur.MUST);
  }
  return indexSearcher.search(bq.build(), k);
}
 
Example #26
Source File: BM25NBClassifier.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private double getTermProbForClass(Term classTerm, String... words) throws IOException {
  BooleanQuery.Builder builder = new BooleanQuery.Builder();
  builder.add(new BooleanClause(new TermQuery(classTerm), BooleanClause.Occur.MUST));
  for (String textFieldName : textFieldNames) {
    for (String word : words) {
      builder.add(new BooleanClause(new TermQuery(new Term(textFieldName, word)), BooleanClause.Occur.SHOULD));
    }
  }
  if (query != null) {
    builder.add(query, BooleanClause.Occur.MUST);
  }
  TopDocs search = indexSearcher.search(builder.build(), 1);
  return search.totalHits.value > 0 ? search.scoreDocs[0].score : 1;
}
 
Example #27
Source File: TestUnifiedHighlighter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testCambridgeMA() throws Exception {
  BufferedReader r = new BufferedReader(new InputStreamReader(
      this.getClass().getResourceAsStream("CambridgeMA.utf8"), StandardCharsets.UTF_8));
  String text = r.readLine();
  r.close();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
  Field body = new Field("body", text, fieldType);
  Document document = new Document();
  document.add(body);
  iw.addDocument(document);
  IndexReader ir = iw.getReader();
  iw.close();
  IndexSearcher searcher = newSearcher(ir);
  BooleanQuery query = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("body", "porter")), BooleanClause.Occur.SHOULD)
      .add(new TermQuery(new Term("body", "square")), BooleanClause.Occur.SHOULD)
      .add(new TermQuery(new Term("body", "massachusetts")), BooleanClause.Occur.SHOULD)
      .build();
  TopDocs topDocs = searcher.search(query, 10);
  assertEquals(1, topDocs.totalHits.value);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  highlighter.setMaxLength(Integer.MAX_VALUE - 1);
  String snippets[] = highlighter.highlight("body", query, topDocs, 2);
  assertEquals(1, snippets.length);
  assertTrue(snippets[0].contains("<b>Square</b>"));
  assertTrue(snippets[0].contains("<b>Porter</b>"));
  ir.close();
}
 
Example #28
Source File: TestFunctionScoreExplanations.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testTopLevelBoost() throws Exception {
  Query q = new TermQuery(new Term(FIELD, "w1"));
  FunctionScoreQuery csq = new FunctionScoreQuery(q, DoubleValuesSource.constant(5));
  BooleanQuery.Builder bqB = new BooleanQuery.Builder();
  bqB.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
  bqB.add(csq, BooleanClause.Occur.MUST);
  BooleanQuery bq = bqB.build();
  qtest(new BoostQuery(bq, 6), new int[] { 0,1,2,3 });
}
 
Example #29
Source File: BBoxStrategy.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Makes a boolean query based upon a collection of queries and a logical operator.
 *
 * @param occur the logical operator
 * @param queries the query collection
 * @return the query
 */
BooleanQuery makeQuery(BooleanClause.Occur occur, Query... queries) {
  BooleanQuery.Builder bq = new BooleanQuery.Builder();
  for (Query query : queries) {
    if (query != null)
      bq.add(query, occur);
  }
  return bq.build();
}
 
Example #30
Source File: TestSimpleQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDisableSlop() {
  PhraseQuery expectedPhrase = new PhraseQuery("field", "foo", "bar");

  BooleanQuery.Builder expected = new BooleanQuery.Builder();
  expected.add(expectedPhrase, Occur.MUST);
  expected.add(new TermQuery(new Term("field", "~2")), Occur.MUST);
  assertEquals(expected.build(), parse("\"foo bar\"~2", ~NEAR_OPERATOR));
}