Search now apply to all fields by default
This commit is contained in:
@@ -30,6 +30,12 @@ class LuceneDsl {
|
||||
fun addBooleanClause(booleanDsl: BooleanExpression) {
|
||||
clauses.add(booleanDsl)
|
||||
}
|
||||
|
||||
infix fun List<String>.anyMatch(query: String?) {
|
||||
map { BooleanExpression(it, query) }.forEach {
|
||||
addBooleanClause(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun LuceneDsl.or(booleanExpression: () -> BooleanExpression) {
|
||||
|
||||
@@ -85,6 +85,7 @@ class NoteSearcherImpl(basePath: Path = Path.of("/tmp", "lucene")) : NoteSearche
|
||||
or { titleField eq terms.title }
|
||||
or { tagsField eq terms.tag }
|
||||
or { contentField eq terms.content }
|
||||
listOf(titleField, tagsField, contentField) anyMatch terms.all
|
||||
}.map(Document::toNoteMeta)
|
||||
|
||||
override fun dropIndex(userId: Int) = rmdir(File(baseFile, userId.toString()).toPath())
|
||||
|
||||
@@ -25,7 +25,7 @@ internal class NoteSearcherImplTest {
|
||||
content: String = "",
|
||||
uuid: UUID = UUID.randomUUID(),
|
||||
): PersistedNote {
|
||||
val note = PersistedNote(NoteMetadata(title, tags), markdown = "", content, LocalDateTime.now(), uuid)
|
||||
val note = PersistedNote(NoteMetadata(title, tags), markdown = "", content, LocalDateTime.MIN, uuid)
|
||||
searcher.indexNote(1, note)
|
||||
return note
|
||||
}
|
||||
@@ -34,7 +34,8 @@ internal class NoteSearcherImplTest {
|
||||
title: String? = null,
|
||||
tag: String? = null,
|
||||
content: String? = null,
|
||||
): List<PersistedNoteMetadata> = searcher.search(1, SearchTerms(title, tag, content))
|
||||
all: String? = null,
|
||||
): List<PersistedNoteMetadata> = searcher.search(1, SearchTerms(title, tag, content, all))
|
||||
|
||||
@BeforeEach
|
||||
@AfterAll
|
||||
@@ -146,6 +147,36 @@ internal class NoteSearcherImplTest {
|
||||
.hasSize(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `search all`() {
|
||||
@Language("html")
|
||||
val content =
|
||||
"""
|
||||
<div>
|
||||
<h1 class="title">Apache Lucene Core</h1>
|
||||
<p>Apache Lucene<span style="vertical-align: super; font-size: xx-small">TM</span> is a
|
||||
high-performance, full-featured text search engine library written entirely in Java.
|
||||
It is a technology suitable for nearly any application that requires full-text search,
|
||||
especially cross-platform.</p>
|
||||
<p>Apache Lucene is an open source project available for free download. Please use the
|
||||
links on the right to access Lucene.</p>
|
||||
<h1 id="lucenetm-features">Lucene<span style="vertical-align: super; font-size: xx-small">TM</span> Features</h1>
|
||||
<p>Lucene offers powerful features through a simple API:</p>
|
||||
<h2 id="scalable-high-performance-indexing">Scalable, High-Performance Indexing</h2>
|
||||
<ul>
|
||||
<li>over <a href="http://home.apache.org/~mikemccand/lucenebench/indexing.html">150GB/hour on modern hardware</a></li>
|
||||
<li>small RAM requirements -- only 1MB heap</li>
|
||||
<li>incremental indexing as fast as batch indexing</li>
|
||||
<li>index size roughly 20-30% the size of text indexed</li>
|
||||
</ul>
|
||||
""".trimIndent()
|
||||
|
||||
index("first", content = content, tags = listOf("abc"))
|
||||
|
||||
assertThat(search(all = "abc", title = "first"))
|
||||
.hasSize(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `delete index`() {
|
||||
val uuid = index("first").uuid
|
||||
|
||||
Reference in New Issue
Block a user