@@ -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]
|
||||
----
|
||||
====
|
||||
@@ -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[]
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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[]
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user