Java Code Examples for org.apache.lucene.search.BooleanClause.Occur#SHOULD

The following examples show how to use org.apache.lucene.search.BooleanClause.Occur#SHOULD . 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: TestBooleanRewrites.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private Query randomBooleanQuery() {
  if (random().nextInt(10) == 0) {
    return new BoostQuery(randomBooleanQuery(), TestUtil.nextInt(random(), 1, 10));
  }
  final int numClauses = random().nextInt(5);
  BooleanQuery.Builder b = new BooleanQuery.Builder();
  int numShoulds = 0;
  for (int i = 0; i < numClauses; ++i) {
    final Occur occur = Occur.values()[random().nextInt(Occur.values().length)];
    if (occur == Occur.SHOULD) {
      numShoulds++;
    }
    final Query query = randomQuery();
    b.add(query, occur);
  }
  b.setMinimumNumberShouldMatch(random().nextBoolean() ? 0 : TestUtil.nextInt(random(), 0, numShoulds + 1));
  return b.build();
}
 
Example 2
Source File: WebDSLFacet.java    From webdsl with Apache License 2.0 6 votes vote down vote up
public static WebDSLFacet fromParamMap(Map<String,String> paramMap){
    WebDSLFacet f = new WebDSLFacet();
    try{
        String key, value;
        for (Map.Entry<String, String> e : paramMap.entrySet()) {
            key = e.getKey();
            value = e.getValue();
            if ("fn".equals(key)) {
                f.fieldName = value;
            } else if ("v".equals(key)) {
                f.value = value;
            } else if ("cnt".equals(key)) {
                f.count = Integer.parseInt(value);
            } else if ("occ".equals(key)){
                f.occur = Occur.valueOf(value);
            } else if ("sel".equals(key)){
                f.isSelected = Boolean.parseBoolean(value);
            }
        }
    } catch (Exception ex){
        return new WebDSLFacet("Illegal facet", "Illegal facet", Occur.SHOULD, 0);
    }
    return f;
}
 
Example 3
Source File: BooleanClauseWritable.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Override
public void readFields(DataInput in) throws IOException {
  Occur occur = null;
  byte o = in.readByte();
  switch (o) {
  case 0:
    occur = Occur.MUST;
    break;
  case 1:
    occur = Occur.MUST_NOT;
    break;
  case 2:
    occur = Occur.SHOULD;
    break;
  default:
    throw new RuntimeException("Occur [" + o + "] not supported");
  }
  QueryWritable queryWritable = new QueryWritable();
  queryWritable.readFields(in);
  booleanClause = new BooleanClause(queryWritable.getQuery(), occur);
}
 
