public void testBoostsSimple() throws Exception {
    Map boosts = new HashMap();
    boosts.put("b", new Float(5));
    boosts.put("t", new Float(10));
    String[] fields = {"b", "t"};
    MultiFieldQueryParser mfqp = new MultiFieldQueryParser(fields, new StandardAnalyzer(), boosts);

    // Check for simple
    Query q = mfqp.parse("one");
    assertEquals("b:one^5.0 t:one^10.0", q.toString());

    // Check for AND
    q = mfqp.parse("one AND two");
    assertEquals("+(b:one^5.0 t:one^10.0) +(b:two^5.0 t:two^10.0)", q.toString());

    // Check for OR
    q = mfqp.parse("one OR two");
    assertEquals("(b:one^5.0 t:one^10.0) (b:two^5.0 t:two^10.0)", q.toString());

    // Check for AND and a field
    q = mfqp.parse("one AND two AND foo:test");
    assertEquals("+(b:one^5.0 t:one^10.0) +(b:two^5.0 t:two^10.0) +foo:test", q.toString());

    q = mfqp.parse("one^3 AND two^4");
    assertEquals("+((b:one^5.0 t:one^10.0)^3.0) +((b:two^5.0 t:two^10.0)^4.0)", q.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
    }
  }
 /**
  * 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();
 }
 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 String toString(String s) {
   return "{!rerank mainQuery='"
       + mainQuery.toString()
       + "' reRankQuery='"
       + reRankQuery.toString()
       + "' reRankDocs="
       + reRankDocs
       + " reRankWeigh="
       + reRankWeight
       + "}";
 }
  // verify parsing of query using a stopping analyzer
  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());
  }
 public void testAnalyzerReturningNull() throws ParseException {
   String[] fields = new String[] {"f1", "f2", "f3"};
   MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new AnalyzerReturningNull());
   Query q = parser.parse("bla AND blo");
   assertEquals("+(f2:bla f3:bla) +(f2:blo f3:blo)", q.toString());
   // the following queries are not affected as their terms are not analyzed anyway:
   q = parser.parse("bla*");
   assertEquals("f1:bla* f2:bla* f3:bla*", q.toString());
   q = parser.parse("bla~");
   assertEquals("f1:bla~0.5 f2:bla~0.5 f3:bla~0.5", q.toString());
   q = parser.parse("[a TO c]");
   assertEquals("f1:[a TO c] f2:[a TO c] f3:[a TO c]", q.toString());
 }
 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 + "/");
   }
 }
 @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 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);
  }
Example #12
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 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]"));
 }
 @Override
 public String toString(String field) {
   StringBuilder sb = new StringBuilder();
   sb.append("boost(").append(q.toString(field)).append(',').append(boostVal).append(')');
   sb.append(ToStringUtils.boost(getBoost()));
   return sb.toString();
 }
  @Test
  public void testCustomStopWords() throws Exception {
    myAnalyzer = new EnglishLuceneAnalyzer();
    // set custom stop word
    myAnalyzer.setDoStopWords(true);
    String[] stopWords = {"thy", "ye", "unto", "shalt"};
    myAnalyzer.setStopWords(new CharArraySet(Arrays.asList(stopWords), false));
    parser = new QueryParser(Version.LUCENE_29, field, myAnalyzer);
    String testInput = "Upon thy belly Shalt thou go";
    Query query = parser.parse(testInput);
    // System.out.println("ParsedQuery- "+ query.toString());

    assertTrue(query.toString().indexOf(field + ":shalt") == -1);
    assertTrue(query.toString().indexOf(field + ":thy") == -1);
    assertTrue(query.toString().indexOf(field + ":upon") > -1);
  }
Example #16
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());
 }
 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 + "/");
   }
 }
 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());
    }
 @Override
 public String toString(String field) {
   return new StringBuilder("ConstantScore(")
       .append((query == null) ? filter.toString() : query.toString(field))
       .append(')')
       .append(ToStringUtils.boost(getBoost()))
       .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);
  }
    @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(groups = "ch12")
  public void vectorTest() throws Exception {
    FullTextSession session = Search.getFullTextSession(openSession());
    Transaction tx = session.beginTransaction();
    buildIndex(session, tx);

    try {
      tx = session.beginTransaction();

      Query query = new TermQuery(new Term("content", "properties"));
      System.out.println(query.toString());

      FullTextQuery hibQuery = session.createFullTextQuery(query, ElectricalProperties.class);
      hibQuery.setProjection(
          FullTextQuery.DOCUMENT, FullTextQuery.DOCUMENT_ID, FullTextQuery.SCORE);

      reader = getReader(session);

      List<Object[]> results = hibQuery.list();

      assert results.size() > 0 : "no results returned";
      for (int x = 0; x < results.size(); x++) {

        Integer docId = (Integer) results.get(x)[1];
        TermPositionVector vector = (TermPositionVector) reader.getTermFreqVector(docId, "content");
        String[] terms = vector.getTerms();
        int[] f = vector.getTermFrequencies();

        System.out.println(results.get(x)[2]);
        for (int y = 0; y < vector.size(); y++) {
          System.out.print("docID# =>" + docId);
          System.out.print(" term => " + terms[y]);
          System.out.print(" freq => " + f[y]);

          int[] positions = vector.getTermPositions(y);
          TermVectorOffsetInfo[] offsets = vector.getOffsets(y);
          for (int z = 0; z < positions.length; z++) {
            System.out.print(" position => " + positions[z]);
            System.out.print(" starting offset => " + offsets[z].getStartOffset());
            System.out.println(" ending offset => " + offsets[z].getEndOffset());
          }
          System.out.println("---------------");
        }
      }
      for (Object element :
          session.createQuery("from " + ElectricalProperties.class.getName()).list())
        session.delete(element);

      tx.commit();
    } finally {
      session.close();
      if (provider != null) {
        provider.closeReader(reader);
      }
    }
  }
  public void testPerFieldAnalyzer() throws Exception {
    PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer_());
    analyzer.addAnalyzer("partnum", new KeywordAnalyzer());

    Query query =
        new QueryParser(Version.LUCENE_30, "description", analyzer).parse("partnum:Q36 AND SPACE");

    assertEquals("Q36 kept as-is", "+partnum:Q36 +space", query.toString("description"));
    assertEquals("doc found!", 1, TestUtil.hitCount(searcher, query));
  }
 public void testBasicQueryParser() throws Exception {
   Query query =
       new QueryParser(
               Version.LUCENE_30, // 1
               "description", // 1
               new SimpleAnalyzer_()) // 1
           .parse("partnum:Q36 AND SPACE"); // 1
   assertEquals("note Q36 -> q", "+partnum:q +space", query.toString("description")); // 2
   assertEquals("doc not found :(", 0, TestUtil.hitCount(searcher, query));
 }
  @Test
  public void testCreateRelaxedQueryMulti() throws Exception {

    final Query query =
        new KeywordProductSearchQueryBuilder().createRelaxedQuery(10L, "query", "Search, Word");
    assertNotNull(query);
    assertEquals(
        "(name:search~0.5^2.5 displayName:search~0.5^3.0 brand:search~0.7^3.5 code:search~0.7^4.0 manufacturerCode:search~0.7^4.0 sku.code:search~0.7^4.0 sku.manufacturerCode:search~0.7^4.0 code_stem:search~0.75 manufacturerCode_stem:search~0.75 sku.code_stem:search~0.75 sku.manufacturerCode_stem:search~0.75 attribute.attrvalsearch:search~0.65^2.75 description_stem:search^0.5) (name:word~0.5^2.5 displayName:word~0.5^3.0 brand:word~0.7^3.5 code:word~0.7^4.0 manufacturerCode:word~0.7^4.0 sku.code:word~0.7^4.0 sku.manufacturerCode:word~0.7^4.0 code_stem:word~0.75 manufacturerCode_stem:word~0.75 sku.code_stem:word~0.75 sku.manufacturerCode_stem:word~0.75 attribute.attrvalsearch:word~0.65^2.75 description_stem:word^0.5)",
        query.toString());
  }
  @Test
  public void testCreateStrictQueryMulti() throws Exception {

    final Query query =
        new KeywordProductSearchQueryBuilder().createStrictQuery(10L, "query", "Search, Word");
    assertNotNull(query);
    assertEquals(
        "(name:Search, Word~0.6^2.5 displayName:Search, Word~0.6^4.0 brand:search, word~0.8^5.0 code:Search, Word~0.8^10.0 manufacturerCode:Search, Word~0.8^10.0 sku.code:Search, Word~0.8^10.0 sku.manufacturerCode:Search, Word~0.8^10.0 attribute.attrvalsearch:Search, Word~0.65^3.5) (name:search~0.6^2.5 displayName:search~0.6^3.0 brand:search~0.8^3.5 code:search~0.8^4.0 manufacturerCode:search~0.8^4.0 sku.code:search~0.8^4.0 sku.manufacturerCode:search~0.8^4.0 attribute.attrvalsearch:search~0.65^2.75) (name:word~0.6^2.5 displayName:word~0.6^3.0 brand:word~0.8^3.5 code:word~0.8^4.0 manufacturerCode:word~0.8^4.0 sku.code:word~0.8^4.0 sku.manufacturerCode:word~0.8^4.0 attribute.attrvalsearch:word~0.65^2.75)",
        query.toString());
  }
 @Override
 public String toString(String field) {
   // TODO verify this looks good
   return getClass().getSimpleName()
       + "("
       + indexQuery.toString(field)
       + ", "
       + predicateValueSource
       + ")";
 }
 /* (non-Javadoc) @see org.apache.lucene.search.Query#toString(java.lang.String) */
 @Override
 public String toString(String field) {
   StringBuilder sb = new StringBuilder(name()).append("(");
   sb.append(subQuery.toString(field));
   for (int i = 0; i < valSrcQueries.length; i++) {
     sb.append(", ").append(valSrcQueries[i].toString(field));
   }
   sb.append(")");
   sb.append(strict ? " STRICT" : "");
   return sb.toString() + ToStringUtils.boost(getBoost());
 }
 /**
  * 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("(");
   for (int i = 0; i < disjuncts.length; i++) {
     Query subquery = disjuncts[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 != disjuncts.length - 1) buffer.append(" | ");
   }
   buffer.append(")");
   if (tieBreakerMultiplier != 0.0f) {
     buffer.append("~");
     buffer.append(tieBreakerMultiplier);
   }
   return buffer.toString();
 }