Add search algo docs

- Relates #579
This commit is contained in:
Janne Valkealahti
2022-12-06 09:00:24 +00:00
parent 74c04a4964
commit e5570ae62c
4 changed files with 127 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
[#appendix-tech-intro-searchalgorithm]
=== Search Algorithms
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
`SearchMatch` is an interface to match _text_ with a _pattern_. Match
results are in a returned value `SearchMatchResult`. Match result
contains info about match positions and overall score of a match.
https://github.com/junegunn/fzf[fzf].
==== Implementations
*FuzzyMatchV2Search*
Port of _fzf FuzzyMatchV2Search_ algorithm. Does a fast fuzzy search and is good
quickly finding paths.
*ExactMatchNaive*
Port of _fzf ExactMatchNaive_ algorithm. Simple exact match works more accurately
if you know what to search.
==== SearchMatch
Algorithms and default syntax are hidden inside package protected classes
as we don't want to fully open these until we know API's are good to go
for longer support. You need to construct `SearchMatch` via its
build-in builder.
====
[source, java, indent=0]
----
include::{snippets}/SearchAlgorithmsSnippets.java[tag=builder]
----
====
It's possible to configure _case sensitivity_, on what _direction_ search
happens or if text should be _normilized_ before search happens. Normalization
is handy when different languages have sligh variation for same type
of characters.
Search algorithm is selected based on a search syntax shown in
below table.
.Search syntax
|===
|Token |Match type |Description
|`hell`
|fuzzy-match
|Items that match `hello`
|`'stuff`
|exact-match
|Items that include `stuff`
|===
==== Examples
====
[source, java, indent=0]
----
include::{snippets}/SearchAlgorithmsSnippets.java[tag=simple]
----
====

View File

@@ -15,3 +15,5 @@ include::appendices-techical-intro-commandcontext.adoc[]
include::appendices-techical-intro-commandcatalog.adoc[]
include::appendices-techical-intro-theming.adoc[]
include::appendices-techical-intro-searchalgorithm.adoc[]

View File

@@ -13,6 +13,9 @@ include::{snippets}/UiComponentSnippets.java[tag=snippet9]
----
====
NOTE: Logic for search is passed as is into algorithms documented
in <<appendix-tech-intro-searchalgorithm>>.
The following image shows typical output from a path search component:
image::images/component-path-search-1.svg[text input]

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.docs;
import org.springframework.shell.support.search.SearchMatch;
import org.springframework.shell.support.search.SearchMatchResult;
class SearchAlgorithmsSnippets {
@SuppressWarnings("unused")
void dump1() {
// tag::builder[]
SearchMatch searchMatch = SearchMatch.builder()
.caseSensitive(false)
.normalize(false)
.forward(true)
.build();
// end::builder[]
}
void dump2() {
// tag::simple[]
SearchMatch searchMatch = SearchMatch.builder()
.caseSensitive(false)
.normalize(false)
.forward(true)
.build();
SearchMatchResult result = searchMatch.match("foo bar baz", "fbb");
result.getStart();
// 0 - start position inclusive
result.getEnd();
// 9 - end position exclusive
result.getPositions();
// 0,4,8 - positions, inclusive
result.getScore();
// 112 - score
result.getAlgorithm();
// FuzzyMatchV2SearchMatchAlgorithm - resolved algo
// end::simple[]
}
}