示例#1
0
  synchronized void evictIfNecessary() {
    // under a lock to make sure that mostRecentlyUsedQueries and cache keep sync'ed
    if (requiresEviction()) {

      Iterator<Query> iterator = mostRecentlyUsedQueries.iterator();
      do {
        final Query query = iterator.next();
        final int size = mostRecentlyUsedQueries.size();
        iterator.remove();
        if (size == mostRecentlyUsedQueries.size()) {
          // size did not decrease, because the hash of the query changed since it has been
          // put into the cache
          throw new ConcurrentModificationException(
              "Removal from the cache failed! This "
                  + "is probably due to a query which has been modified after having been put into "
                  + " the cache or a badly implemented clone(). Query class: ["
                  + query.getClass()
                  + "], query: ["
                  + query
                  + "]");
        }
        onEviction(query);
      } while (iterator.hasNext() && requiresEviction());
    }
  }
示例#2
0
 private void testQuery(Expression<?> expr, String expectedQuery, int expectedHits)
     throws Exception {
   Query query = serializer.toQuery(expr, metadata);
   TopDocs docs = searcher.search(query, 100);
   assertEquals(expectedHits, docs.totalHits);
   assertEquals(expectedQuery, query.toString());
 }
 @Test
 public void testAnyEqArrayReference() throws Exception {
   Reference ref = createReference("d_array", new ArrayType(DataTypes.DOUBLE));
   Literal doubleLiteral = Literal.newLiteral(1.5d);
   Query query = convert(whereClause(AnyEqOperator.NAME, doubleLiteral, ref));
   assertThat(query.toString(), is("d_array:[1.5 TO 1.5]"));
 }
 public void assertQueryEqualsDOA(String query, Analyzer a, String result) throws Exception {
   Query q = getQueryDOA(query, a);
   String s = q.toString("field");
   if (!s.equals(result)) {
     fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result + "/");
   }
 }
 public void testFQ6() throws Exception {
   Query q =
       new FilteredQuery(
           new TermQuery(new Term(FIELD, "xx")), new ItemizedFilter(new int[] {1, 3}));
   q.setBoost(1000);
   qtest(q, new int[] {3});
 }
 /**
  * Prettyprint us.
  *
  * @param field the field to which we are applied
  * @return a string that shows what we do, of the form "(disjunct1 | disjunct2 | ... |
  *     disjunctn)^boost"
  */
 @Override
 public String toString(String field) {
   StringBuilder buffer = new StringBuilder();
   buffer.append("(");
   int numDisjunctions = disjuncts.size();
   for (int i = 0; i < numDisjunctions; i++) {
     Query subquery = disjuncts.get(i);
     if (subquery instanceof BooleanQuery) { // wrap sub-bools in parens
       buffer.append("(");
       buffer.append(subquery.toString(field));
       buffer.append(")");
     } else buffer.append(subquery.toString(field));
     if (i != numDisjunctions - 1) buffer.append(" | ");
   }
   buffer.append(")");
   if (tieBreakerMultiplier != 0.0f) {
     buffer.append("~");
     buffer.append(tieBreakerMultiplier);
   }
   if (getBoost() != 1.0) {
     buffer.append("^");
     buffer.append(getBoost());
   }
   return buffer.toString();
 }
示例#7
0
  public static void main(String[] args) {
    QsolParser parser = ParserFactory.getInstance(new QsolConfiguration()).getParser(false);

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String query = null;

    while (true) {
      System.out.print("Enter query: ");

      try {
        query = br.readLine();
      } catch (IOException ioe) {
        System.exit(1);
      }

      parser.markDateField("date");

      Query result = null;

      try {
        result = parser.parse("allFields", query, new StandardAnalyzer(Version.LUCENE_33));
      } catch (QsolSyntaxException e) {
        System.out.println("syntax exception:");
        e.printStackTrace();
      } catch (EmptyQueryException e) {
      }

      System.out.println("lucene query:" + result.toString());
    }
  }
 @Test
 public void testIdQuery() throws Exception {
   Reference ref = createReference("_id", DataTypes.STRING);
   Query query = convert(whereClause(EqOperator.NAME, ref, Literal.newLiteral("i1")));
   assertThat(query, instanceOf(TermQuery.class));
   assertThat(query.toString(), is("_uid:default#i1"));
 }
  public void testGetQueryWorksCorrectly() throws Exception {
    SecurityType securityType =
        (SecurityType) ManagerFactory.getPermissionTypeManager().getSchemeType("reporter");

    // Setup permissions so that a query is created
    GenericValue project = UtilsForTests.getTestEntity("Project", EasyMap.build("name", "Project"));
    PermissionSchemeManager permissionSchemeManager = ManagerFactory.getPermissionSchemeManager();
    GenericValue scheme = permissionSchemeManager.createScheme("Scheme", "scheme");
    permissionSchemeManager.addSchemeToProject(project, scheme);

    SchemeEntity schemeEntity =
        new SchemeEntity(securityType.getType(), null, new Long(Permissions.BROWSE));
    permissionSchemeManager.createSchemeEntity(scheme, schemeEntity);

    Query query = securityType.getQuery(u, project, null);
    assertEquals(
        "(+"
            + DocumentConstants.PROJECT_ID
            + ":"
            + project.getLong("id")
            + " +"
            + DocumentConstants.ISSUE_AUTHOR
            + ":owen)",
        query.toString(""));
  }
  public void testStaticMethod2Old() throws ParseException {
    String[] fields = {"b", "t"};
    // int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};
    BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT};
    MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());

    Query q =
        MultiFieldQueryParser.parse(
            "one",
            fields,
            flags,
            new StandardAnalyzer()); // , fields, flags, new StandardAnalyzer());
    assertEquals("+b:one -t:one", q.toString());

    q = MultiFieldQueryParser.parse("one two", fields, flags, new StandardAnalyzer());
    assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());

    try {
      BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
      q = MultiFieldQueryParser.parse("blah", fields, flags2, new StandardAnalyzer());
      fail();
    } catch (IllegalArgumentException e) {
      // expected exception, array length differs
    }
  }
示例#11
0
  private final void populateTerm(
      final HQuery hQuery, final boolean isShould, final boolean isMust, final Query subQueryL)
      throws FederatedSearchException {

    HTerm hTerm = new HTerm();
    hTerm.isShould = isShould;
    hTerm.isMust = isMust;
    hTerm.boost = subQueryL.getBoost();
    hQuery.terms.add(hTerm);

    if (subQueryL instanceof TermQuery) {
      TermQuery lTerm = (TermQuery) subQueryL;
      hTerm.type = lTerm.getTerm().field();
      hTerm.text = lTerm.getTerm().text();

    } else if (subQueryL instanceof FuzzyQuery) {
      FuzzyQuery lTerm = (FuzzyQuery) subQueryL;
      hTerm.isFuzzy = true;
      hTerm.type = lTerm.getTerm().field();
      hTerm.text = lTerm.getTerm().text();

    } else if (subQueryL instanceof TermRangeQuery) {
      TermRangeQuery lTerm = (TermRangeQuery) subQueryL;
      hTerm.isFuzzy = false;
      hTerm.type = lTerm.getField();
      hTerm.minRange = lTerm.getLowerTerm();
      hTerm.maxRange = lTerm.getUpperTerm();

    } else {
      throw new FederatedSearchException(
          "HQueryParser: Not Implemented Query :" + subQueryL.getClass().toString());
    }
  }
