Esempio n. 1
0
  /**
   * 在所有的字段中搜索,如果是坐标的话,需要加上*,才能匹配到
   *
   * @param queryString 关键字
   * @throws Exception
   */
  public static void searchListIK(String queryString) throws Exception {
    // 查询的字符串:输入不存在的字符串是查询不到的,如:中国
    // 查询字段集合
    String[] queryFileds = {"name", "city", "address", "eastNew", "northNew", "geom", "phone"};
    IndexSearcher searcher = LuceneUtils.createIndexSearcher();
    // 这里使用的是IK的
    Query query = LuceneUtils.createQueryIK(queryFileds, queryString);
    // 在搜索器中进行查询
    // 对查询内容进行过滤
    Filter filter = null;
    // 一次在索引器查询多少条数据
    int queryCount = 100;

    TopDocs results = null;
    //	= searcher.search(query, filter, queryCount);
    //	System.out.println("总符合: " + results.totalHits + "条数!");

    ezfilter filter1 = new ezfilter();
    filter1.addFilter("datatype", "基站");
    filter1.addFilter("datatype", "网吧");

    query = filter1.getFilterQuery(query);

    results = searcher.search(query, filter, queryCount);
    System.out.println("总符合: " + results.totalHits + "条数!");

    // 显示记录
    for (ScoreDoc sr : results.scoreDocs) {
      // 文档编号
      int docID = sr.doc;
      // 真正的内容
      Document doc = searcher.doc(docID);
      System.out.println("name = " + doc.get("name"));
      System.out.println("address = " + doc.get("address"));
      System.out.println("eastNew = " + doc.get("eastNew"));
      System.out.println("northNew = " + doc.get("northNew"));
      System.out.println("geom = " + doc.get("geom"));
      System.out.println("phone = " + doc.get("phone"));
      System.out.println("datatype = " + doc.get("datatype"));
    }
  }