Package org.apache.lucene.queryParser

Examples of org.apache.lucene.queryParser.QueryParser


   * @throws Exception
   */
  public void testParsingAndSearching() throws Exception {
    String field = "content";
    boolean dbg = false;
    QueryParser qp = new QueryParser(field, new WhitespaceAnalyzer());
    qp.setAllowLeadingWildcard(true);
    String docs[] = {
        "\\ abcdefg1",
        "\\79 hijklmn1",
        "\\\\ opqrstu1",
    };
    // queries that should find all docs
    String matchAll[] = {
        "*", "*1", "**1", "*?", "*?1", "?*1", "**", "***", "\\\\*"
    };
    // queries that should find no docs
    String matchNone[] = {
        "a*h", "a?h", "*a*h", "?a", "a?",
    };
    // queries that should be parsed to prefix queries
    String matchOneDocPrefix[][] = {
        {"a*", "ab*", "abc*", }, // these should find only doc 0
        {"h*", "hi*", "hij*", "\\\\7*"}, // these should find only doc 1
        {"o*", "op*", "opq*", "\\\\\\\\*"}, // these should find only doc 2
    };
    // queries that should be parsed to wildcard queries
    String matchOneDocWild[][] = {
        {"*a*", "*ab*", "*abc**", "ab*e*", "*g?", "*f?1", "abc**"}, // these should find only doc 0
        {"*h*", "*hi*", "*hij**", "hi*k*", "*n?", "*m?1", "hij**"}, // these should find only doc 1
        {"*o*", "*op*", "*opq**", "op*q*", "*u?", "*t?1", "opq**"}, // these should find only doc 2
    };

    // prepare the index
    RAMDirectory dir = new RAMDirectory();
    IndexWriter iw = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
    for (int i = 0; i < docs.length; i++) {
      Document doc = new Document();
      doc.add(new Field(field,docs[i],Store.NO,Index.ANALYZED));
      iw.addDocument(doc);
    }
    iw.close();
   
    IndexSearcher searcher = new IndexSearcher(dir);
   
    // test queries that must find all
    for (int i = 0; i < matchAll.length; i++) {
      String qtxt = matchAll[i];
      Query q = qp.parse(qtxt);
      if (dbg) System.out.println("matchAll: qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
      ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
      assertEquals(docs.length,hits.length);
    }
   
    // test queries that must find none
    for (int i = 0; i < matchNone.length; i++) {
      String qtxt = matchNone[i];
      Query q = qp.parse(qtxt);
      if (dbg) System.out.println("matchNone: qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
      ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
      assertEquals(0,hits.length);
    }

    // test queries that must be prefix queries and must find only one doc
    for (int i = 0; i < matchOneDocPrefix.length; i++) {
      for (int j = 0; j < matchOneDocPrefix[i].length; j++) {
        String qtxt = matchOneDocPrefix[i][j];
        Query q = qp.parse(qtxt);
        if (dbg) System.out.println("match 1 prefix: doc="+docs[i]+" qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
        assertEquals(PrefixQuery.class, q.getClass());
        ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
        assertEquals(1,hits.length);
        assertEquals(i,hits[0].doc);
      }
    }

    // test queries that must be wildcard queries and must find only one doc
    for (int i = 0; i < matchOneDocPrefix.length; i++) {
      for (int j = 0; j < matchOneDocWild[i].length; j++) {
        String qtxt = matchOneDocWild[i][j];
        Query q = qp.parse(qtxt);
        if (dbg) System.out.println("match 1 wild: doc="+docs[i]+" qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
        assertEquals(WildcardQuery.class, q.getClass());
        ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
        assertEquals(1,hits.length);
        assertEquals(i,hits[0].doc);
View Full Code Here


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

        throws Exception {
        Directory fsDir = FSDirectory.getDirectory(indexDir);
        IndexSearcher searcher = new IndexSearcher(fsDir);

        long start = new Date().getTime();
        QueryParser parser = new QueryParser("contents",new StandardAnalyzer());
        //System.err.println("searching for: '\"" + A + " " + B + "\"~"+MAX_PHRASE+"'");
        parser.setPhraseSlop(MAX_PHRASE);
        String my_phrase = "\"" + A + " " + B + "\"";
        Query query = parser.parse(my_phrase);
        //System.err.println("total hits: " + results.totalHits);

        //set similarity to use only the frequencies
        //score is based on frequency of phrase only
        searcher.setSimilarity(
View Full Code Here

            Query query;
            if (StringUtils.isEmpty(term)) {
                // If the search term is not null, all events should be displayed.
                query = new MatchAllDocsQuery();
            } else {
                QueryParser partialParser = new QueryParser(Version.LUCENE_30, "CONTENT", analyzer);
                query = partialParser.parse(term);
            }

            // TODO: なんか汚い...。
            Filter filter;
            if (beforeDeadlineOnly) {
View Full Code Here

  public final Query createQuery(Analyzer analyzer) throws ParseException {
    QueryParams params=configureQuery();
    String[] tokens=params.getToken();
    Query query=null;
    if( tokens.length==1 ) {
      QueryParser parser=new QueryParser(tokens[0],analyzer);
      query=parser.parse(params.getTextToSearch());
    } else {
      query=MultiFieldQueryParser.parse(params.getTextToSearch(),tokens,analyzer);
    }
    setQueryProperties(query);
    return query;
View Full Code Here

    System.out.println("Searching for: " + queryStr);
    if (searcher == null) {
      searcher = new IndexSearcher(directory, true);
    }
    Results result = new Results();
    QueryParser qp = new QueryParser(Version.LUCENE_36, "paragraph", new StandardAnalyzer(Version.LUCENE_36));
    Query query = qp.parse(queryStr);
    TopDocs topDocs = searcher.search(query, 20);
    System.out.println("Found " + topDocs.totalHits + " total hits.");
    for (int i = 0; i < topDocs.scoreDocs.length; i++) {
      Document theDoc = searcher.doc(topDocs.scoreDocs[i].doc);
      result.matches.add(theDoc);
View Full Code Here

    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), terms.length);
    for (int i = 0; i < topDocs.scoreDocs.length; i++){
        System.out.println("Id: " + topDocs.scoreDocs[i].doc + " Val: " + searcher.doc(topDocs.scoreDocs[i].doc).get("chars"));
    }
    QueryParser qp = new QueryParser(Version.LUCENE_36, "chars", analyzer);
    Query query = qp.parse(queryTerm);
    return searcher.search(query, n);
  }
View Full Code Here

  public final Query createQuery(Analyzer analyzer) throws ParseException {
    QueryParams params = configureQuery();
    String[] tokens = params.getToken();
    Query query = null;
    if( tokens.length==1 ) {
      QueryParser parser = new QueryParser(tokens[0], analyzer);
      query = parser.parse(params.getTextToSearch());
    } else {
      QueryParser parser = new MultiFieldQueryParser(tokens, analyzer);
      query = parser.parse(params.getTextToSearch());
    }
    setQueryProperties(query);
    return query;
  }
View Full Code Here

        String field = "fileContent";
        try {
            searcher = new IndexSearcher(FSDirectory.open(new File(Vars.Lucene_Repo)));
            // stdanalyzer = new StandardAnalyzer(Version.LUCENE_30);
            analyzer = new RussianAnalyzer(Version.LUCENE_40);
            parser = new QueryParser(Version.LUCENE_40, field, analyzer);

            query = parser.parse(line);
            //System.out.println("Searching for: " + query.toString(field));
            outMultiData.add(query.toString(field));
            outMultiData.add(doPagingSearch(searcher, query, hitsPerPage, raw, null == null));
View Full Code Here

    doc2.add(newField("field", "foo baz", Field.Index.ANALYZED));
    iw2.addDocument(doc2);
    IndexReader reader2 = iw2.getReader();
    iw2.close();
   
    QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockAnalyzer(random));
    qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
   
    MultiReader multireader = new MultiReader(reader1, reader2);
    IndexSearcher searcher = newSearcher(multireader);
    assertEquals(0, searcher.search(qp.parse("+foo -ba*"), 10).totalHits);
    searcher.close();
   
    final ExecutorService es = Executors.newCachedThreadPool();
    searcher = new IndexSearcher(multireader, es);
    if (VERBOSE)
      System.out.println("rewritten form: " + searcher.rewrite(qp.parse("+foo -ba*")));
    assertEquals(0, searcher.search(qp.parse("+foo -ba*"), 10).totalHits);
    es.shutdown();
    es.awaitTermination(1, TimeUnit.SECONDS);

    multireader.close();
    reader1.close();
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.