当前位置: 首页>>代码示例>>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;未经允许,请勿转载。