Example 4
Source File: QueryUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static Query createUsagesQuery(
        final @NonNull String resourceName,
        final @NonNull Set<? extends ClassIndexImpl.UsageType> mask,
        final @NonNull Occur operator) {
    Parameters.notNull("resourceName", resourceName);
    Parameters.notNull("mask", mask);
    Parameters.notNull("operator", operator);
    if (operator == Occur.SHOULD) {
        final BooleanQuery query = new BooleanQuery ();
        for (ClassIndexImpl.UsageType ut : mask) {
            final Query subQuery = new WildcardQuery(
                DocumentUtil.referencesTerm (
                    resourceName,
                    EnumSet.of(ut),
                    false));
            query.add(subQuery, operator);
        }
        return query;
    } else if (operator == Occur.MUST) {
        return new WildcardQuery(
            DocumentUtil.referencesTerm (
                resourceName,
                mask,
                false));
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 5
Source File: TestMultiFieldQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void assertStopQueryIsMatchNoDocsQuery(String qtxt) throws Exception {
  String[] fields = { "b", "t" };
  Occur occur[] = { Occur.SHOULD, Occur.SHOULD };
  TestQPHelper.QPTestAnalyzer a = new TestQPHelper.QPTestAnalyzer();
  StandardQueryParser mfqp = new StandardQueryParser();
  mfqp.setMultiFields(fields);
  mfqp.setAnalyzer(a);

  Query q = mfqp.parse(qtxt, null);
  assertTrue(q instanceof MatchNoDocsQuery);
}
 
Example 6
Source File: TestMultiFieldQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void assertStopQueryEquals(String qtxt, String expectedRes)
    throws Exception {
  String[] fields = { "b", "t" };
  Occur occur[] = { Occur.SHOULD, Occur.SHOULD };
  TestQPHelper.QPTestAnalyzer a = new TestQPHelper.QPTestAnalyzer();
  StandardQueryParser mfqp = new StandardQueryParser();
  mfqp.setMultiFields(fields);
  mfqp.setAnalyzer(a);

  Query q = mfqp.parse(qtxt, null);
  assertEquals(expectedRes, q.toString().trim());

  q = QueryParserUtil.parse(qtxt, fields, occur, a);
  assertEquals(expectedRes, q.toString().trim());
}
 
Example 7
Source File: TestMultiFieldQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void assertStopQueryEquals (String qtxt, String expectedRes) throws Exception {
  String[] fields = {"b", "t"};
  Occur occur[] = {Occur.SHOULD, Occur.SHOULD};
  TestQueryParser.QPTestAnalyzer a = new TestQueryParser.QPTestAnalyzer();
  MultiFieldQueryParser mfqp = new MultiFieldQueryParser(fields, a);
  
  Query q = mfqp.parse(qtxt);
  assertEquals(expectedRes, q.toString());
  
  q = MultiFieldQueryParser.parse(qtxt, fields, occur, a);
  assertEquals(expectedRes, q.toString());
}
 
Example 8
Source File: BooleanWeight.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
BulkScorer optionalBulkScorer(LeafReaderContext context) throws IOException {
  List<BulkScorer> optional = new ArrayList<BulkScorer>();
  for (WeightedBooleanClause wc : weightedClauses) {
    Weight w = wc.weight;
    BooleanClause c = wc.clause;
    if (c.getOccur() != Occur.SHOULD) {
      continue;
    }
    BulkScorer subScorer = w.bulkScorer(context);

    if (subScorer != null) {
      optional.add(subScorer);
    }
  }

  if (optional.size() == 0) {
    return null;
  }

  if (query.getMinimumNumberShouldMatch() > optional.size()) {
    return null;
  }

  if (optional.size() == 1) {
    return optional.get(0);
  }

  return new BooleanScorer(this, optional, Math.max(1, query.getMinimumNumberShouldMatch()), scoreMode.needsScores());
}
 
Example 9
Source File: SuperParser.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
private boolean containsAllNegativeQueries(BooleanQuery booleanQuery) {
  for (BooleanClause clause : booleanQuery.clauses()) {
    if (clause.getOccur() == Occur.MUST || clause.getOccur() == Occur.SHOULD) {
      return false;
    }
  }
  return true;
}
 
Example 10
Source File: FastVectorHighlighterTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testCommonTermsQueryHighlight() throws IOException {
  Directory dir = newDirectory();
  IndexWriter writer = new IndexWriter(dir,
      newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET))
      .setMergePolicy(newLogMergePolicy())); // don't reorder doc ids
  FieldType type = new FieldType(TextField.TYPE_STORED);
  type.setStoreTermVectorOffsets(true);
  type.setStoreTermVectorPositions(true);
  type.setStoreTermVectors(true);
  type.freeze();
  String[] texts = {
      "Hello this is a piece of text that is very long and contains too much preamble and the meat is really here which says kennedy has been shot",
      "This piece of text refers to Kennedy at the beginning then has a longer piece of text that is very long in the middle and finally ends with another reference to Kennedy",
      "JFK has been shot", "John Kennedy has been shot",
      "This text has a typo in referring to Keneddy",
      "wordx wordy wordz wordx wordy wordx worda wordb wordy wordc", "y z x y z a b", "lets is a the lets is a the lets is a the lets" };
  for (int i = 0; i < texts.length; i++) {
    Document doc = new Document();
    Field field = new Field("field", texts[i], type);
    doc.add(field);
    writer.addDocument(doc);
  }
  CommonTermsQuery query = new CommonTermsQuery(Occur.MUST, Occur.SHOULD, 2);
  query.add(new Term("field", "text"));
  query.add(new Term("field", "long"));
  query.add(new Term("field", "very"));
 
  FastVectorHighlighter highlighter = new FastVectorHighlighter();
  IndexReader reader = DirectoryReader.open(writer);
  IndexSearcher searcher = newSearcher(reader);
  TopDocs hits = searcher.search(query, 10);
  assertEquals(2, hits.totalHits.value);
  FieldQuery fieldQuery  = highlighter.getFieldQuery(query, reader);
  String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader, 1, "field", 1000, 1);
  assertEquals("This piece of <b>text</b> refers to Kennedy at the beginning then has a longer piece of <b>text</b> that is <b>very</b> <b>long</b> in the middle and finally ends with another reference to Kennedy", bestFragments[0]);

  fieldQuery  = highlighter.getFieldQuery(query, reader);
  bestFragments = highlighter.getBestFragments(fieldQuery, reader, 0, "field", 1000, 1);
  assertEquals("Hello this is a piece of <b>text</b> that is <b>very</b> <b>long</b> and contains too much preamble and the meat is really here which says kennedy has been shot", bestFragments[0]);

  reader.close();
  writer.close();
  dir.close();
}
 
