- 浏览: 44620 次
文章分类
最新评论
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("");
}
}
}
}
发表评论
-
扩展TokenFilter,实现二次分词
2011-09-01 16:00 1208经过paoding分词后,再对每个token进行2次分词,此处 ... -
solr的怪问题
2011-08-12 18:47 13使用solr3.1版本,继承paoding <fiel ... -
solr的unique-key的fieldtype必须为string类型
2011-08-09 18:51 3085今天发现了一个以前没有注意的问题,solr的schema.xm ... -
solr整合paoding
2011-07-04 18:03 14261. 下载 paoding ... -
Solr 使用自定义 Query Parser-2
2011-07-04 17:57 994Solr 使用自定义 Query Parser -
solr 使用自定义的 QueryParser
2011-07-04 17:51 1365如果想让 solr 有更加强 ... -
Solr Multicore 结合 Solr Distributed Searching 切分大索引来搜索
2011-05-06 14:21 857Solr Multicore 结合 Solr Dist ...
相关推荐
标题中的"solr ssm java"表明这是一个使用Java语言,结合Spring、SpringMVC和MyBatis(SSM)框架的项目,其中整合了Apache Solr搜索引擎。让我们深入了解一下这些技术及其相互作用。 **Solr**: ...
SolrJ是Apache Solr项目的Java客户端库,它为与Solr服务器进行交互提供了便利的API。这个压缩包包含了两个版本的SolrJ库:solr-solrj-4.10.3.jar和solr-solrj-5.0.0.jar。这两个版本的差异主要在于对Solr服务器的...
Solr-Solrj是Apache Lucene项目下的一个子项目,专门为Apache Solr搜索引擎提供Java客户端库。Solr是一款强大的全文检索服务器,而Solrj则是与之交互的Java API,使得开发人员能够轻松地在Java应用程序中集成Solr的...
solrj工具类封装,包括条件批量查询,批量增删改,分段修改。
SolrJ是Apache Solr官方提供的Java客户端库,它使得在Java应用程序中与Solr搜索引擎进行交互变得简单。Solr是一个高性能、可扩展的企业级全文搜索引擎,广泛用于内容索引和搜索。SolrJ提供了丰富的API,允许开发者...
SolrJ是Apache Solr官方提供的Java客户端库,它使得Java开发者能够轻松地与Solr搜索引擎进行通信,执行查询、索引文档等操作。SolrJ的使用是大数据环境中实现高效全文检索和数据分析的关键组件。在Java项目中,正确...
Solr-Solrj 5.0.0 是一个用于与Apache Solr进行交互的Java客户端库。在本文中,我们将深入探讨Solr-Solrj的使用、功能及其与自建Solr服务的集成,特别是涉及到中文分词的场景。 Apache Solr是一款流行的开源全文...
SolrJ 6.3.0 是一个针对 Apache Solr 的 Java 客户端库,它使得在 Java 应用程序中与 Solr 服务器进行交互变得更加便捷。Solr 是一个流行的开源搜索引擎,用于处理和索引大量文本数据,提供高效、可扩展的全文搜索...
SolrJ是Apache Solr官方提供的Java客户端库,它使得在Java应用程序中与Solr搜索引擎进行交互变得简单。这个压缩包文件包含了SolrJ运行所必需的一些关键库,包括JUnit测试框架、Commons IO和Commons Logging。接下来...
### Solr配置与SolrJ使用详解 #### 一、Solr基本安装与配置 **1. 下载Solr** - **步骤说明**: 从Apache官方镜像站点下载Solr 1.4.1版本。 - **操作详情**: 访问链接`http://apache.etoak.com/lucene/solr/`,...
solr-solrj-4.9.0.jar
SolrJ是Apache Solr官方提供的Java客户端库,它使得在Java应用程序中与Solr进行交互变得简单。这个“solrj使用教程”很可能是为了帮助开发者了解如何使用SolrJ来连接、查询和管理Solr索引。下面将详细介绍SolrJ的...
solr-solrj-4.10.3.jar。
solr-solrj-4.4.0.jar
solr-solrj-6.6.0.jar
solr详细配置教程与solrj的使用
solr部署在tomcat势必要设置用户权限,所以客户端solrj在创建连接的时候也需要用户名加密码
solrJ是Java连接solr进行查询检索和索引更新维护的jar包。
java大数据开发中solrJ所需要的核心包.................
主要讲解了 solr客户端如何调用带账号密码的solr服务器调用,实现添加索引和查询索引,以及分组查询