當前位置: 首頁>>代碼示例>>Java>>正文


Java BooleanClause.Occur方法代碼示例

本文整理匯總了Java中org.apache.lucene.search.BooleanClause.Occur方法的典型用法代碼示例。如果您正苦於以下問題:Java BooleanClause.Occur方法的具體用法?Java BooleanClause.Occur怎麽用?Java BooleanClause.Occur使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.lucene.search.BooleanClause的用法示例。


在下文中一共展示了BooleanClause.Occur方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testAnalyzeWildcard

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
public void testAnalyzeWildcard() {
    SimpleQueryParser.Settings settings = new SimpleQueryParser.Settings();
    settings.analyzeWildcard(true);
    Map<String, Float> weights = new HashMap<>();
    weights.put("field1", 1.0f);
    SimpleQueryParser parser = new MockSimpleQueryParser(new StandardAnalyzer(), weights, -1, settings);
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        parser.setDefaultOperator(defaultOp);
        Query query = parser.parse("first foo-bar-foobar* last");
        Query expectedQuery =
            new BooleanQuery.Builder()
                .add(new BooleanClause(new TermQuery(new Term("field1", "first")), defaultOp))
                .add(new BooleanQuery.Builder()
                    .add(new BooleanClause(new TermQuery(new Term("field1", "foo")), defaultOp))
                    .add(new BooleanClause(new TermQuery(new Term("field1", "bar")), defaultOp))
                    .add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")), defaultOp))
                    .build(), defaultOp)
                .add(new BooleanClause(new TermQuery(new Term("field1", "last")), defaultOp))
                .build();
        assertThat(query, equalTo(expectedQuery));
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:24,代碼來源:SimpleQueryParserTests.java

示例2: getModifierValue

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
private static BooleanClause.Occur getModifierValue(QueryNode node) {

    if (node instanceof ModifierQueryNode) {
      ModifierQueryNode mNode = ((ModifierQueryNode) node);
      Modifier modifier = mNode.getModifier();

      if (Modifier.MOD_NONE.equals(modifier)) {
        return BooleanClause.Occur.SHOULD;

      } else if (Modifier.MOD_NOT.equals(modifier)) {
        return BooleanClause.Occur.MUST_NOT;

      } else {
        return BooleanClause.Occur.MUST;
      }
    }

    return BooleanClause.Occur.SHOULD;

  }
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:21,代碼來源:StandardBooleanQueryNodeBuilder.java

示例3: parse

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
/**
 * @deprecated Use {@link #parse(String[], String[], BooleanClause.Occur[], Analyzer)}
 */
@Deprecated
public static Query parse(Version matchVersion, String[] queries, String[] fields, BooleanClause.Occur[] flags,
    Analyzer analyzer) throws ParseException
{
  if (!(queries.length == fields.length && queries.length == flags.length))
    throw new IllegalArgumentException("queries, fields, and flags array have have different length");
  BooleanQuery bQuery = new BooleanQuery();
  for (int i = 0; i < fields.length; i++)
  {
    QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
    Query q = qp.parse(queries[i]);
    if (q!=null && // q never null, just being defensive
        (!(q instanceof BooleanQuery) || ((BooleanQuery)q).getClauses().length>0)) {
      bQuery.add(q, flags[i]);
    }
  }
  return bQuery;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:22,代碼來源:MultiFieldQueryParser.java

示例4: getQuery

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
@Override
public Query getQuery(Element e) throws ParserException {
  BooleanQuery bq = new BooleanQuery(DOMUtils.getAttribute(e, "disableCoord", false));
  bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e, "minimumNumberShouldMatch", 0));
  bq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));

  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 = getOccursValue(clauseElem);

      Element clauseQuery = DOMUtils.getFirstChildOrFail(clauseElem);
      Query q = factory.getQuery(clauseQuery);
      bq.add(new BooleanClause(q, occurs));
    }
  }

  return bq;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:22,代碼來源:BooleanQueryBuilder.java

示例5: getOccursValue

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
static BooleanClause.Occur getOccursValue(Element clauseElem) throws ParserException {
  String occs = clauseElem.getAttribute("occurs");
  BooleanClause.Occur occurs = BooleanClause.Occur.SHOULD;
  if ("must".equalsIgnoreCase(occs)) {
    occurs = BooleanClause.Occur.MUST;
  } else {
    if ("mustNot".equalsIgnoreCase(occs)) {
      occurs = BooleanClause.Occur.MUST_NOT;
    } else {
      if (("should".equalsIgnoreCase(occs)) || ("".equals(occs))) {
        occurs = BooleanClause.Occur.SHOULD;
      } else {
        if (occs != null) {
          throw new ParserException("Invalid value for \"occurs\" attribute of clause:" + occs);
        }
      }
    }
  }
  return occurs;

}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:22,代碼來源:BooleanQueryBuilder.java