示例#12
0
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   Query query =
       innerRangeQuery(
           lowerTerm, upperTerm, includeLower, includeUpper, timeZone, forcedDateParser);
   return query.rewrite(reader);
 }
  public void testBoost() throws Exception {
    StandardAnalyzer oneStopAnalyzer =
        new StandardAnalyzer(TEST_VERSION_CURRENT, Collections.singleton("on"));
    AqpQueryParser qp = getParser();
    qp.setAnalyzer(oneStopAnalyzer);

    Query q = qp.parse("on^1.0", "field");
    assertNotNull(q);
    q = qp.parse("\"hello\"^2.0", "field");
    assertNotNull(q);
    assertEquals(q.getBoost(), (float) 2.0, (float) 0.5);
    q = qp.parse("hello^2.0", "field");
    assertNotNull(q);
    assertEquals(q.getBoost(), (float) 2.0, (float) 0.5);
    q = qp.parse("\"on\"^1.0", "field");
    assertNotNull(q);

    AqpQueryParser qp2 = AqpStandardLuceneParser.init();
    qp2.setAnalyzer(new StandardAnalyzer(TEST_VERSION_CURRENT));

    q = qp2.parse("the^3", "field");
    // "the" is a stop word so the result is an empty query:
    assertNotNull(q);
    assertEquals("", q.toString());
    assertEquals(1.0f, q.getBoost(), 0.01f);
  }
    @Override
    public Query rewrite(IndexReader reader) throws IOException {
      SolrRequestInfo info = SolrRequestInfo.getRequestInfo();

      CoreContainer container = info.getReq().getCore().getCoreDescriptor().getCoreContainer();

      final SolrCore fromCore = container.getCore(fromIndex);

      if (fromCore == null) {
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Cross-core join: no such core " + fromIndex);
      }
      RefCounted<SolrIndexSearcher> fromHolder = null;
      fromHolder = fromCore.getRegisteredSearcher();
      final Query joinQuery;
      try {
        joinQuery =
            JoinUtil.createJoinQuery(
                fromField, true, toField, fromQuery, fromHolder.get(), scoreMode);
      } finally {
        fromCore.close();
        fromHolder.decref();
      }
      return joinQuery.rewrite(reader);
    }
  @Test
  public void testQueryText() {

    Float boost = 0.7f;
    Object[] values = new Object[] {"houses", "cats"};

    Schema schema = schema().mapper("name", textMapper()).defaultAnalyzer("english").build();

    ContainsCondition condition = new ContainsCondition(boost, "name", values);
    Query query = condition.query(schema);
    assertNotNull("Query is not built", query);
    assertEquals("Query type is wrong", BooleanQuery.class, query.getClass());

    BooleanQuery booleanQuery = (BooleanQuery) query;
    assertEquals("Query boost is wrong", 0.7f, query.getBoost(), 0);
    BooleanClause[] clauses = booleanQuery.getClauses();
    assertEquals(
        "Query is wrong",
        "hous",
        ((TermQuery) clauses[0].getQuery()).getTerm().bytes().utf8ToString());
    assertEquals(
        "Query is wrong",
        "cat",
        ((TermQuery) clauses[1].getQuery()).getTerm().bytes().utf8ToString());
  }
示例#16
0
  /**
   * @param query
   * @param virtualWikiNames comma separated list of virtual wiki names
   * @param languages comma separated list of language codes to search in, may be null to search all
   *     languages
   */
  private Query buildQuery(String query, String virtualWikiNames, String languages)
      throws ParseException {
    // build a query like this: <user query string> AND <wikiNamesQuery> AND
    // <languageQuery>
    BooleanQuery bQuery = new BooleanQuery();
    Query parsedQuery = null;

    // for object search
    if (query.startsWith("PROP ")) {
      String property = query.substring(0, query.indexOf(":"));
      query = query.substring(query.indexOf(":") + 1, query.length());
      QueryParser qp = new QueryParser(Version.LUCENE_36, property, this.analyzer);
      parsedQuery = qp.parse(query);
      bQuery.add(parsedQuery, BooleanClause.Occur.MUST);
    } else if (query.startsWith("MULTI ")) {
      // for fulltext search
      List<String> fieldList = IndexUpdater.fields;
      String[] fields = fieldList.toArray(new String[fieldList.size()]);
      BooleanClause.Occur[] flags = new BooleanClause.Occur[fields.length];
      for (int i = 0; i < flags.length; i++) {
        flags[i] = BooleanClause.Occur.SHOULD;
      }
      parsedQuery =
          MultiFieldQueryParser.parse(Version.LUCENE_36, query, fields, flags, this.analyzer);
      bQuery.add(parsedQuery, BooleanClause.Occur.MUST);
    } else {
      String[] fields =
          new String[] {
            IndexFields.FULLTEXT,
            IndexFields.DOCUMENT_TITLE,
            IndexFields.DOCUMENT_NAME,
            IndexFields.FILENAME
          };
      BooleanClause.Occur[] flags = new BooleanClause.Occur[fields.length];
      for (int i = 0; i < flags.length; i++) {
        flags[i] = BooleanClause.Occur.SHOULD;
      }
      QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36, fields, this.analyzer);
      parsedQuery = parser.parse(query);
      // Since the sub-queries are OR-ed, each sub-query score is normally divided by the number of
      // sub-queries,
      // which would cause extra-small scores whenever there's a hit on only one sub-query;
      // compensate this by boosting the whole outer query
      parsedQuery.setBoost(fields.length);
      bQuery.add(parsedQuery, BooleanClause.Occur.MUST);
    }

    if (virtualWikiNames != null && virtualWikiNames.length() > 0) {
      bQuery.add(
          buildOredTermQuery(virtualWikiNames, IndexFields.DOCUMENT_WIKI),
          BooleanClause.Occur.MUST);
    }
    if (languages != null && languages.length() > 0) {
      bQuery.add(
          buildOredTermQuery(languages, IndexFields.DOCUMENT_LANGUAGE), BooleanClause.Occur.MUST);
    }

    return bQuery;
  }
示例#17
0
 public void assertQueryEquals(QueryParserWrapper qp, String field, String query, String result)
     throws Exception {
   Query q = qp.parse(query);
   String s = q.toString(field);
   if (!s.equals(result)) {
     fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result + "/");
   }
 }
 private String getRewrittenQuery(IndexSearcher searcher, Query query) throws IOException {
   Query queryRewrite = searcher.rewrite(query);
   if (queryRewrite instanceof MatchNoDocsQuery) {
     return query.toString();
   } else {
     return queryRewrite.toString();
   }
 }
 public int hashCode() {
   return mainQuery.hashCode()
       + reRankQuery.hashCode()
       + (int) reRankWeight
       + reRankDocs
       + (int) getBoost()
       + (scale ? 1 : 0);
 }
示例#20
0
 public void assertWildcardQueryEquals(String query, String result) throws Exception {
   QueryParserWrapper qp = getParser(null);
   Query q = qp.parse(query);
   String s = q.toString("field");
   if (!s.equals(result)) {
     fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /" + result + "/");
   }
 }
    @Test
    public void testConstantScorePrefixSubQueryCreation() {
        final AbstractIssuePickerSearchProvider.QueryCreator queryCreator = getConstantScorePrefixQueryCreator("web search", "summary");
        final Query query = queryCreator.getQuery();

        assertNotNull(query);
        assertEquals("summary:web* summary:search*", query.toString());
    }
示例#22
0
 public void testFQ6() throws Exception {
   Query q =
       new FilteredQuery(
           new TermQuery(new Term(FIELD, "xx")),
           new QueryWrapperFilter(matchTheseItems(new int[] {1, 3})));
   q.setBoost(1000);
   qtest(q, new int[] {3});
 }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
   final Query jq =
       JoinUtil.createJoinQuery(
           fromField, true, toField, fromQuery, info.getReq().getSearcher(), scoreMode);
   return jq.rewrite(reader);
 }
    @Test
    public void testWildCardQueryCreation()
    {
        final AbstractIssuePickerSearchProvider.QueryCreator queryCreator = getWildCardQueryCreator("web search", "key");
        final Query query = queryCreator.getQuery();

        assertNotNull(query);
        assertEquals("key:*web* key:*search*", query.toString());
    }
  @Test
  public void testDefaultBehavior() throws ParseException {
    String testInput = "Surely will every man walketh";
    Query query = parser.parse(testInput);

    // stemming on
    assertTrue(query.toString().indexOf(field + ":sure ") > -1);
    assertTrue(query.toString().indexOf(field + ":everi") > -1);
  }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   Query matchRewritten = match.rewrite(reader);
   Query contextRewritten = context.rewrite(reader);
   if (match != matchRewritten || context != contextRewritten) {
     return new BoostingQuery(matchRewritten, contextRewritten, boost);
   }
   return super.rewrite(reader);
 }
 @Override
 public Query clone() {
   ChildrenConstantScoreQuery q = (ChildrenConstantScoreQuery) super.clone();
   q.originalChildQuery = originalChildQuery.clone();
   if (q.rewrittenChildQuery != null) {
     q.rewrittenChildQuery = rewrittenChildQuery.clone();
   }
   return q;
 }
示例#28
0
 @Override
 public int hashCode() {
   int result = super.hashCode();
   result = 31 * result + joinField.hashCode();
   result = 31 * result + toQuery.hashCode();
   result = 31 * result + fromQuery.hashCode();
   result = 31 * result + indexReader.hashCode();
   return result;
 }
示例#29
0
 public Query rewrite(Query original) throws IOException {
   Query query = original;
   for (Query rewrittenQuery = query.rewrite(reader);
       rewrittenQuery != query;
       rewrittenQuery = query.rewrite(reader)) {
     query = rewrittenQuery;
   }
   return query;
 }
 /**
  * we rewrite against an empty indexreader: as we don't want things like rangeQueries that don't
  * summarize the document
  */
 private static Query rewrite(Query original) throws IOException {
   Query query = original;
   for (Query rewrittenQuery = query.rewrite(EMPTY_INDEXREADER);
       rewrittenQuery != query;
       rewrittenQuery = query.rewrite(EMPTY_INDEXREADER)) {
     query = rewrittenQuery;
   }
   return query;
 }