Advertisement
asolodin

lucene-field-sort

Nov 5th, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. import static org.junit.jupiter.api.Assertions.*;
  2. import java.io.IOException;
  3. import java.util.Random;
  4. import java.util.concurrent.atomic.AtomicInteger;
  5. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  6. import org.apache.lucene.document.Document;
  7. import org.apache.lucene.document.SortedDocValuesField;
  8. import org.apache.lucene.document.StoredField;
  9. import org.apache.lucene.index.DirectoryReader;
  10. import org.apache.lucene.index.IndexWriter;
  11. import org.apache.lucene.index.IndexWriterConfig;
  12. import org.apache.lucene.index.LeafReaderContext;
  13. import org.apache.lucene.search.DocIdSetIterator;
  14. import org.apache.lucene.search.FilterCollector;
  15. import org.apache.lucene.search.FilterLeafCollector;
  16. import org.apache.lucene.search.IndexSearcher;
  17. import org.apache.lucene.search.LeafCollector;
  18. import org.apache.lucene.search.MatchAllDocsQuery;
  19. import org.apache.lucene.search.Sort;
  20. import org.apache.lucene.search.SortField;
  21. import org.apache.lucene.search.TopDocs;
  22. import org.apache.lucene.search.TopDocsCollector;
  23. import org.apache.lucene.search.SortField.Type;
  24. import org.apache.lucene.search.TopFieldCollector;
  25. import org.apache.lucene.search.TotalHits.Relation;
  26. import org.apache.lucene.store.ByteBuffersDirectory;
  27. import org.apache.lucene.util.BytesRef;
  28. import org.junit.jupiter.api.AfterAll;
  29. import org.junit.jupiter.api.AfterEach;
  30. import org.junit.jupiter.api.BeforeAll;
  31. import org.junit.jupiter.api.BeforeEach;
  32. import org.junit.jupiter.api.Test;
  33.  
  34. class TestFieldCollector {
  35.  
  36. static final int NUM_DOCS = 10000;
  37. static ByteBuffersDirectory directory;
  38. static final long seed = 123;
  39.  
  40. @BeforeAll
  41. static void setUpBeforeClass() throws Exception {
  42. directory = new ByteBuffersDirectory();
  43. Random rand = new Random(seed);
  44. try (IndexWriter indexWriter =
  45. new IndexWriter(directory, new IndexWriterConfig(new StandardAnalyzer()))) {
  46. for (int i = 0; i < NUM_DOCS; i++) {
  47. Document doc = new Document();
  48. doc.add(new StoredField("id", String.valueOf(i)));
  49. doc.add(new SortedDocValuesField("sortedDocValues", new BytesRef("sdv" + rand.nextInt())));
  50. indexWriter.addDocument(doc);
  51. if (i % 999 == 0) {
  52. indexWriter.commit();
  53. }
  54. }
  55. }
  56. }
  57.  
  58. @AfterAll
  59. static void tearDownAfterClass() throws Exception {}
  60.  
  61. @BeforeEach
  62. void setUp() throws Exception {}
  63.  
  64. @AfterEach
  65. void tearDown() throws Exception {}
  66.  
  67. @Test
  68. void testFieldSort() throws IOException {
  69. SortField [] sf = { new SortField("sortedDocValues", Type.STRING) };
  70. Sort sort = new Sort(sf);
  71. AtomicInteger totalCollected = new AtomicInteger();
  72. TopDocsCollector topDocsCollector = TopFieldCollector.create(sort, 100, 100);
  73. FilterCollector collector = new FilterCollector(topDocsCollector) {
  74.  
  75. @Override
  76. public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
  77. return new FilterLeafCollector(super.getLeafCollector(context)) {
  78. @Override
  79. public void collect(int doc) throws IOException {
  80. totalCollected.incrementAndGet();
  81. super.collect(doc);
  82. }
  83.  
  84. @Override
  85. public DocIdSetIterator competitiveIterator() throws IOException {
  86. return in.competitiveIterator();
  87. }
  88. };
  89. }
  90. };
  91. IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
  92. searcher.search(new MatchAllDocsQuery(), collector);
  93. TopDocs docs = topDocsCollector.topDocs();
  94. assertTrue(totalCollected.get() < NUM_DOCS, String.valueOf(totalCollected.get()));
  95. assertEquals(100, docs.scoreDocs.length);
  96. assertEquals(Relation.GREATER_THAN_OR_EQUAL_TO, docs.totalHits.relation);
  97. assertTrue(docs.totalHits.value < NUM_DOCS, String.valueOf(docs.totalHits.value));
  98. }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement