本文整理汇总了Java中org.apache.lucene.search.Hits.length方法的典型用法代码示例。如果您正苦于以下问题:Java Hits.length方法的具体用法?Java Hits.length怎么用?Java Hits.length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.search.Hits
的用法示例。
在下文中一共展示了Hits.length方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: searchIndex
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
/**
* 查询索引
*
* @param keywords
* @return
* @throws Exception
*/
public List<Document> searchIndex(Integer typeId, String keywords) throws Exception {
// 1.init searcher
Analyzer analyzer = new PaodingAnalyzer();
IndexReader reader = IndexReader.open(typeId == appConfig.getGameTypeId() ? appConfig.getGameIndexDir()
: appConfig.getSoftIndexDir());
BooleanClause.Occur[] flags = new BooleanClause.Occur[] { BooleanClause.Occur.SHOULD,
BooleanClause.Occur.SHOULD };
Query query = MultiFieldQueryParser.parse(keywords, appConfig.getQueryFields(), flags, analyzer);
query = query.rewrite(reader);
// 2.search
List<Document> docs = new ArrayList<Document>();
Hits hits = (typeId == appConfig.getGameTypeId() ? gameSearcher.search(query, Sort.RELEVANCE) : softSearcher
.search(query, Sort.RELEVANCE));// searcher.search(query,
// Sort.RELEVANCE);
for (int i = 0; i < hits.length(); i++) {
docs.add(hits.doc(i));
}
// 3.return
reader.close();
return docs;
}
示例2: addField
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
public boolean addField(String recID, String prefix, String value){
try {
Searcher searcher = new IndexSearcher(indexPath);
Query q=new TermQuery(new Term("ID",recID));
Hits hits=searcher.search(q);
if ((hits==null)||(hits.length()!=1)){
return false;
}
Document doc=hits.doc(0);
IndexWriter iw = getIndexWriter();
Field f=new Field(prefix,value, Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.NO);
doc.add(f);
iw.updateDocument(new Term("ID", recID), doc);
} catch (IOException ex) {
log.fatal(ex);
return false;
}
return true;
}
示例3: addField
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
public boolean addField(String recID, String prefix, String value){
try {
Searcher searcher = new IndexSearcher(indexPath);
Query q=new TermQuery(new Term("ID",recID));
Hits hits=searcher.search(q);
if ((hits==null)||(hits.length()!=1)){
return false;
}
Document doc=hits.doc(0);
IndexWriter iw = getIndexWriter();
Field f=new Field(prefix,value, Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.NO);
doc.add(f);
iw.updateDocument(new Term("ID", recID), doc);
iw.close();
} catch (IOException ex) {
log.fatal(ex);
return false;
}
return true;
}
示例4: deleteField
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
public void deleteField(String recID, String prefix, String value){
try {
Searcher searcher = new IndexSearcher(indexPath);
Query q=new TermQuery(new Term("ID",recID));
Hits hits=searcher.search(q);
if ((hits==null)||(hits.length()!=1)){
log.fatal("greska pri brisanju polja. Zapis: "+recID);
return ;
}
Document doc=hits.doc(0);
Field [] fields=doc.getFields(prefix);
IndexWriter iw = getIndexWriter();
doc.removeFields(prefix);
for(int i=0;i<fields.length;i++){
if(!fields[i].stringValue().equals(value)){
doc.add(fields[i]);
}
}
iw.updateDocument(new Term("ID", recID), doc);
iw.close();
} catch (IOException ex) {
log.fatal(ex);
}
}
示例5: verify
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
private void verify(Directory dir, int expectedHits) throws IOException {
IndexSearcher searcher = new IndexSearcher(dir);
Hits hits = searcher.search(new TermQuery(new Term("content", "apache")));
int numHits = hits.length();
assertEquals(expectedHits, numHits);
int[] docs = new int[numHits];
for (int i = 0; i < numHits; i++) {
Document hit = hits.doc(i);
docs[Integer.parseInt(hit.get("id"))]++;
}
for (int i = 0; i < numHits; i++) {
assertEquals(1, docs[i]);
}
searcher.close();
}
示例6: docExists
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
/** Checks if a given document exists in the index.<br><br>
*
* @param key The key associated with the document in the index.
*
* @return true if a document was found, false if not.
*
*/
public boolean docExists(String key)
throws ParserConfigurationException, SAXException, IOException
{
// We need to find the docInfo chunk that contains the specified
// file. So construct a boolean query looking for a chunk with
// a "docInfo" field AND a "key" field containing the specified
// source file key.
//
BooleanQuery query = new BooleanQuery();
Term docInfo = new Term("docInfo", "1");
Term srcPath = new Term("key", key);
query.add(new TermQuery(docInfo), BooleanClause.Occur.MUST);
query.add(new TermQuery(srcPath), BooleanClause.Occur.MUST);
// Use the query to see if the document is in the index..
Hits match = indexSearcher.search(query);
// Let the caller know if we found a match.
return match.length() > 0;
}
示例7: performSearch
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
public Hits performSearch(Directory dir, String query, boolean useMust)
throws Exception {
NGramQueryParser parser = new NGramQueryParser("name",
new NGramAnalyzer(min_ngram, max_ngram), useMust);
IndexSearcher searcher = new IndexSearcher(dir);
Query q = parser.parse(query);
Hits hits = searcher.search(q);
log.info("Original Query = " + query);
log.info("Parsed Query = " + q.toString());
log.info("Hits.length() = " + hits.length());
for (int i=0; i < hits.length(); i++) {
log.debug("Document<"+hits.id(i)+"> = " + hits.doc(i));
//Explanation explain = searcher.explain(q, hits.id(i));
//log.debug("explain = " + explain.toString());
}
return hits;
}
示例8: thresholdHits
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
protected int thresholdHits(Hits hits) throws IOException {
/** We could consider doing thresholding as a relative thing...
* instead of checking against an absolute value, we grab top score
* then filter based on difference from that...
*/
int counter = 0;
for (int i=0; i < hits.length(); i++) {
if (hits.score(i) >= score_threshold) {
counter++;
}
else {
break;
}
}
return counter;
}
示例9: debugExplainResults
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
private void debugExplainResults(String indexName, Hits hits, IndexSearcher searcher,
Query q, Set<Term> queryTerms)
throws IOException {
log.debug("Parsed Query is " + q.toString());
log.debug("Looking at index: " + indexName);
for (int i = 0; i < hits.length(); i++) {
if ((i < 10)) {
Document doc = hits.doc(i);
Float score = hits.score(i);
Explanation ex = searcher.explain(q, hits.id(i));
log.debug("Looking at hit<" + i + ", " + hits.id(i) + ", " + score +
">: " + doc);
log.debug("Explanation: " + ex);
MatchingField match = new MatchingField(q.toString(), doc, queryTerms);
String fieldName = match.getFieldName();
String fieldValue = match.getFieldValue();
log.debug("Guessing that matched fieldName is " + fieldName + " = " +
fieldValue);
}
}
}
示例10: LuceneResultSet
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
/**
* Wrap a lucene seach result with node support
*
* @param hits Hits
* @param searcher Searcher
* @param nodeService nodeService
* @param tenantService tenant service
* @param searchParameters SearchParameters
* @param config - lucene config
*/
public LuceneResultSet(Hits hits, Searcher searcher, NodeService nodeService, TenantService tenantService, SearchParameters searchParameters,
LuceneConfig config)
{
super();
this.hits = hits;
this.searcher = searcher;
this.nodeService = nodeService;
this.tenantService = tenantService;
this.searchParameters = searchParameters;
this.config = config;
prefetch = new BitSet(hits.length());
}
示例11: selectAll
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
public Result selectAll(Query query, String sortPrefix){
try {
BooleanQuery.setMaxClauseCount(20000);//zbog heap-a
Searcher searcher = new IndexSearcher(indexPath);
Hits hits;
if (sortPrefix == null || "".equals(sortPrefix))
hits = searcher.search(query);
else {
int sortType = SortField.STRING;
if ("RN_sort".equals(sortPrefix))
sortType = SortField.INT;
hits = searcher.search(query, new Sort(
new SortField(sortPrefix, sortType)));
}
int n = hits.length();
int[] retVal = new int[n];
List<String> invs = new ArrayList<String>();
Field[] tmp = null;
for (int i = 0; i < n; i++) {
String recordID = hits.doc(i).get("ID");
retVal[i] = Integer.parseInt(recordID);
tmp = hits.doc(i).getFields("IN");
if (tmp != null){
for (int j = 0; j<tmp.length; j++){
invs.add(tmp[j].stringValue());
}
}
}
searcher.close();
Result result = new Result();
result.setRecords(retVal);
result.setInvs(invs);
return result;
} catch (Exception ex) {
log.fatal(ex);
return null;
}
}
示例12: select
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
public int[] select(Query query, Filter filter, String sortPrefix){
try {
BooleanQuery.setMaxClauseCount(Integer.MAX_VALUE);
Searcher searcher = new IndexSearcher(indexPath);
Hits hits;
if (sortPrefix == null || "".equals(sortPrefix)){
hits = searcher.search(query,filter);
} else {
int sortType = SortField.STRING;
if ("RN_sort".equals(sortPrefix))
sortType = SortField.INT;
hits = searcher.search(query,filter, new Sort(
new SortField(sortPrefix, sortType)));
}
int n = hits.length();
int[] retVal = new int[n];
for (int i = 0; i < n; i++) {
String recordID = hits.doc(i).get("ID");
retVal[i] = Integer.parseInt(recordID);
}
searcher.close();
return retVal;
} catch (Exception ex) {
log.fatal(ex);
return null;
}
}
示例13: selectExpand
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
public List<String> selectExpand(String query, String prefix,String text){
try {
WhitespaceAnalyzer sa= new WhitespaceAnalyzer();
BooleanQuery.setMaxClauseCount(Integer.MAX_VALUE);
QueryParser p = new QueryParser("contents", sa);
Query q = p.parse(query);
Searcher searcher = new IndexSearcher(indexPath);
StopWatch clock=new StopWatch();
clock.start();
Hits hits = searcher.search(q);
int n = hits.length();
List <String> expandList = new ArrayList<String>();
Field[] tmp = null;
String pom="";
for (int i = 0; i < n; i++) {
tmp = hits.doc(i).getFields(prefix);
if (tmp != null){
for (int j = 0; j<tmp.length; j++){
pom=tmp[j].stringValue().replace("0start0 ", "");
pom=pom.replace(" 0end0", "");
if(pom.startsWith(text)&&(!expandList.contains(pom))){
expandList.add(pom);
}
}
}
}
clock.stop();
searcher.close();
return expandList;
} catch (Exception ex) {
log.fatal(ex);
return null;
}
}
示例14: verify
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
private void verify(Shard[] shards) throws IOException {
// verify the index
IndexReader[] readers = new IndexReader[shards.length];
for (int i = 0; i < shards.length; i++) {
Directory dir =
new FileSystemDirectory(fs, new Path(shards[i].getDirectory()),
false, conf);
readers[i] = IndexReader.open(dir);
}
IndexReader reader = new MultiReader(readers);
IndexSearcher searcher = new IndexSearcher(reader);
Hits hits = searcher.search(new TermQuery(new Term("content", "apache")));
assertEquals(0, hits.length());
hits = searcher.search(new TermQuery(new Term("content", "hadoop")));
assertEquals(numDocsPerRun / 2, hits.length());
int[] counts = new int[numDocsPerRun];
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
counts[Integer.parseInt(doc.get("id"))]++;
}
for (int i = 0; i < numDocsPerRun; i++) {
if (i % 2 == 0) {
assertEquals(0, counts[i]);
} else {
assertEquals(1, counts[i]);
}
}
searcher.close();
reader.close();
}
示例15: searchCorpus
import org.apache.lucene.search.Hits; //导入方法依赖的package包/类
/**
* Searches the current corpus using the search terms in the search field.
*/
private void searchCorpus() {
if (search.getText().trim().equals("")) return;
try {
indexSearcher = guess.getSelected() != null ?
getIndex(getDiffCorpus(gold.getSelected(), guess.getSelected())) :
getIndex(gold.getSelected());
//System.out.println("Searching...");
QueryParser parser = new QueryParser("Word", analyzer);
Query query = parser.parse(search.getText());
Hits hits = indexSearcher.search(query);
Highlighter highlighter = new Highlighter(new QueryScorer(query));
DefaultListModel model = new DefaultListModel();
for (int i = 0; i < hits.length(); i++) {
Document hitDoc = hits.doc(i);
int nr = Integer.parseInt(hitDoc.get("<nr>"));
//System.out.println(hitDoc.get("<nr>"));
String best = null;
for (Object field : hitDoc.getFields()) {
Field f = (Field) field;
best = highlighter.getBestFragment(analyzer, f.name(), hitDoc.get(f.name()));
if (best != null) break;
}
if (best != null)
model.addElement(new Result(nr, "<html>" + nr + ":" + best + "</html>"));
//System.out.println(highlighter.getBestFragment(analyzer, "Word", hitDoc.get("Word")));
//assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
}
results.setModel(model);
repaint();
} catch (Exception ex) {
ex.printStackTrace();
}
}