Example 11
Source File: CommonTermsQueryTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static Occur randomOccur(Random random) {
  return random.nextBoolean() ? Occur.MUST : Occur.SHOULD;
}
 
Example 12
Source File: TestLRUQueryCache.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static Query buildRandomQuery(int level) {
  if (level == 10) {
    // at most 10 levels
    return new MatchAllDocsQuery();
  }
  switch (random().nextInt(6)) {
    case 0:
      return new TermQuery(randomTerm());
    case 1:
      BooleanQuery.Builder bq = new BooleanQuery.Builder();
      final int numClauses = TestUtil.nextInt(random(), 1, 3);
      int numShould = 0;
      for (int i = 0; i < numClauses; ++i) {
        final Occur occur = RandomPicks.randomFrom(random(), Occur.values());
        bq.add(buildRandomQuery(level + 1), occur);
        if (occur == Occur.SHOULD) {
          numShould++;
        }
      }
      bq.setMinimumNumberShouldMatch(TestUtil.nextInt(random(), 0, numShould));
      return bq.build();
    case 2:
      Term t1 = randomTerm();
      Term t2 = randomTerm();
      PhraseQuery pq = new PhraseQuery(random().nextInt(2), t1.field(), t1.bytes(), t2.bytes());
      return pq;
    case 3:
      return new MatchAllDocsQuery();
    case 4:
      return new ConstantScoreQuery(buildRandomQuery(level + 1));
    case 5:
      List<Query> disjuncts = new ArrayList<>();
      final int numQueries = TestUtil.nextInt(random(), 1, 3);
      for (int i = 0; i < numQueries; ++i) {
        disjuncts.add(buildRandomQuery(level + 1));
      }
      return new DisjunctionMaxQuery(disjuncts, random().nextFloat());
    default:
      throw new AssertionError();
  }
}
 
Example 13
Source File: WebDSLFacet.java    From webdsl with Apache License 2.0 4 votes vote down vote up
public WebDSLFacet should(){
    occur = Occur.SHOULD;
    return this;
}
 
