|
16 | 16 | */ |
17 | 17 | package org.apache.lucene.queries.function; |
18 | 18 |
|
19 | | -import org.apache.lucene.analysis.Analyzer; |
20 | 19 | import org.apache.lucene.document.Document; |
21 | | -import org.apache.lucene.document.Field; |
| 20 | +import org.apache.lucene.document.Field.Store; |
| 21 | +import org.apache.lucene.document.StringField; |
22 | 22 | import org.apache.lucene.document.TextField; |
23 | | -import org.apache.lucene.index.IndexReader; |
24 | 23 | import org.apache.lucene.index.IndexWriterConfig; |
25 | 24 | import org.apache.lucene.queries.function.valuesource.NormValueSource; |
26 | 25 | import org.apache.lucene.search.IndexSearcher; |
27 | 26 | import org.apache.lucene.search.Query; |
28 | 27 | import org.apache.lucene.search.ScoreDoc; |
29 | 28 | import org.apache.lucene.search.Sort; |
30 | | -import org.apache.lucene.search.SortField; |
31 | 29 | import org.apache.lucene.search.TopDocs; |
32 | 30 | import org.apache.lucene.search.similarities.ClassicSimilarity; |
33 | 31 | import org.apache.lucene.search.similarities.Similarity; |
34 | | -import org.apache.lucene.store.Directory; |
35 | 32 | import org.apache.lucene.tests.analysis.MockAnalyzer; |
36 | 33 | import org.apache.lucene.tests.index.RandomIndexWriter; |
37 | 34 | import org.apache.lucene.tests.search.CheckHits; |
38 | 35 | import org.apache.lucene.tests.util.LuceneTestCase; |
39 | | -import org.junit.AfterClass; |
40 | | -import org.junit.BeforeClass; |
41 | 36 |
|
42 | 37 | public class TestLongNormValueSource extends LuceneTestCase { |
43 | 38 |
|
44 | | - static Directory dir; |
45 | | - static IndexReader reader; |
46 | | - static IndexSearcher searcher; |
47 | | - static Analyzer analyzer; |
48 | | - |
49 | | - private static final Similarity sim = new ClassicSimilarity(); |
50 | | - |
51 | | - @BeforeClass |
52 | | - public static void beforeClass() throws Exception { |
53 | | - dir = newDirectory(); |
54 | | - analyzer = new MockAnalyzer(random()); |
| 39 | + public void testNorm() throws Exception { |
| 40 | + Similarity sim = new ClassicSimilarity(); |
| 41 | + final var field = "text"; |
| 42 | + var analyzer = new MockAnalyzer(random()); |
| 43 | + var dir = newDirectory(); |
55 | 44 | IndexWriterConfig iwConfig = newIndexWriterConfig(analyzer); |
56 | 45 | iwConfig.setMergePolicy(newLogMergePolicy()); |
57 | 46 | iwConfig.setSimilarity(sim); |
58 | | - RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConfig); |
59 | 47 |
|
60 | | - Document doc = new Document(); |
61 | | - doc.add(new TextField("text", "this is a test test test", Field.Store.NO)); |
62 | | - iw.addDocument(doc); |
| 48 | + try (RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConfig)) { |
63 | 49 |
|
64 | | - doc = new Document(); |
65 | | - doc.add(new TextField("text", "second test", Field.Store.NO)); |
66 | | - iw.addDocument(doc); |
| 50 | + // the leading number is the number of terms / positions for that value |
| 51 | + final var store = Store.YES; |
| 52 | + Document doc = new Document(); |
| 53 | + doc.add(new StringField("id", "6", store)); |
| 54 | + doc.add(new TextField(field, "this is a test test test", store)); |
| 55 | + iw.addDocument(doc); |
67 | 56 |
|
68 | | - reader = iw.getReader(); |
69 | | - searcher = newSearcher(reader); |
70 | | - iw.close(); |
71 | | - } |
| 57 | + doc = new Document(); |
| 58 | + doc.add(new StringField("id", "2", store)); |
| 59 | + doc.add(new TextField(field, "second test", store)); |
| 60 | + iw.addDocument(doc); |
72 | 61 |
|
73 | | - @AfterClass |
74 | | - public static void afterClass() throws Exception { |
75 | | - searcher = null; |
76 | | - reader.close(); |
77 | | - reader = null; |
78 | | - dir.close(); |
79 | | - dir = null; |
80 | | - analyzer.close(); |
81 | | - analyzer = null; |
82 | | - } |
| 62 | + IndexSearcher searcher; |
| 63 | + try (var reader = iw.getReader()) { |
| 64 | + searcher = newSearcher(reader); |
| 65 | + searcher.setSimilarity(sim); |
83 | 66 |
|
84 | | - public void testNorm() throws Exception { |
85 | | - Similarity saved = searcher.getSimilarity(); |
86 | | - try { |
87 | | - // no norm field (so agnostic to indexed similarity) |
88 | | - searcher.setSimilarity(sim); |
89 | | - assertHits(new FunctionQuery(new NormValueSource("text")), new float[] {0f, 0f}); |
| 67 | + Query q = new FunctionQuery(new NormValueSource(field)); |
| 68 | + TopDocs docs = searcher.search(q, searcher.getIndexReader().numDocs(), new Sort(), true); |
| 69 | + final var expectedDocIds = new int[] {1, 0}; |
| 70 | + final var expectedScoreDocs = |
| 71 | + new ScoreDoc[] {new ScoreDoc(1, 0.70710677f), new ScoreDoc(0, 0.4082483f)}; |
| 72 | + CheckHits.checkHitsQuery(q, expectedScoreDocs, docs.scoreDocs, expectedDocIds); |
| 73 | + CheckHits.checkExplanations(q, "", searcher); |
| 74 | + final var storedFields = searcher.getIndexReader().storedFields(); |
| 75 | + assertEquals("2", storedFields.document(expectedDocIds[0]).get("id")); |
| 76 | + assertEquals("6", storedFields.document(expectedDocIds[1]).get("id")); |
| 77 | + } |
90 | 78 | } finally { |
91 | | - searcher.setSimilarity(saved); |
| 79 | + dir.close(); |
| 80 | + analyzer.close(); |
92 | 81 | } |
93 | 82 | } |
94 | | - |
95 | | - void assertHits(Query q, float[] scores) throws Exception { |
96 | | - ScoreDoc[] expected = new ScoreDoc[scores.length]; |
97 | | - int[] expectedDocs = new int[scores.length]; |
98 | | - for (int i = 0; i < expected.length; i++) { |
99 | | - expectedDocs[i] = i; |
100 | | - expected[i] = new ScoreDoc(i, scores[i]); |
101 | | - } |
102 | | - TopDocs docs = searcher.search(q, 2, new Sort(new SortField("id", SortField.Type.STRING))); |
103 | | - |
104 | | - /* |
105 | | - for (int i=0;i<docs.scoreDocs.length;i++) { |
106 | | - System.out.println(searcher.explain(q, docs.scoreDocs[i].doc)); |
107 | | - } |
108 | | - */ |
109 | | - |
110 | | - CheckHits.checkHits(random(), q, "", searcher, expectedDocs); |
111 | | - CheckHits.checkHitsQuery(q, expected, docs.scoreDocs, expectedDocs); |
112 | | - CheckHits.checkExplanations(q, "", searcher); |
113 | | - } |
114 | 83 | } |
0 commit comments