示例6: toBooleanQuery

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
private Query toBooleanQuery(SearchQuery.Boolean booleanQuery) {
  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();
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:20,代碼來源:LuceneQueryConverter.java

示例7: searchIndex

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
/**
 * 查詢索引
 * 
 * @param keywords
 * @return
 * @throws Exception
 */
public List<Document> searchIndex(Integer typeId, String keywords) throws Exception {
    // 1.init searcher
    Analyzer analyzer = new PaodingAnalyzer();
    IndexReader reader = IndexReader.open(typeId == appConfig.getGameTypeId() ? appConfig.getGameIndexDir()
            : appConfig.getSoftIndexDir());
    BooleanClause.Occur[] flags = new BooleanClause.Occur[] { BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.SHOULD };
    Query query = MultiFieldQueryParser.parse(keywords, appConfig.getQueryFields(), flags, analyzer);
    query = query.rewrite(reader);

    // 2.search
    List<Document> docs = new ArrayList<Document>();
    Hits hits = (typeId == appConfig.getGameTypeId() ? gameSearcher.search(query, Sort.RELEVANCE) : softSearcher
            .search(query, Sort.RELEVANCE));// searcher.search(query,
                                            // Sort.RELEVANCE);
    for (int i = 0; i < hits.length(); i++) {
        docs.add(hits.doc(i));
    }

    // 3.return
    reader.close();
    return docs;
}
 
開發者ID:zhaoxi1988,項目名稱:sjk,代碼行數:31,代碼來源:SearchServiceImpl.java

示例8: testAnalyzerWildcardWithSynonyms

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
public void testAnalyzerWildcardWithSynonyms() {
    SimpleQueryParser.Settings settings = new SimpleQueryParser.Settings();
    settings.analyzeWildcard(true);
    Map<String, Float> weights = new HashMap<>();
    weights.put("field1", 1.0f);
    SimpleQueryParser parser = new MockSimpleQueryParser(new MockRepeatAnalyzer(), weights, -1, settings);

    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        parser.setDefaultOperator(defaultOp);
        Query query = parser.parse("first foo-bar-foobar* last");

        Query expectedQuery = new BooleanQuery.Builder()
            .add(new BooleanClause(new SynonymQuery(new Term("field1", "first"),
                new Term("field1", "first")), defaultOp))
            .add(new BooleanQuery.Builder()
                .add(new BooleanClause(new SynonymQuery(new Term("field1", "foo"),
                    new Term("field1", "foo")), defaultOp))
                .add(new BooleanClause(new SynonymQuery(new Term("field1", "bar"),
                    new Term("field1", "bar")), defaultOp))
                .add(new BooleanQuery.Builder()
                    .add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")),
                        BooleanClause.Occur.SHOULD))
                    .add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")),
                        BooleanClause.Occur.SHOULD))
                    .setDisableCoord(true)
                    .build(), defaultOp)
                .build(), defaultOp)
            .add(new BooleanClause(new SynonymQuery(new Term("field1", "last"),
                new Term("field1", "last")), defaultOp))
            .build();
        assertThat(query, equalTo(expectedQuery));
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:35,代碼來源:SimpleQueryParserTests.java

示例9: testToQueryWildcarQuery

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
public void testToQueryWildcarQuery() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        MapperQueryParser queryParser = new MapperQueryParser(createShardContext());
        QueryParserSettings settings = new QueryParserSettings("first foo-bar-foobar* last");
        settings.defaultField(STRING_FIELD_NAME);
        settings.fieldsAndWeights(Collections.emptyMap());
        settings.analyzeWildcard(true);
        settings.fuzziness(Fuzziness.AUTO);
        settings.rewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
        settings.defaultOperator(op.toQueryParserOperator());
        queryParser.reset(settings);
        Query query = queryParser.parse("first foo-bar-foobar* last");
        Query expectedQuery =
            new BooleanQuery.Builder()
                .add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "first")), defaultOp))
                .add(new BooleanQuery.Builder()
                    .add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "foo")), defaultOp))
                    .add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "bar")), defaultOp))
                    .add(new BooleanClause(new PrefixQuery(new Term(STRING_FIELD_NAME, "foobar")), defaultOp))
                    .build(), defaultOp)
                .add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "last")), defaultOp))
                .build();
        assertThat(query, Matchers.equalTo(expectedQuery));
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:28,代碼來源:QueryStringQueryBuilderTests.java

