Package org.apache.lucene.queryParser

Examples of org.apache.lucene.queryParser.QueryParser


   * @throws ParseException
   */
  private void checkQuery(String queryStr) throws IOException, ParseException {
    // check result hit ranking
    if(VERBOSE) System.out.println("Query: " + queryStr);
      QueryParser queryParser = new QueryParser(TEST_VERSION_CURRENT, FIELD_NAME, new StandardAnalyzer(TEST_VERSION_CURRENT));
    Query query = queryParser.parse(queryStr);
    ScoreDoc[] multiSearcherHits = multiSearcher.search(query, null, 1000).scoreDocs;
    ScoreDoc[] singleSearcherHits = singleSearcher.search(query, null, 1000).scoreDocs;
    assertEquals(multiSearcherHits.length, singleSearcherHits.length);
    for (int i = 0; i < multiSearcherHits.length; i++) {
      Document docMulti = multiSearcher.doc(multiSearcherHits[i].doc);
View Full Code Here


  };

  protected void setUp() throws Exception {
    analyzerW = new WhitespaceAnalyzer();
    analyzerB = new BigramAnalyzer();
    paW = new QueryParser( F, analyzerW );
    paB = new QueryParser( F, analyzerB );
    dir = new RAMDirectory();
  }
View Full Code Here

    iwriter.close();
   
    // Now search the index:
    IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true
    // Parse a simple query that searches for "text":
    QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, "fieldname", analyzer);
    Query query = parser.parse("text");
    TopDocs hits = isearcher.search(query, null, 1);
    assertEquals(1, hits.totalHits);
    // Iterate through the results:
    for (int i = 0; i < hits.scoreDocs.length; i++) {
      Document hitDoc = isearcher.doc(hits.scoreDocs[i].doc);
      assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
    }

    // Test simple phrase query
    query = parser.parse("\"to be\"");
    assertEquals(1, isearcher.search(query, null, 1).totalHits);

    isearcher.close();
    directory.close();
  }
View Full Code Here

    super(arg0);
  }

  public void testQueryScorerHits() throws Exception {
    Analyzer analyzer = new SimpleAnalyzer();
    QueryParser qp = new QueryParser(FIELD_NAME, analyzer);
    query = qp.parse("\"very long\"");
    searcher = new IndexSearcher(ramDir, false);
    TopDocs hits = searcher.search(query, 10);
   
    QueryScorer scorer = new QueryScorer(query, FIELD_NAME);
    Highlighter highlighter = new Highlighter(scorer);
View Full Code Here

 
  public void testHighlightingWithDefaultField() throws Exception {

    String s1 = "I call our world Flatland, not because we call it so,";

    QueryParser parser = new QueryParser(FIELD_NAME, new StandardAnalyzer(Version.LUCENE_CURRENT));

    // Verify that a query against the default field results in text being
    // highlighted
    // regardless of the field name.
    Query q = parser.parse("\"world Flatland\"~3");
    String expected = "I call our <B>world</B> <B>Flatland</B>, not because we call it so,";
    String observed = highlightField(q, "SOME_FIELD_NAME", s1);
    System.out.println("Expected: \"" + expected + "\n" + "Observed: \"" + observed);
    assertEquals("Query in the default field results in text for *ANY* field being highlighted",
        expected, observed);

    // Verify that a query against a named field does not result in any
    // highlighting
    // when the query field name differs from the name of the field being
    // highlighted,
    // which in this example happens to be the default field name.
    q = parser.parse("text:\"world Flatland\"~3");
    expected = s1;
    observed = highlightField(q, FIELD_NAME, s1);
    System.out.println("Expected: \"" + expected + "\n" + "Observed: \"" + observed);
    assertEquals(
        "Query in a named field does not result in highlighting when that field isn't in the query",
View Full Code Here

    String f1c = f1 + ":";
    String f2c = f2 + ":";
    String q = "(" + f1c + ph1 + " OR " + f2c + ph1 + ") AND (" + f1c + ph2
        + " OR " + f2c + ph2 + ")";
    Analyzer analyzer = new WhitespaceAnalyzer();
    QueryParser qp = new QueryParser(f1, analyzer);
    Query query = qp.parse(q);

    QueryScorer scorer = new QueryScorer(query, f1);
    scorer.setExpandMultiTermQuery(false);

    Highlighter h = new Highlighter(this, scorer);
View Full Code Here

        String queryString = FIELD_NAME + ":[kannedy TO kznnedy]";

        // Need to explicitly set the QueryParser property to use TermRangeQuery
        // rather
        // than RangeFilters
        QueryParser parser = new QueryParser(FIELD_NAME, new StandardAnalyzer());
        parser.setUseOldRangeQuery(true);
        query = parser.parse(queryString);
        doSearching(query);

        doStandardHighlights(analyzer, hits, query, HighlighterTest.this);
        assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
            numHighlights == 5);
View Full Code Here

        synonyms.put("football", "soccer,footie");
        Analyzer analyzer = new SynonymAnalyzer(synonyms);
        String srchkey = "football";

        String s = "football-soccer in the euro 2004 footie competition";
        QueryParser parser = new QueryParser("bookid", analyzer);
        Query query = parser.parse(srchkey);

        TokenStream tokenStream = analyzer.tokenStream(null, new StringReader(s));

        Highlighter highlighter = getHighlighter(query, null, tokenStream, HighlighterTest.this);
View Full Code Here

        numHighlights = 0;
        // test to show how rewritten query can still be used
        searcher = new IndexSearcher(ramDir);
        Analyzer analyzer = new StandardAnalyzer();

        QueryParser parser = new QueryParser(FIELD_NAME, analyzer);
        Query query = parser.parse("JF? or Kenned*");
        System.out.println("Searching with primitive query");
        // forget to set this and...
        // query=query.rewrite(reader);
        Hits hits = searcher.search(query);

View Full Code Here

    IndexSearcher searchers[] = new IndexSearcher[2];
    searchers[0] = new IndexSearcher(ramDir1);
    searchers[1] = new IndexSearcher(ramDir2);
    MultiSearcher multiSearcher = new MultiSearcher(searchers);
    QueryParser parser = new QueryParser(FIELD_NAME, new StandardAnalyzer());
    parser.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
    query = parser.parse("multi*");
    System.out.println("Searching for: " + query.toString(FIELD_NAME));
    // at this point the multisearcher calls combine(query[])
    hits = multiSearcher.search(query);

    // query = QueryParser.parse("multi*", FIELD_NAME, new StandardAnalyzer());
View Full Code Here

TOP

Related Classes of org.apache.lucene.queryParser.QueryParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.