Java Code Examples for org.apache.lucene.search.Hits#length()
The following examples show how to use
org.apache.lucene.search.Hits#length() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NGramQueryParserTest.java From uyuni with GNU General Public License v2.0 | 6 votes |
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; }
Example 2
Source File: NGramTestSetup.java From uyuni with GNU General Public License v2.0 | 6 votes |
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; }
Example 3
Source File: NGramQueryParserTest.java From spacewalk with GNU General Public License v2.0 | 6 votes |
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; }
Example 4
Source File: NGramTestSetup.java From spacewalk with GNU General Public License v2.0 | 6 votes |
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; }
Example 5
Source File: IndexManager.java From spacewalk with GNU General Public License v2.0 | 6 votes |
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); } } }
Example 6
Source File: TestMixedDirectory.java From RDFS with Apache License 2.0 | 6 votes |
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(); }
Example 7
Source File: TestMixedDirectory.java From hadoop-gpu with Apache License 2.0 | 6 votes |
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(); }
Example 8
Source File: NGramTestSetup.java From uyuni with GNU General Public License v2.0 | 5 votes |
protected void displayHits(Hits hits) throws IOException { for (int i = 0; i < hits.length(); i++) { Document doc = hits.doc(i); String name = doc.get("name"); String description = doc.get("description"); log.info("Hit<" + i + "> Score< " + hits.score(i) + "> name = <" + name + "> description = <" + description + ">"); } }
Example 9
Source File: IndexManager.java From uyuni with GNU General Public License v2.0 | 5 votes |
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); } } }
Example 10
Source File: LuceneResultSet.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * 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()); }
Example 11
Source File: NGramTestSetup.java From spacewalk with GNU General Public License v2.0 | 5 votes |
protected void displayHits(Hits hits) throws IOException { for (int i = 0; i < hits.length(); i++) { Document doc = hits.doc(i); String name = doc.get("name"); String description = doc.get("description"); log.info("Hit<" + i + "> Score< " + hits.score(i) + "> name = <" + name + "> description = <" + description + ">"); } }
Example 12
Source File: TestDistributionPolicy.java From RDFS with Apache License 2.0 | 5 votes |
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(); }
Example 13
Source File: TestDistributionPolicy.java From hadoop-gpu with Apache License 2.0 | 5 votes |
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(); }
Example 14
Source File: IndexManager.java From uyuni with GNU General Public License v2.0 | 4 votes |
private List<Result> processHits(String indexName, Hits hits, Set<Term> queryTerms, String query, String lang) throws IOException { List<Result> retval = new ArrayList<Result>(); for (int x = 0; x < hits.length(); x++) { Document doc = hits.doc(x); Result pr = null; if (!isScoreAcceptable(indexName, hits, x, query)) { break; } if (indexName.compareTo(BuilderFactory.DOCS_TYPE) == 0) { pr = new DocResult(x, hits.score(x), doc); String summary = lookupDocSummary(doc, query, lang); if (summary != null) { ((DocResult)pr).setSummary(summary); } } else if (indexName.compareTo(BuilderFactory.HARDWARE_DEVICE_TYPE) == 0) { pr = new HardwareDeviceResult(x, hits.score(x), doc); } else if (indexName.compareTo(BuilderFactory.SNAPSHOT_TAG_TYPE) == 0) { pr = new SnapshotTagResult(x, hits.score(x), doc); } else if (indexName.compareTo(BuilderFactory.SERVER_CUSTOM_INFO_TYPE) == 0) { pr = new ServerCustomInfoResult(x, hits.score(x), doc); } else if (indexName.compareTo(BuilderFactory.XCCDF_IDENT_TYPE) == 0) { pr = new Result(x, doc.getField("id").stringValue(), doc.getField("identifier").stringValue(), hits.score(x)); } else { pr = new Result(x, doc.getField("id").stringValue(), doc.getField("name").stringValue(), hits.score(x)); } if (log.isDebugEnabled()) { log.debug("Hit[" + x + "] Score = " + hits.score(x) + ", Result = " + pr); } /** * matchingField will help the webUI to understand what field was responsible * for this match. Later implementation should use "Explanation" to determine * field, for now we will simply grab one term and return it's field. */ try { MatchingField match = new MatchingField(query, doc, queryTerms); pr.setMatchingField(match.getFieldName()); pr.setMatchingFieldValue(match.getFieldValue()); log.info("hit[" + x + "] matchingField is being set to: <" + pr.getMatchingField() + "> based on passed in query field. " + "matchingFieldValue = " + pr.getMatchingFieldValue()); } catch (Exception e) { log.error("Caught exception: ", e); } if (pr != null) { retval.add(pr); } if (maxHits > 0 && x == maxHits) { break; } } return retval; }
Example 15
Source File: SearchInTypeShortName.java From gAnswer with BSD 3-Clause "New" or "Revised" License | 4 votes |
public ArrayList<String> searchType(String s, double thres1, double thres2, int k) throws Exception { Hits hits = null; String queryString = null; Query query = null; IndexSearcher searcher = new IndexSearcher(Globals.localPath+"data/DBpedia2016/lucene/type_fragment_index"); ArrayList<String> typeNames = new ArrayList<String>(); //String[] array = s.split(" "); //queryString = array[array.length-1]; queryString = s; Analyzer analyzer = new StandardAnalyzer(); try { QueryParser qp = new QueryParser("SplittedTypeShortName", analyzer); query = qp.parse(queryString); } catch (ParseException e) { e.printStackTrace(); } if (searcher != null) { hits = searcher.search(query); System.out.println("find " + hits.length() + " answars!"); if (hits.length() > 0) { for (int i=0; i<hits.length(); i++) { if (i < k) { System.out.println("<<<<---" + hits.doc(i).get("TypeShortName") + " : " + hits.score(i)); if(hits.score(i) >= thres1){ System.out.println("Score>=thres1("+thres1+") ---" + hits.doc(i).get("TypeShortName") + " : " + hits.score(i)); typeNames.add(hits.doc(i).get("TypeShortName")); //if (satisfiedStrictly(hits.doc(i).get("SplittedTypeShortName"), queryString)) typeNames.add(hits.doc(i).get("TypeShortName")); } else { //break; } } else { if(hits.score(i) >= thres2){ System.out.println("<<<<---" + hits.doc(i).get("TypeShortName") + " : " + hits.score(i)); typeNames.add(hits.doc(i).get("TypeShortName")); //if (satisfiedStrictly(hits.doc(i).get("SplittedTypeShortName"), queryString)) typeNames.add(hits.doc(i).get("TypeShortName")); } else { break; } } } } } return typeNames; }
Example 16
Source File: IndexManager.java From spacewalk with GNU General Public License v2.0 | 4 votes |
private List<Result> processHits(String indexName, Hits hits, Set<Term> queryTerms, String query, String lang) throws IOException { List<Result> retval = new ArrayList<Result>(); for (int x = 0; x < hits.length(); x++) { Document doc = hits.doc(x); Result pr = null; if (!isScoreAcceptable(indexName, hits, x, query)) { break; } if (indexName.compareTo(BuilderFactory.DOCS_TYPE) == 0) { pr = new DocResult(x, hits.score(x), doc); String summary = lookupDocSummary(doc, query, lang); if (summary != null) { ((DocResult)pr).setSummary(summary); } } else if (indexName.compareTo(BuilderFactory.HARDWARE_DEVICE_TYPE) == 0) { pr = new HardwareDeviceResult(x, hits.score(x), doc); } else if (indexName.compareTo(BuilderFactory.SNAPSHOT_TAG_TYPE) == 0) { pr = new SnapshotTagResult(x, hits.score(x), doc); } else if (indexName.compareTo(BuilderFactory.SERVER_CUSTOM_INFO_TYPE) == 0) { pr = new ServerCustomInfoResult(x, hits.score(x), doc); } else if (indexName.compareTo(BuilderFactory.XCCDF_IDENT_TYPE) == 0) { pr = new Result(x, doc.getField("id").stringValue(), doc.getField("identifier").stringValue(), hits.score(x)); } else { pr = new Result(x, doc.getField("id").stringValue(), doc.getField("name").stringValue(), hits.score(x)); } if (log.isDebugEnabled()) { log.debug("Hit[" + x + "] Score = " + hits.score(x) + ", Result = " + pr); } /** * matchingField will help the webUI to understand what field was responsible * for this match. Later implementation should use "Explanation" to determine * field, for now we will simply grab one term and return it's field. */ try { MatchingField match = new MatchingField(query, doc, queryTerms); pr.setMatchingField(match.getFieldName()); pr.setMatchingFieldValue(match.getFieldValue()); log.info("hit[" + x + "] matchingField is being set to: <" + pr.getMatchingField() + "> based on passed in query field. " + "matchingFieldValue = " + pr.getMatchingFieldValue()); } catch (Exception e) { log.error("Caught exception: ", e); } if (pr != null) { retval.add(pr); } if (maxHits > 0 && x == maxHits) { break; } } return retval; }