FILTER QUERY(fq):过滤查询,查询的内容会被缓存。
sort:排序
fl:字段列表
df:默认查询字段(text)
只要在text这个查询字段中,都可以查询出来相关的字段。
copyfile:
若直接写字段而不加字段类型的话,默认从text中取相关的字段。
indent :是否格式化
debugQuery:调试查询
hl:高亮
facet:分组统计类似于sql中的group by
spell:拼写检查
往solr里面添加数据时是如何建立索引的呢?
查询的时候如何对数据进行解析的呢?
第一次尝试敲的可运行的solr的java代码:
package cn.crxy.solr_8;
import java.util.Collection;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;
public class SolrTest {
@Test
public void test1() throws Exception {
String baseURL = "http://192.168.8.110:8983/solr";
HttpSolrServer solrServer = new HttpSolrServer(baseURL);
SolrQuery params = new SolrQuery();
params.set("q", "*:*");
QueryResponse response = solrServer.query(params);
SolrDocumentList results = response.getResults();
for (SolrDocument solrDocument : results) {
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
System.out.println(field+"--"+solrDocument.get(field));
}
System.out.println("============================================");
}
}
}
第二次尝试敲的可运行的solr的java代码:(基于第一次修改的加了一个numfound:一共多少条数据):
package cn.crxy.solr_8;
import java.util.Collection;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;
public class SolrTest {
@Test
public void test1() throws Exception {
String baseURL = "http://192.168.x.xxx:8983/solr";
HttpSolrServer solrServer = new HttpSolrServer(baseURL);
SolrQuery params = new SolrQuery();
params.set("q", "*:*");
QueryResponse response = solrServer.query(params);
SolrDocumentList results = response.getResults();
//获取数据总条数
long numFound = results.getNumFound();
System.out.println("一共:"+numFound);
for (SolrDocument solrDocument : results) {
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
System.out.println(field+"--"+solrDocument.get(field));
}
System.out.println("============================================");
}
}
}
第三次尝试敲的可运行的solr的java代码:(基于第二次修改的 ):
package cn.crxy.solr_8;
import java.util.Collection;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;
public class SolrTest {
@Test
public void test1() throws Exception {
String baseURL = "http://192.168.x.xxx:8983/solr";
HttpSolrServer solrServer = new HttpSolrServer(baseURL);
SolrQuery params = new SolrQuery();
params.set("q", "*:*");
QueryResponse response = solrServer.query(params);
SolrDocumentList results = response.getResults();
//获取数据总条数
//long numFound = results.getNumFound();
long numFound = results.size();
System.out.println("一共:"+numFound);
for (SolrDocument solrDocument : results) {
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
System.out.println(field+"--"+solrDocument.get(field));
}
System.out.println("============================================");
}
}
}
这种方法是不行的,这样的话结果出来是10,而本来的结果是32,它这个10表示的是你现在在这个solrDocument
里面封装的数据的条数是10条,而实际上是有32 个. 因为在solrweb界面的start,rows里面已经限定了每次只取10
条.这个相当于如果是做分页的话,相当于每一页是10条.通过SolrDocumentList只是获取每一页的数据,
什么时候做的分页?
start,rows是默认的查询前10 条
第四次修改(基于第三次加了一个collection1 指定了core)可以正常运行
package cn.crxy.solr_8;
import java.util.Collection;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;
public class SolrTest {
@Test
public void test1() throws Exception {
String baseURL = "http://192.168.x.xxx:8983/solr/collection1";
HttpSolrServer solrServer = new HttpSolrServer(baseURL);
SolrQuery params = new SolrQuery();
params.set("q", "*:*");
QueryResponse response = solrServer.query(params);
SolrDocumentList results = response.getResults();
//获取数据总条数
//long numFound = results.getNumFound();
long numFound = results.size();
System.out.println("一共:"+numFound);
for (SolrDocument solrDocument : results) {
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
System.out.println(field+"--"+solrDocument.get(field));
}
System.out.println("============================================");
}
}
}
经过测试发现collection1中可以 检索出来32条数据(collection1中含有32条数据) .
改为collection2后检索出来0条数据,(colletction2中含有0条数据).
第五次修改 加入索引(加入索引有两种方法)
package cn.crxy.solr_8;
import static org.junit.Assert.*;
import java.util.Collection;
import java.util.Iterator;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
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.SolrInputDocument;
import org.junit.Test;
public class SolrTest {
String baseURL = "http://192.168.x.xxx:8983/solr/collection2";
HttpSolrServer solrServer = new HttpSolrServer(baseURL);
/**
* 查询
* @throws Exception
*/
@Test
public void test1() throws Exception {
SolrQuery params = new SolrQuery();
params.set("q", "*:*");
QueryResponse response = solrServer.query(params);
SolrDocumentList results = response.getResults();
//获取数据总条数
//long numFound = results.getNumFound();
long numFound = results.size();
System.out.println("一共:"+numFound);
for (SolrDocument solrDocument : results) {
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
System.out.println(field+"--"+solrDocument.get(field));
}
System.out.println("============================================");
}
}
/**
*建立索引1
* @throws Exception
*/
@Test
public void test2() throws Exception {
SolrInputDocument docs = new SolrInputDocument();
docs.setField("id", "1");
docs.setField("name", "crxy");
solrServer.add(docs);
}
//封装一个对象建立一个索引
/**
* 建立索引2
* @throws Exception
*/
@Test
public void test3() throws Exception {
Person person = new Person();
person.setId("2");
person.setName("zs");
person.setAge("12");
solrServer.addBean(person);
}
}
第六次修改(删除索引)
package cn.crxy.solr_8;
import static org.junit.Assert.*;
import java.util.Collection;
import java.util.Iterator;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
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.SolrInputDocument;
import org.junit.Test;
public class SolrTest {
String baseURL = "http://192.168.x.xxx:8983/solr/collection2";
HttpSolrServer solrServer = new HttpSolrServer(baseURL);
/**
* 查询
* @throws Exception
*/
@Test
public void test1() throws Exception {
SolrQuery params = new SolrQuery();
params.set("q", "*:*");
QueryResponse response = solrServer.query(params);
SolrDocumentList results = response.getResults();
//获取数据总条数
//long numFound = results.getNumFound();
long numFound = results.size();
System.out.println("一共:"+numFound);
for (SolrDocument solrDocument : results) {
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
System.out.println(field+"--"+solrDocument.get(field));
}
System.out.println("============================================");
}
}
/**
*建立索引1
* @throws Exception
*/
@Test
public void test2() throws Exception {
SolrInputDocument docs = new SolrInputDocument();
docs.setField("id", "1");
docs.setField("name", "crxy");
solrServer.add(docs);
}
//封装一个对象建立一个索引
/**
* 建立索引2
* @throws Exception
*/
@Test
public void test3() throws Exception {
Person person = new Person();
person.setId("2");
person.setName("zs");
person.setAge("12");
solrServer.addBean(person);
}
/**
* 删除
* @throws Exception
*/
@Test
public void test4() throws Exception {
solrServer.deleteById("1");
}
}
把Id=1的给删除了的方法.(1)
方法(2)通过deleteByQuery(query)删除数据(该方法的实现自己写 未完成)
/**
* 删除
* @throws Exception
*/
@Test
public void test4() throws Exception {
solrServer.deleteById("1");
//TODO--根据查询条件查询数据
solrServer.deleteByQuery(query);
}
第七次修改(拼写检查)
略过 因为配置文件命名的问题,在192.168.8.xxx的机子上无法实现在solr的web界面进行该项检查,需要重新
打开一台机器,重新安装solr之后再实现这个功能,
第八次修改(solr的时间问题)
在test2中进行的相关修改同上原因没法在192.168.x.xxx机子上实现(代码已粘)
package cn.crxy.solr_8;
import static org.junit.Assert.*;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Collation;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Correction;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
public class SolrTest {
String baseURL = "http://192.168.x.xxx:8983/solr/collection2";
HttpSolrServer solrServer = new HttpSolrServer(baseURL);
/**
* 查询
* @throws Exception
*/
@Test
public void test1() throws Exception {
SolrQuery params = new SolrQuery();
params.set("q", "*:*");
QueryResponse response = solrServer.query(params);
SolrDocumentList results = response.getResults();
//获取数据总条数
//long numFound = results.getNumFound();
long numFound = results.size();
System.out.println("一共:"+numFound);
for (SolrDocument solrDocument : results) {
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
System.out.println(field+"--"+solrDocument.get(field));
}
System.out.println("============================================");
}
}
/**
*建立索引1
* @throws Exception
*/
@Test
public void test2() throws Exception {
SolrInputDocument docs = new SolrInputDocument();
docs.setField("id", "2");
docs.setField("name", "crxy");
docs.setField("last_modified",new Date());
solrServer.add(docs);
}
//封装一个对象建立一个索引
/**
* 建立索引2
* @throws Exception
*/
@Test
public void test3() throws Exception {
Person person = new Person();
person.setId("2");
person.setName("zs");
person.setAge("12");
solrServer.addBean(person);
}
/**
* 删除
* @throws Exception
*/
@Test
public void test4() throws Exception {
solrServer.deleteById("1");
//TODO--根据查询条件查询数据
}
/**
* 拼写检查
* @throws Exception
*/
@Test
public void test5() throws Exception {
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("qt", "/spell");
solrQuery.set("q", "name:crxx");
QueryResponse response = solrServer.query(solrQuery);
SolrDocumentList results = response.getResults();
long numFound = results.getNumFound();
if(numFound==0){
System.out.println("拼写错误");
SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse();
List<Collation> collatedResults = spellCheckResponse.getCollatedResults();
for (Collation collation : collatedResults) {
long numberOfHits = collation.getNumberOfHits();
System.out.println("推荐词语的个数:"+numberOfHits);
List<Correction> misspellingsAndCorrections = collation.getMisspellingsAndCorrections();
for (Correction correction : misspellingsAndCorrections) {
String source_data = correction.getOriginal();
String current_data = correction.getCorrection();
System.out.println("原始:"+source_data+"--推荐:"+current_data);
}
}
}else{
for (SolrDocument solrDocument : results) {
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
System.out.println(field+"--"+solrDocument.get(field));
}
System.out.println("====================================");
}
}
}
}