solr java api

	// 获取cloudsolrserver
	private CloudSolrServer getsearchclient(String corename) {
		CloudSolrServer server = null;
		// 从配置文件中读取
		PropertyHolder propertyHolder = new PropertyHolder("search");
		String ZKHOST = propertyHolder.getProperty("ZKHOST");
		String client_timeout = propertyHolder.getProperty("ZK_CLIENT_TIMEOUT");
		String connect_timeout = propertyHolder
				.getProperty("ZK_CONNECT_TIMEOUT");
		try {
			server = new CloudSolrServer(ZKHOST);
			server.connect();
			System.out.println("连接成功");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		server.setDefaultCollection(corename);
		server.setZkClientTimeout(Integer.parseInt(client_timeout));
		server.setZkConnectTimeout(Integer.parseInt(connect_timeout));

		return server;
	}

	// 分页查询
	private SolrQuery querypage() {
		SolrQuery query = new SolrQuery("*:*");
		query.set("start", 0);
		// rows 设为 (Integer.MAX_VALUE)则为查询所有数据
		query.set("rows", 30);
		return query;
	}

	// 条件查询
	private SolrQuery query() {
		SolrQuery query = new SolrQuery();
		// 查询不等于1
		// query.setQuery("id:(!1)");
		// 查询等于1
		// query.setQuery("id:1");
		// 查询id 等于1 或等于2(注意加空格)
		// query.setQuery("id:(1 || 2)");
		// 查询不等于1 且不等于10
		query.setQuery("id:(!1 && !10)");
		query.set("sort", "id asc");
		return query;
	}

	private SolrQuery querysunm() {
		SolrQuery query = new SolrQuery("*:*");
		// query.setQuery("")
		return query;
	}

	// 查询所有的索引
	private SolrQuery queryall() {

		SolrQuery query = new SolrQuery("*:*");
		query.set("sort", "id asc");
		return query;
	}

	// 聚合查询
	private SolrQuery groupquery() {
		// group by 查询必须加 *:*
		SolrQuery query = new SolrQuery("*:*");
		query.addFacetField("dept");
		// 查询分组后字段dept 前缀为网络营销的
		query.setFacetPrefix("网络营销");
		// query.setFacetSort(true);
		return query;
	}

	// 聚合查询输出
	private void groupprint(CloudSolrServer server, SolrQuery query,
			String field) {
		QueryResponse response;
		try {
			response = server.query(query);
			List<Count> factcount = response.getFacetField(field).getValues();
			for (Count count : factcount) {
				System.out.println(count);
			}
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 查询输出
	private void print(CloudSolrServer server, SolrQuery query) {
		QueryResponse response;
		try {
			response = server.query(query);
			SolrDocumentList list = response.getResults();
			for (SolrDocument solrDocument : list) {
				if (solrDocument.get("id") != null) {
					String dept = solrDocument.get("id").toString();
					System.out.println(dept);
				} else {
					continue;
				}
			}
		} catch (SolrServerException e) {
			e.printStackTrace();
		}

	}

	// 过滤查询
	private SolrQuery filterquery() {
		SolrQuery query = new SolrQuery("*:*");
		// 查询id大于等于1 并且小于等于 10 的索引
		query.set("fq", "id:[1 TO 10]");
		return query;
	}

你可能感兴趣的:(solr java api)