我正在使用solr api开发自定义请求处理程序。 作为参考,我将“MoreLikeThis”作为参考。
我可以使用“getDocList”API访问文档,然后获取“iterator”对象,但它只给我文档ID。
如何访问文档的每个字段??
package org.apache.solr.handler.ext;
import org.apache.solr.request.*;
import org.apache.solr.response.*;
import org.apache.solr.handler.*;
import org.apache.solr.core.*;
import org.apache.solr.schema.*;
import org.apache.lucene.analysis.*;
import org.apache.solr.common.params.*;
import org.apache.solr.search.*;
import org.apache.lucene.search.*;
import org.apache.solr.search.SolrIndexSearcher.*;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import java.util.*;
import java.io.*;
public class MyRequestHandler extends RequestHandlerBase{
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
throws Exception {
System.out.println("I am in My Handler");
/**
* Working with core
*/
SolrCore core = req.getCore();
System.out.println("Core is " + "" + core.getDescription());
System.out.println("Data directory is " + "" + core.getDataDir());
System.out.println("Index directory is " + "" + core.getIndexDir());
/**
* Working with schema
*/
IndexSchema schema = core.getSchema();
SchemaField field = schema.getField("country");
System.out.println(field.isRequired());
/**
* Create Map
*/
IndexSchema schemaObj = req.getSchema();
Map<String , SchemaField>map = req.getSchema().getFields();
// Iterate over keys
for (String str : map.keySet()){
System.out.println(str);
}
// Iterate over SchemaField
for (SchemaField schemaFieldObj: map.values()){
System.out.println(schemaFieldObj.isRequired());
}
/**
* Find the Unique Key Field
*/
field = schemaObj.getUniqueKeyField();
System.out.println(field.getName());
System.out.println(schemaObj.getQueryParserDefaultOperator());
/**
* Working with request parameters
*/
SolrParams reqParams = req.getParams();
String q = reqParams.get(CommonParams.Q);
System.out.println(reqParams.get(CommonParams.Q));
String defType = reqParams.get(QueryParsing.DEFTYPE , QParserPlugin.DEFAULT_QTYPE);
System.out.println(defType);
/**
* Working with QParser
*/
QParser queryParser = QParser.getParser(q, defType, req);
Query query = queryParser.getQuery();
/**
* Now get searcher
*/
SolrIndexSearcher indexSearcher = req.getSearcher();
DocList matchDocs = indexSearcher.getDocList(query, null, null, 1, 100000,10000);
// Number of documents matched
System.out.println(matchDocs.matches());
// Get Iterator
// get documentreader
Reader reader;
IndexReader readDoc;
Document doc;
DocIterator iterator = matchDocs.iterator();
while (iterator.hasNext()){
Integer id = iterator.nextDoc();
System.out.println("Here I am " + id);
/* HOW TO ACCESS ALL FIELDS OF DOCUMENT */
}
}
@Override
public String getDescription() {
return "My Search Handler";
}
@Override
public String getSource() {
return "$Source$";
}
@Override
public String getVersion() {
return "$Revision$";
}
}
答案 0 :(得分:4)
嗯,我得到了答案.. 这是帮助我到达每个领域的代码..
package org.apache.solr.handler.ext;
public class MyRequestHandler extends RequestHandlerBase{
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
throws Exception {
System.out.println("I am in My Handler");
/**
* Working with core
*/
SolrCore core = req.getCore();
System.out.println("Core is " + "" + core.getDescription());
System.out.println("Data directory is " + "" + core.getDataDir());
System.out.println("Index directory is " + "" + core.getIndexDir());
/**
* Working with schema
*/
IndexSchema schema = core.getSchema();
SchemaField field = schema.getField("country");
System.out.println(field.isRequired());
/**
* Create Map
*/
IndexSchema schemaObj = req.getSchema();
Map<String , SchemaField>map = req.getSchema().getFields();
// Iterate over keys
for (String str : map.keySet()){
System.out.println(str);
}
// Iterate over SchemaField
for (SchemaField schemaFieldObj: map.values()){
System.out.println(schemaFieldObj.isRequired());
}
/**
* Find the Unique Key Field
*/
field = schemaObj.getUniqueKeyField();
System.out.println(field.getName());
System.out.println(schemaObj.getQueryParserDefaultOperator());
/**
* Working with request parameters
*/
SolrParams reqParams = req.getParams();
String q = reqParams.get(CommonParams.Q);
System.out.println(reqParams.get(CommonParams.Q));
String defType = reqParams.get(QueryParsing.DEFTYPE , QParserPlugin.DEFAULT_QTYPE);
System.out.println(defType);
/**
* Working with QParser
*/
QParser queryParser = QParser.getParser(q, defType, req);
Query query = queryParser.getQuery();
/**
* Now get searcher
*/
SolrIndexSearcher indexSearcher = req.getSearcher();
IndexReader reader = indexSearcher.getIndexReader();
DocList matchDocs = indexSearcher.getDocList(query, null, null, 1, 100000,10000);
// Number of documents matched
System.out.println(matchDocs.matches());
// Get Iterator
// get documentreader
Document doc;
String[] DocFields;
DocIterator iterator = matchDocs.iterator();
while (iterator.hasNext()){
Integer id = iterator.nextDoc();
System.out.println("Here I am " + id);
doc = reader.document(id);
DocFields = doc.getValues("state");
for(int i=0;i<DocFields.length;i++){
System.out.println(DocFields[i]);
}
}
}
@Override
public String getDescription() {
return "My Search Handler";
}
@Override
public String getSource() {
return "$Source$";
}
@Override
public String getVersion() {
return "$Revision$";
}
}
总之,我们要访问文档,我们需要“读者”一个“IndexReader”类的对象然后从'reader'调用方法'getValues('你的字段名')'。它将返回字符串数组。