so as in the title when I'm trying to search for a query i get an error
Exception in thread "main" java.lang.IllegalStateException: field "content" was indexed without position data; cannot run PhraseQuery (phrase=content:"to be not"~1) at org.apache.lucene.search.PhraseQuery$1.getPhraseMatcher(PhraseQuery.java:497) at org.apache.lucene.search.PhraseWeight.scorer(PhraseWeight.java:64) at org.apache.lucene.search.Weight.bulkScorer(Weight.java:166) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:731) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:655) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:649) at org.apache.lucene.search.IndexSearcher.searchAfter(IndexSearcher.java:487) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:501) at ProximitySearch.main(ProximitySearch.java:81)
Here is my code:
public static void main(String[] args) throws IOException, ParseException {
Analyzer analyzer = new StandardAnalyzer();
List<KeyValuePairs> listOfDocs = new LinkedList<>();
KeyValuePairs file1 = new KeyValuePairs("file1", "to be or not to be that is the question");
KeyValuePairs file2 = new KeyValuePairs("file2", "make a long story short");
KeyValuePairs file3 = new KeyValuePairs("file3", "see eye to eye");
listOfDocs.add(file1);
listOfDocs.add(file2);
listOfDocs.add(file3);
Path indexPath = Files.createTempDirectory("tempIndex");
Directory directory = FSDirectory.open(indexPath);
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter iwriter = new IndexWriter(directory, config);
for (KeyValuePairs listOfDoc : listOfDocs) {
Document doc = new Document();
String text = listOfDoc.getKey();
System.out.println(text);
String title = listOfDoc.getValue();
doc.add(new StringField("content", text, Field.Store.YES));
doc.add(new Field("title", title, TextField.TYPE_STORED));
iwriter.addDocument(doc);
}
iwriter.close();
// Now search the index:
DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
// Parse a simple query that searches for "something that u want to search":
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.parse("\"to be not\"~1");
ScoreDoc[] hits = isearcher.search(query, 10).scoreDocs;
System.out.println(Arrays.toString(Arrays.stream(hits).toArray()));
System.out.println("Search terms found in :: " + hits.length + " files");
ireader.close();
directory.close();
IOUtils.rm(indexPath);
}
I dont know what am i doing wrong.