Example 14
Source File: LuceneQueryBuilder.java    From querqy with Apache License 2.0 4 votes vote down vote up
@Override
public LuceneQueryFactory<?> visit(final BooleanQuery booleanQuery) {

    BooleanQueryFactory bq = new BooleanQueryFactory(normalizeBooleanQueryBoost && parentType == ParentType.DMQ);
  
    ParentType myParentType = parentType;
    parentType = ParentType.BQ;

    clauseStack.add(bq);
    super.visit(booleanQuery);
    clauseStack.removeLast();

    parentType = myParentType;

    final Clause result;

    switch (bq.getNumberOfClauses()) {
        case 0:
            // no sub-query - this can happen if analysis filters out all tokens (stopwords)
            return new NeverMatchQueryFactory();
        case 1:
            final Clause firstClause = bq.getFirstClause();
            if (firstClause.occur == Occur.SHOULD) {
                // optimise and propagate the single clause up one level, but only
                // if occur equals neither MUST nor MUST_NOT, which would be lost on the
                // top level query
                result = bq.getFirstClause();
            } else {
                result = new Clause(bq, occur(booleanQuery.occur));
            }

            break;
        default:
            result = new Clause(bq, occur(booleanQuery.occur));
    }

    switch (parentType) {
        case BQ:
            if (!clauseStack.isEmpty()) {
                clauseStack.getLast().add(result);
                return bq;
            } else {// else we are the top BQ
                return result.queryFactory;
            }
        case DMQ:
            if (result.occur != Occur.SHOULD) {
                // create a wrapper query
                final BooleanQueryFactory wrapper = new BooleanQueryFactory(false);
                wrapper.add(result);
                bq = wrapper;
            }
            dmqStack.getLast().add(bq);
            return bq;

        default:
            throw new RuntimeException("Unknown parentType " + parentType);
    }
}
 
Example 15
Source File: SuperParserTest.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
public static BooleanClause bc(Query q) {
  return new BooleanClause(q, Occur.SHOULD);
}
 
Example 16
Source File: SolrPluginUtils.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Checks the number of optional clauses in the query, and compares it
 * with the specification string to determine the proper value to use.
 * <p>
 * If mmAutoRelax=true, we'll perform auto relaxation of mm if tokens
 * are removed from some but not all DisMax clauses, as can happen when
 * stopwords or punctuation tokens are removed in analysis.
 * </p>
 * <p>
 * Details about the specification format can be found
 * <a href="doc-files/min-should-match.html">here</a>
 * </p>
 *
 * <p>A few important notes...</p>
 * <ul>
 * <li>
 * If the calculations based on the specification determine that no
 * optional clauses are needed, BooleanQuerysetMinMumberShouldMatch
 * will never be called, but the usual rules about BooleanQueries
 * still apply at search time (a BooleanQuery containing no required
 * clauses must still match at least one optional clause)
 * <li>
 * <li>
 * No matter what number the calculation arrives at,
 * BooleanQuery.setMinShouldMatch() will never be called with a
 * value greater then the number of optional clauses (or less then 1)
 * </li>
 * </ul>
 *
 * <p>:TODO: should optimize the case where number is same
 * as clauses to just make them all "required"
 * </p>
 *
 * @param q The query as a BooleanQuery.Builder
 * @param spec The mm spec
 * @param mmAutoRelax whether to perform auto relaxation of mm if tokens are removed from some but not all DisMax clauses
 */
public static void setMinShouldMatch(BooleanQuery.Builder q, String spec, boolean mmAutoRelax) {

  int optionalClauses = 0;
  int maxDisjunctsSize = 0;
  int optionalDismaxClauses = 0;
  for (BooleanClause c : q.build().clauses()) {
    if (c.getOccur() == Occur.SHOULD) {
      if (mmAutoRelax && c.getQuery() instanceof DisjunctionMaxQuery) {
        int numDisjuncts = ((DisjunctionMaxQuery)c.getQuery()).getDisjuncts().size();
        if (numDisjuncts>maxDisjunctsSize) {
          maxDisjunctsSize = numDisjuncts;
          optionalDismaxClauses = 1;
        }
        else if (numDisjuncts == maxDisjunctsSize) {
          optionalDismaxClauses++;
        }
      } else {
        optionalClauses++;
      }
    }
  }

  int msm = calculateMinShouldMatch(optionalClauses + optionalDismaxClauses, spec);
  if (0 < msm) {
    q.setMinimumNumberShouldMatch(msm);
  }
}