private QueryExpression create(Request request, ResourceDefinition resourceDefinition)
      throws InvalidQueryException {
    String queryString;
    if (request.getCardinality() == Request.Cardinality.INSTANCE) {
      String idPropertyName = resourceDefinition.getIdPropertyName();
      queryString =
          String.format("%s:%s", idPropertyName, request.<String>getProperty(idPropertyName));
    } else {
      queryString = request.getQueryString();
    }

    QueryExpression queryExpression;
    if (queryString != null && !queryString.isEmpty()) {
      QueryParser queryParser = new QueryParser(Version.LUCENE_48, "name", new KeywordAnalyzer());
      queryParser.setLowercaseExpandedTerms(false);
      queryParser.setAllowLeadingWildcard(true);
      Query query;
      try {
        query = queryParser.parse((String) escape(queryString));
      } catch (ParseException e) {
        throw new InvalidQueryException(e.getMessage());
      }
      LOG.info("LuceneQuery: " + query);
      queryExpression = create(query, resourceDefinition);
    } else {
      queryExpression = new AlwaysQueryExpression();
    }
    // add query properties to request so that they are returned
    request.addAdditionalSelectProperties(queryExpression.getProperties());
    return queryExpression;
  }
Exemple #2
0
  /**
   * give the id list of sentences, from Lucene index
   *
   * @param input input word
   * @param catalogName catalog (domain) name which we'd like to search in
   * @param limit how many hits are needed (0 means all)
   */
  public List<String> query(String input, String catalogName, int limit) {

    List<String> res = new ArrayList<String>();
    try {

      catalog c = catalogs.get(catalogName);
      IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(c.indexPath)));
      IndexSearcher searcher = new IndexSearcher(reader);

      QueryParser parser = new QueryParser("contents", analyzer);
      Query query = parser.parse(QueryParser.escape(input));

      int n = limit > 0 ? limit : searcher.count(query);
      if (n == 0) n = 1;
      TopDocs results = searcher.search(query, n);

      int endPos = limit;
      if (limit != 0) endPos = Math.min(results.totalHits, limit); // 1st n hits
      else endPos = results.totalHits; // all hits

      for (int i = 0; i < endPos; i++) {
        int id = results.scoreDocs[i].doc;
        Document doc = searcher.doc(id);
        res.add(doc.get("filename"));
      }
      reader.close();
      return res;

    } catch (ParseException e) {
      log(e.getMessage());
    } catch (IOException e) {
      log(e.getMessage());
    }
    return res;
  }
Exemple #3
0
  /** This function is only for test search. */
  public static List<String> searchQuery(
      String indexDir, String queryString, int numResults, CharArraySet stopwords) {
    String field = "contents";
    List<String> hitPaths = new ArrayList<String>();

    try {
      IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexDir)));
      IndexSearcher searcher = new IndexSearcher(reader);

      Analyzer analyzer = new MyAnalyzer(Version.LUCENE_44, stopwords);

      QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer);
      Query query;
      query = parser.parse(QueryParser.escape(queryString));

      TopDocs results = searcher.search(query, null, numResults);
      for (ScoreDoc hit : results.scoreDocs) {
        String path = searcher.doc(hit.doc).get("path");
        hitPaths.add(path.substring(0, path.length() - 4)); // chop off the file extension (".txt")
      }
    } catch (IOException e) {
      System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    } catch (ParseException e) {
      System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }

    return hitPaths;
  }
  /** This function is only for test search. */
  public static List<String> searchQuery(
      String indexDir, String queryString, int numResults, CharArraySet stopwords) {
    String field = "contents";
    List<String> hitPaths = new ArrayList<String>();

    try {
      IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexDir)));
      IndexSearcher searcher = new IndexSearcher(reader);

      // Used with mySimilarity.java for part E
      mySimilarity sim = new mySimilarity();
      searcher.setSimilarity(sim);

      // Used in part F
      // String field="Search_Field";
      // the second parameter calls the getAvgLength method and automatically calculates the
      // Average Length value
      // BM25Parameters.setAverageLength(field,getAvgLen(reader,field));
      // BM25Parameters.setB(0.75f);
      // BM25Parameters.setK1(2f);
      // BM25BooleanQuery query = new BM25BooleanQuery( "Cystic hydroma" ,field,analyzer);
      // System.out.println ("Searching for: " + query.toString(field));
      // TopDocs top=searcher.search(query, 10);
      // ScoreDoc[] docs = top.scoreDocs;
      // for (int i = 0; i < 10; i++){
      // System.out.println("the document with id= " + docs[i].doc + " has score ="+docs[i].score);
      // }

      Analyzer analyzer = new MyAnalyzer(Version.LUCENE_44, stopwords);

      QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer);
      Query query;
      query = parser.parse(QueryParser.escape(queryString));

      TopDocs results = searcher.search(query, null, numResults);
      for (ScoreDoc hit : results.scoreDocs) {
        String path = searcher.doc(hit.doc).get("path");
        hitPaths.add(path.substring(0, path.length() - 4)); // chop off the file extension (".txt")
      }
    } catch (IOException e) {
      System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    } catch (ParseException e) {
      System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }

    return hitPaths;
  }
  public String giveHelp(String[] words) {
    // TODO Add here for example credits, copyright, license

    if (words.length <= 1) {
      IntroHelp.print(stdout);
      return "";
    }

    if (!indexAvailable()) {
      return "";
    }

    Analyzer multiFieldAnalyzer = Onthology.multiFieldAnalyzer();

    try {
      String searchFields[] = {"index", "synopsis", "doc"};

      QueryParser parser = new MultiFieldQueryParser(searchFields, multiFieldAnalyzer);

      StringBuilder sb = new StringBuilder();
      for (int i = 1; i < words.length; i++) {
        sb.append(" ").append(escapeForQuery(words[i]));
      }
      Query query;
      try {
        query = parser.parse(sb.toString());
      } catch (ParseException e) {
        stderr.println("Cannot parse query: " + sb + ", " + e.getMessage());
        return "";
      }

      if (words[0].equals("help")) {
        return reportHelp(words, indexSearcher.search(query, maxSearch).scoreDocs);
      } else {
        return reportApropos(words, indexSearcher.search(query, maxSearch).scoreDocs);
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return "";
  }
 @Override
 public Query getQuery(Element e) throws ParserException {
   String text = DOMUtils.getText(e);
   try {
     Query q = null;
     if (unSafeParser != null) {
       // synchronize on unsafe parser
       synchronized (unSafeParser) {
         q = unSafeParser.parse(text);
       }
     } else {
       String fieldName = DOMUtils.getAttribute(e, "fieldName", defaultField);
       // Create new parser
       QueryParser parser = createQueryParser(fieldName, analyzer);
       q = parser.parse(text);
     }
     q.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
     return q;
   } catch (ParseException e1) {
     throw new ParserException(e1.getMessage());
   }
 }
 @ExceptionHandler({ParseException.class})
 public String parseExceptionHandler(ParseException pe) {
   return "ParseException: query parse exception!" + pe.getMessage();
 }