mirror of https://github.com/zlatinb/muwire
basic search index
parent
f377807101
commit
a8eb37458f
|
@ -0,0 +1,37 @@
|
|||
package com.muwire.core.files
|
||||
|
||||
|
||||
class SearchIndex {
|
||||
|
||||
final Map<String, Set<File>> keywords = new HashMap<>()
|
||||
|
||||
void add(File f) {
|
||||
String name = f.getName()
|
||||
name = name.replaceAll("\\."," ")
|
||||
String [] split = name.split(" ")
|
||||
split.each {
|
||||
Set<File> existing = keywords.get(it)
|
||||
if (existing == null) {
|
||||
existing = new HashSet<>()
|
||||
keywords.put(it, existing)
|
||||
}
|
||||
existing.add(f)
|
||||
}
|
||||
}
|
||||
|
||||
File[] search(List<String> terms) {
|
||||
Set<File> rv = null;
|
||||
|
||||
terms.each {
|
||||
Set<File> forWord = keywords.get it
|
||||
if (rv == null) {
|
||||
rv = forWord
|
||||
} else {
|
||||
rv.retainAll(forWord)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
rv.asList()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.muwire.core.files
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
class SearchIndexTest {
|
||||
|
||||
SearchIndex index
|
||||
|
||||
private void initIndex(List<String> entries) {
|
||||
index = new SearchIndex()
|
||||
entries.each {
|
||||
File f = new File(it)
|
||||
index.add(f)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSingleTerm() {
|
||||
initIndex(["a b.c", "d e.f"])
|
||||
|
||||
def found = index.search(["a"])
|
||||
assert found.size() == 1
|
||||
assert found.contains(new File("a b.c"))
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSingleTermOverlap() {
|
||||
initIndex(["a b.c", "c d.e"])
|
||||
|
||||
def found = index.search(["c"])
|
||||
assert found.size() == 2
|
||||
assert found.contains(new File("a b.c"))
|
||||
assert found.contains(new File("c d.e"))
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDrillDown() {
|
||||
initIndex(["a b.c", "c d.e"])
|
||||
|
||||
def found = index.search(["c", "e"])
|
||||
assert found.size() == 1
|
||||
assert found.contains(new File("c d.e"))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue