query
stringLookup the index for anything matching query
and return limit
results.
The algorithm used is the following: 1- lookup by name prefix 2- lookup by full_name prefix 3- loopup by levenshtein distance
At each step if the limit
is reached, the algorithm stops and returns the results.
# Search mentities based on a `query` string
#
# Lookup the index for anything matching `query` and return `limit` results.
#
# The algorithm used is the following:
# 1- lookup by name prefix
# 2- lookup by full_name prefix
# 3- loopup by levenshtein distance
#
# At each step if the `limit` is reached, the algorithm stops and returns the results.
fun find(query: String, limit: nullable Int, filter: nullable ModelFilter): Array[MEntity] do
# Find, lookup by name prefix
var matches = index.find_by_name_prefix(query, filter).uniq.
sort(lname_sorter, name_sorter, kind_sorter)
if limit != null and matches.length >= limit then
return matches.limit(limit).rerank.sort(vis_sorter, score_sorter).mentities
end
matches = matches.rerank.sort(vis_sorter, score_sorter)
# If limit not reached, lookup by full_name prefix
var malus = matches.length
var full_matches = new IndexMatches
for match in index.find_by_full_name_prefix(query, filter).
sort(kind_sorter, lfname_sorter, fname_sorter) do
match.score += malus
full_matches.add match
end
matches = matches.uniq
if limit != null and matches.length + full_matches.length >= limit then
matches.add_all full_matches
matches = matches.uniq.limit(limit).rerank.sort(vis_sorter, score_sorter)
return matches.mentities
end
# If limit not reached, lookup by similarity
malus = matches.length
var sim_matches = new IndexMatches
for match in index.find_by_similarity(query, filter).sort(score_sorter, kind_sorter, lname_sorter, name_sorter) do
match.score += malus
sim_matches.add match
end
matches.add_all sim_matches
matches = matches.uniq
if limit != null then matches = matches.limit(limit)
return matches.rerank.sort(vis_sorter, score_sorter).mentities
end
src/model/model_index.nit:179,2--224,4