Package: tinysearch
TinySearchTinySearch is a tiny one-phase search engine. It is extremely easy to
use and works well with simple lists where the query may not match the
document text exactly.This is a minimal search engine. You don't need to run separate, big
instances of search engine when your use case is a few hundreds or
thousands small documents.ExampleInput documents:"Goldilocks and the Three Bears"
"Fuzzy Wuzzy"
"The Bear Went Over The Mountain"
"We're Going on a Bear Hunt"
"Brown Bear, Brown Bear, What Do You See?"Search query:bearResults (ordered by best match):"Brown Bear, Brown Bear, What Do You See?"
"Goldilocks and the Three Bears"
"The Bear Went Over The Mountain"
"We're Going on a Bear Hunt"How to usefromtinysearch.searchimportSearchdocs=["Goldilocks and the Three Bears","Fuzzy Wuzzy","The Bear Went Over The Mountain","We're Going on a Bear Hunt","Brown Bear, Brown Bear, What Do You See?",]query="bear"s=Search(docs,query)# How many results?print(s.results.count)# What is the top result?print(s.results.matches[0].doc)# Print all matches. Best results are at the top.formins.results.matches:print(m.doc)Pass your own analyzerWhentinysearch.analyzer.SimpleEnglishAnalyzerdoes not satisfy your
needs, you can write your own analyzer and pass it to theSearchobject.An analyzer inherits fromtinysearch.analyzer.base.Analyzer. It only
need to implementanalyzemethod. Theanalyzemethod accepts a string
representing the document on the input, and returns a list of strings
representing tokens (terms). Everything that you need to make it happen
can be implemented there. See the docstring of theAnalyzerbase class.You can then pass your analyzer toSearch:my_analyzer=MyOwnAnalyzer()s=Search(docs,query,analyzer=my_analyzer)print(s.results.count)Under the hoodWhen you pass documents to theSearchobject, each document is
tokenized and transformed for easier search. The same process is
applied to the query.Then each document is scored using the TF-IDF algorithm to find the
best match, and matches are returned sorted to the user. The best match
is at the top.PerformancePerformance is important since search engines typically respond to
user queries, so it should generate results in a few seconds at most.
More than that would appear as a significant delay.The numbers below are dependent on the running machine, so they are
just indicative.gantt
title Search time for different dataset sizes [s]
dateFormat X
axisFormat %s

section 100
0.0, terms=1 : 0, 0.0s
0.0, terms=2 : 0, 0.0s
0.0, terms=3 : 0, 0.0s

section 1000
0.3, terms=1 : 0, 0.3s
0.2, terms=2 : 0, 0.2s
0.3, terms=3 : 0, 0.3s

section 10000
2.7, terms=1 : 0, 2.7s
2.7, terms=2 : 0, 2.7s
2.7, terms=3 : 0, 2.7s

section 52478
15.1, terms=1 : 0, 15.6s
15.4, terms=2 : 0, 15.1s
15.6, terms=3 : 0, 15.2sDatasets of around 1000 entries might generate reasonable search times,
which is the intended use case for TinySearch. Still, there is probably
room for improvement.Can we make it faster?Most time is spent in analyzer, so improving performance means
improving processing time of the analyzer. The defaultSimpleEnglishAnalyzerhas already been highly optimized.The next step to consider is to split the search into two phases:
indexing and searching. Since analyzer needs to process every document,
indexing can happen earlier in the process execution and searching when
the user requests it. This has an additional benefit of indexing once
and searching multiple times.fromtinysearch.indeximportIndexfromtinysearch.searchimportSearchi=Index(docs)# ...later...s=Search(i,query)print(s.results.matches[0])LicenseSee LICENSE.