示例10: testToQueryWilcardQueryWithSynonyms

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
public void testToQueryWilcardQueryWithSynonyms() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        MapperQueryParser queryParser = new MapperQueryParser(createShardContext());
        QueryParserSettings settings = new QueryParserSettings("first foo-bar-foobar* last");
        settings.defaultField(STRING_FIELD_NAME);
        settings.fieldsAndWeights(Collections.emptyMap());
        settings.analyzeWildcard(true);
        settings.fuzziness(Fuzziness.AUTO);
        settings.rewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
        settings.defaultOperator(op.toQueryParserOperator());
        settings.forceAnalyzer(new MockRepeatAnalyzer());
        queryParser.reset(settings);
        Query query = queryParser.parse("first foo-bar-foobar* last");

        Query expectedQuery = new BooleanQuery.Builder()
            .add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "first"),
                new Term(STRING_FIELD_NAME, "first")), defaultOp))
            .add(new BooleanQuery.Builder()
                .add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "foo"),
                    new Term(STRING_FIELD_NAME, "foo")), defaultOp))
                .add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "bar"),
                    new Term(STRING_FIELD_NAME, "bar")), defaultOp))
                .add(new BooleanQuery.Builder()
                    .add(new BooleanClause(new PrefixQuery(new Term(STRING_FIELD_NAME, "foobar")),
                        BooleanClause.Occur.SHOULD))
                    .add(new BooleanClause(new PrefixQuery(new Term(STRING_FIELD_NAME, "foobar")),
                        BooleanClause.Occur.SHOULD))
                    .setDisableCoord(true)
                    .build(), defaultOp)
                .build(), defaultOp)
            .add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "last"),
                new Term(STRING_FIELD_NAME, "last")), defaultOp))
            .build();
        assertThat(query, Matchers.equalTo(expectedQuery));
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:39,代碼來源:QueryStringQueryBuilderTests.java

示例11: getBooleanClauses

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
private static List<BooleanClause> getBooleanClauses(List<QueryBuilder> queryBuilders, BooleanClause.Occur occur, QueryShardContext context) throws IOException {
    List<BooleanClause> clauses = new ArrayList<>();
    for (QueryBuilder query : queryBuilders) {
        Query innerQuery = query.toQuery(context);
        if (innerQuery != null) {
            clauses.add(new BooleanClause(innerQuery, occur));
        }
    }
    return clauses;
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:11,代碼來源:BoolQueryBuilderTests.java

示例12: setDefaultOperator

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
/**
 * Sets the implicit operator setting, which must be
 * either {@code SHOULD} or {@code MUST}.
 */
public void setDefaultOperator(BooleanClause.Occur operator) {
  if (operator != BooleanClause.Occur.SHOULD && operator != BooleanClause.Occur.MUST) {
    throw new IllegalArgumentException("invalid operator: only SHOULD or MUST are allowed");
  }
  this.defaultOperator = operator;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:11,代碼來源:SimpleQueryParser.java

示例13: setOccur

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
public void setOccur(BooleanClause.Occur occur) {
    this.occur = occur;
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:4,代碼來源:MatchQuery.java

示例14: operator

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
public BooleanClause.Occur operator() {
    return operator;
}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:4,代碼來源:ParsedOptions.java

示例15: parse

import org.apache.lucene.search.BooleanClause; //導入方法依賴的package包/類
/**
 * Parses a query, searching on the fields specified. Use this if you need to
 * specify certain fields as required, and others as prohibited.
 * <p>
 * 
 * Usage:
 * <pre class="prettyprint">
 * <code>
 * String[] query = {&quot;query1&quot;, &quot;query2&quot;, &quot;query3&quot;};
 * String[] fields = {&quot;filename&quot;, &quot;contents&quot;, &quot;description&quot;};
 * BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
 *                BooleanClause.Occur.MUST,
 *                BooleanClause.Occur.MUST_NOT};
 * MultiFieldQueryParser.parse(query, fields, flags, analyzer);
 * </code>
 * </pre>
 *<p>
 * The code above would construct a query:
 * 
 * <pre>
 * <code>
 * (filename:query1) +(contents:query2) -(description:query3)
 * </code>
 * </pre>
 * 
 * @param queries
 *          Queries string to parse
 * @param fields
 *          Fields to search on
 * @param flags
 *          Flags describing the fields
 * @param analyzer
 *          Analyzer to use
 * @throws IllegalArgumentException
 *           if the length of the queries, fields, and flags array differ
 */
public static Query parse(String[] queries, String[] fields,
    BooleanClause.Occur[] flags, Analyzer analyzer) throws QueryNodeException {
  if (!(queries.length == fields.length && queries.length == flags.length))
    throw new IllegalArgumentException(
        "queries, fields, and flags array have have different length");
  BooleanQuery bQuery = new BooleanQuery();

  StandardQueryParser qp = new StandardQueryParser();
  qp.setAnalyzer(analyzer);

  for (int i = 0; i < fields.length; i++) {
    Query q = qp.parse(queries[i], fields[i]);

    if (q != null && // q never null, just being defensive
        (!(q instanceof BooleanQuery) || ((BooleanQuery) q).getClauses().length > 0)) {
      bQuery.add(q, flags[i]);
    }
  }
  return bQuery;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:57,代碼來源:QueryParserUtil.java


注:本文中的org.apache.lucene.search.BooleanClause.Occur方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。