You can use this modification of Lucene 7.3.0 for searching in Android 8.0 or above: https://github.com/texophen/lucene-android
See this answer ( https://stackoverflow.com/a/76719477/22251021 ) for how to use it.
In order to use Lucene in Android, you can:
- Extends org.apache.lucene.store.BaseDirectory:
package com.texopher.gophoxes;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.apache.lucene.store.BaseDirectory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.LockFactory;
public class BucketDirectory extends BaseDirectory {
protected Bucket bucket;
public BucketDirectory(Bucket bucket, LockFactory lockFactory) {
super(lockFactory);
this.bucket = bucket;
}
@Override
public void close() throws IOException {
}
@Override
public IndexOutput createOutput(String name, IOContext context) throws IOException {
return new BucketIndexOutput(bucket, name);
}
@Override
public IndexOutput createTempOutput(String prefix, String suffix, IOContext context) throws IOException {
return new BucketIndexOutput(bucket, prefix + UUID.randomUUID().toString().replaceAll("-", "") + suffix);
}
@Override
public void deleteFile(String name) throws IOException {
try {
bucket.deleteFile(name);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public long fileLength(String name) throws IOException {
try {
return bucket.fileLength(name);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public String[] listAll() throws IOException {
String[] tag = new String[0];
try {
List<String> tmp = bucket.listFiles();
tag = new String[tmp.size()];
for (int i = 0; i < tmp.size(); i++) {
tag[i] = tmp.get(i);
}
} catch (Exception e) {
e.printStackTrace();
}
return tag;
}
@Override
public IndexInput openInput(String name, IOContext context) throws IOException {
try {
return new BucketIndexInput(bucket, name);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void rename(String source, String dest) throws IOException {
try {
bucket.rename(source, dest);;
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void sync(Collection<String> arg0) throws IOException {
}
@Override
public void syncMetaData() throws IOException {
}
}
- Use following code for searching:
Query qr = new TermQuery(new Term("code", this.md5(link)));
Bucket bk = new Bucket(this.gopherServer, this.hole, this.magic);
Directory indexDirectory = new BucketDirectory(bk, new SingleInstanceLockFactory());
IndexReader reader = DirectoryReader.open(indexDirectory);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.search(qr, 1);
ScoreDoc[] hits = docs.scoreDocs;