hidingmyname opened a new issue, #16821:
URL: https://github.com/apache/lucene/issues/16821
### Description
**Location** (lucene @ main f94888d85c1):
`lucene/highlighter/src/java/org/apache/lucene/search/highlight/package-info.java:30`
**Current text**
```
IndexSearcher searcher = new IndexSearcher(directory);
```
**Problem**: IndexSearcher has had no `Directory` constructor since 4.0, and
`IndexSearcher.doc(int)` no longer exists on main (use
`storedFields().document(int)`), so the example doesn't compile.
**Suggested fix**: open a reader for the searcher, and load stored fields
via `storedFields()`.
```diff
---
a/lucene/highlighter/src/java/org/apache/lucene/search/highlight/package-info.java
+++
b/lucene/highlighter/src/java/org/apache/lucene/search/highlight/package-info.java
@@ -28,5 +28,5 @@
* <pre><code class="language-java">
* //... Above, create documents with two fields, one with term vectors
(tv) and one without (notv)
- * IndexSearcher searcher = new IndexSearcher(directory);
+ * IndexSearcher searcher = new
IndexSearcher(DirectoryReader.open(directory));
* QueryParser parser = new QueryParser("notv", analyzer);
* Query query = parser.parse("million");
@@ -38,5 +38,5 @@
* for (int i = 0; i < 10; i++) {
* int id = hits.scoreDocs[i].doc;
- * Document doc = searcher.doc(id);
+ * Document doc = searcher.storedFields().document(id);
* String text = doc.get("notv");
* TokenStream tokenStream =
TokenSources.getAnyTokenStream(searcher.getIndexReader(), id, "notv", analyzer);
```
**Already fixed in Lucene.NET**: apache/lucenenet@2e320ea9c
**How Lucene.NET fixed it**: commit `2e320ea9cd1` (2021-03-30) "docs:
Lucene.Net.Highlighter: Fixed broken formatting and links (see #284, #300)",
file `src/Lucene.Net.Highlighter/Highlight/package.md`
```diff
@@ -25,35 +25,82 @@ The highlight package contains classes to provide
"keyword in context" features
typically used to highlight search terms in the text of results pages.
The Highlighter class is the central component and can be used to extract
the
most interesting sections of a piece of text and highlight them, with the
help of
-Fragmenter, fragment Scorer, and Formatter classes.
+[Fragmenter](xref:Lucene.Net.Search.Highlight.IFragmenter), fragment
[Scorer](xref:Lucene.Net.Search.Highlight.IScorer), and
[Formatter](xref:Lucene.Net.Search.Highlight.IFormatter) classes.
## Example Usage
- //... Above, create documents with two fields, one with term vectors
(tv) and one without (notv)
- IndexSearcher searcher = new IndexSearcher(directory);
- QueryParser parser = new QueryParser("notv", analyzer);
- Query query = parser.parse("million");
-
- TopDocs hits = searcher.search(query, 10);
-
- SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
- Highlighter highlighter = new Highlighter(htmlFormatter, new
QueryScorer(query));
- for (int i = 0; i < 10;="" i++)="" {="" int=""
id="hits.scoreDocs[i].doc;" document="" doc="searcher.doc(id);" string=""
text="doc.get(" notv");"="" tokenstream=""
tokenstream="TokenSources.getAnyTokenStream(searcher.getIndexReader()," id,=""
"notv",="" analyzer);="" textfragment[]=""
frag="highlighter.getBestTextFragments(tokenStream," text,="" false,=""
10);//highlighter.getbestfragments(tokenstream,="" text,="" 3,="" "...");=""
for="" (int="" j="0;" j="">< frag.length;="" j++)="" {="" if="" ((frag[j]=""
!="null)" &&="" (frag[j].getscore()=""> 0)) {
- System.out.println((frag[j].toString()));
- }
+```cs
+const LuceneVersion matchVersion = LuceneVersion.LUCENE_48;
+Analyzer analyzer = new StandardAnalyzer(matchVersion);
+
+// Create an index to search
+string indexPath = Path.Combine(Path.GetTempPath(),
Path.GetFileNameWithoutExtension(Path.GetTempFileName()));
+Directory dir = FSDirectory.Open(indexPath);
+using IndexWriter writer = new IndexWriter(dir, new
IndexWriterConfig(matchVersion, analyzer));
+
+// This field must store term vectors and term vector offsets
+var fieldType = new FieldType(TextField.TYPE_STORED)
+{
+ StoreTermVectors = true,
+ StoreTermVectorOffsets = true
+};
+fieldType.Freeze();
+
+// Create documents with two fields, one with term vectors (tv) and one
without (notv)
+writer.AddDocument(new Document {
+ new Field("tv", "Thanks a million!", fieldType),
+ new TextField("notv", "A million ways to win.", Field.Store.YES)
+});
+writer.AddDocument(new Document {
+ new Field("tv", "Hopefully, this won't highlight a million times.",
fieldType),
+ new TextField("notv", "There are a million different ways to do that!",
Field.Store.YES)
+});
+
+using IndexReader indexReader = writer.GetReader(applyAllDeletes: true);
+writer.Dispose();
+
+// Now search our index using an existing or new IndexReader
+
+IndexSearcher searcher = new IndexSearcher(indexReader);
+QueryParser parser = new QueryParser(matchVersion, "notv", analyzer);
+Query query = parser.Parse("million");
+
+TopDocs hits = searcher.Search(query, 10);
+
+SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
```
I can submit a PR for this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]