solrj+spellcheck

package com.doculibre.constellio.servlets;

import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.servlet.SolrRequestParsers;

/**
 * A simple main to illustrate how to execute a request using SolrJ
 * 
 */
public class SolrJExampleMain {

	private static final String myServer = "http://localhost:8080/constellio/app";

	private static final String myCollection = "test";

	// Can be set to 'on', 'off' or 'constellio' to include Constellio's facets
	private static final String facet = "constellio";

	// q=...
	private static final String query = "open source";

	private static final int start = 0;
	private static final int nbDocuments = 11;

	public static void main(String[] args) throws MalformedURLException,
			SolrServerException {

		// Prepare the SolrServer. Right now, the default SolrJ's ResponseParser
		// isn't supported by Constellio.
		CommonsHttpSolrServer server = new CommonsHttpSolrServer(myServer);
		server.setParser(new XMLResponseParser());

		// Do the same query three times using three different method
		System.out
				.println("= = = = = = = = = = = = = = = = = First way to execute a query = = = = = = = = = = = = = = = = =");
		print(doFirstQuery(server));
		System.out
				.println("= = = = = = = = = = = = = = = = = Second way to execute a query = = = = = = = = = = = = = = = = =");
		print(doSecondQuery(server));
		System.out
				.println("= = = = = = = = = = = = = = = = = Third way to execute query = = = = = = = = = = = = = = = = =");
		print(doThirdQuery(server));
		System.out
				.println("= = = = = = = = = = = = = = = = = Using SpellChecker = = = = = = = = = = = = = = = = =");
		print(spellCheck(server, "opn sorce source"));
	}

	/**
	 * Do the query using a StringBuffer
	 */
	public static QueryResponse doFirstQuery(SolrServer server)
			throws SolrServerException {
		StringBuffer request = new StringBuffer();
		request.append("collectionName=" + myCollection);
		request.append("&facet=" + facet);
		request.append("&q=" + query);
		request.append("&start=" + start);
		request.append("&rows=" + nbDocuments);
		SolrParams solrParams = SolrRequestParsers.parseQueryString(request
				.toString());

		return server.query(solrParams);
	}

	/**
	 * Do the query using a ModifiableSolrParams
	 */
	public static QueryResponse doSecondQuery(SolrServer server)
			throws SolrServerException {
		ModifiableSolrParams solrParams = new ModifiableSolrParams();
		solrParams.set("collectionName", myCollection);
		solrParams.set("facet", facet);
		solrParams.set("q", query);
		solrParams.set("start", start);
		solrParams.set("rows", nbDocuments);
		return server.query(solrParams);
	}

	/**
	 * Do the query using a SolrQuery
	 */
	public static QueryResponse doThirdQuery(SolrServer server)
			throws SolrServerException {
		SolrQuery solrQuery = new SolrQuery();
		solrQuery.setQuery(query);
		solrQuery.set("collectionName", myCollection);
		solrQuery.set("facet", facet);
		solrQuery.setStart(start);
		solrQuery.setRows(nbDocuments);
		return server.query(solrQuery);
	}

	/**
	 * Do the query using a SolrQuery
	 */
	public static QueryResponse spellCheck(SolrServer server, String badQuery)
			throws SolrServerException {
		SolrQuery solrQuery = new SolrQuery();
		solrQuery.setQuery(badQuery);
		solrQuery.set("collectionName", myCollection);

		// qt=spellcheck || qt=spellchecker
		solrQuery.setQueryType("spellcheck"); //根据solrconfig中的设置
		return server.query(solrQuery);
	}

	/**
	 * Print documents and facets
	 * 
	 * @param response
	 */
	@SuppressWarnings("unchecked")
	public static void print(QueryResponse response) {
		SolrDocumentList docs = response.getResults();
		if (docs != null) {
			System.out.println(docs.getNumFound() + " documents found, "
					+ docs.size() + " returned : ");
			for (int i = 0; i < docs.size(); i++) {
				SolrDocument doc = docs.get(i);
				System.out.println("\t" + doc.toString());
			}
		}

		List<FacetField> fieldFacets = response.getFacetFields();
		if (fieldFacets != null && fieldFacets.isEmpty()) {
			System.out.println("\nField Facets : ");
			for (FacetField fieldFacet : fieldFacets) {
				System.out.print("\t" + fieldFacet.getName() + " :\t");
				if (fieldFacet.getValueCount() > 0) {
					for (Count count : fieldFacet.getValues()) {
						System.out.print(count.getName() + "["
								+ count.getCount() + "]\t");
					}
				}
				System.out.println("");
			}
		}

		Map<String, Integer> queryFacets = response.getFacetQuery();
		if (queryFacets != null && !queryFacets.isEmpty()) {
			System.out.println("\nQuery facets : ");
			for (String queryFacet : queryFacets.keySet()) {
				System.out.println("\t" + queryFacet + "\t["
						+ queryFacets.get(queryFacet) + "]");
			}
			System.out.println("");
		}

		NamedList<NamedList<Object>> spellCheckResponse = (NamedList<NamedList<Object>>) response
				.getResponse().get("spellcheck");

		if (spellCheckResponse != null) {
			Iterator<Entry<String, NamedList<Object>>> wordsIterator = spellCheckResponse
					.iterator();

			while (wordsIterator.hasNext()) {
				Entry<String, NamedList<Object>> entry = wordsIterator.next();
				String word = entry.getKey();
				NamedList<Object> spellCheckWordResponse = entry.getValue();
				boolean correct = spellCheckWordResponse.get("frequency")
						.equals(1);
				System.out.println("Word: " + word + ",\tCorrect?: " + correct);
				NamedList<Integer> suggestions = (NamedList<Integer>) spellCheckWordResponse
						.get("suggestions");
				if (suggestions != null && suggestions.size() > 0) {
					System.out.println("Suggestions : ");
					Iterator<Entry<String, Integer>> suggestionsIterator = suggestions
							.iterator();
					while (suggestionsIterator.hasNext()) {
						System.out.println("\t"
								+ suggestionsIterator.next().getKey());
					}

				}
				System.out.println("");
			}

		}

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值