Hi, I'm trying to index numeric, and then to query them using java api and lucene 8.6. I tried several numeric Field types, but I can't make it work. Can someone help me to store numeric in the datastore and then to query them ? ThanksStéphane
Here is a simple JUnit test package test.data; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.*; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.io.IOException; import java.math.BigInteger; import java.util.Arrays; import java.util.Collection; @RunWith(Parameterized.class) public class MTest { static Directory index = new RAMDirectory(); private String query; public MTest(String query) { this.query = query; } @Parameterized.Parameters(name = "{0}") public static Collection<Object[]> data() { Object[][] scannedClasses = new Object[][]{{"TextField:Client"}, {"LongPoint:17"}, {"LongPoint:[16 TO 18]"}, {"BigIntegerPoint:21"}, {"BigIntegerPoint:[20 TO 22]"}, {"StringField:Stephane"}, {"Date:20200615"}, {"Date:[20200614 TO 20200616]"}, {"DoublePoint:37"}, {"DoublePoint:[37 TO 38]"},}; return Arrays.asList(scannedClasses); } @BeforeClass public static void init() throws IOException, ParseException { StandardAnalyzer analyzer = new StandardAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(analyzer); config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); try (IndexWriter writer = new IndexWriter(index, config)) { final Document doc = new Document(); doc.add(new TextField("TextField", "Client", Field.Store.YES)); doc.add(new StringField("StringField", "Stephane", Field.Store.YES)); doc.add(new LongPoint("LongPoint", 17)); doc.add(new BigIntegerPoint("BigIntegerPoint", new BigInteger("21"))); doc.add(new DoublePoint("DoublePoint", new Double(37.7))); doc.add(new StringField("Date", "20200615", Field.Store.YES)); writer.addDocument(doc); writer.commit(); } } private void query(Directory index, QueryParser multiFieldQueryParser, String query) throws ParseException, IOException { System.out.println("query = " + query); Query q = multiFieldQueryParser.parse(query); int hitsPerPage = 10; IndexReader reader = DirectoryReader.open(index); IndexSearcher searcher = new IndexSearcher(reader); TopDocs docs = searcher.search(q, hitsPerPage); ScoreDoc[] hits = docs.scoreDocs; Assert.assertEquals(query, 1, hits.length); } @Test public void testString() throws IOException, ParseException { StandardAnalyzer analyzer = new StandardAnalyzer(); final QueryParser multiFieldQueryParser = new MultiFieldQueryParser(new String[]{"longPoint", "Entity"}, analyzer); query(index, multiFieldQueryParser, query); } }