fixed search to work correctly

This commit is contained in:
Oleg Belyaev 2025-03-24 00:34:36 +03:00
parent 7b35bf2e83
commit ad87bf327e
2 changed files with 34 additions and 30 deletions

View file

@ -282,25 +282,21 @@ declare function abv-m:mark-element($doc as document-node(), $path as xs:string)
declare function abv-m:search($db-lang as xs:string, declare function abv-m:search($db-lang as xs:string,
$type as xs:string, $type as xs:string,
$query as xs:string) { $query as xs:string) {
let $pexpr := string-join( let $db-name := `abaevdict_{$db-lang}`
('declare namespace tei = "http://www.tei-c.org/ns/1.0";', let $docs := collection(`{$db-name}/xml`)
switch($type) let $hits := switch($type)
case "full" return "//text()" case "full" return $docs//text()[. contains text {$query}]
case "form" return "/tei:entry[1]/tei:form/tei:orth" case "form" return $docs/tei:entry[1]/tei:form[./tei:orth contains text {$query}]
case "sense" return "/tei:entry[1]/tei:sense" case "sense" return $docs/tei:entry[1]//tei:sense[./tei:cit[@type='translationEquivalent'] contains text {$query}]
case "example" return "/tei:entry[1]//tei:cit[@type='example']/tei:quote" case "example" return $docs/tei:entry[1]//tei:cit[@type='example']/tei:quote[. contains text {$query}]
case "translation" return "/tei:entry[1]//tei:cit[@type='translation']" case "translation" return $docs/tei:entry[1]//tei:cit[@type='translation' and . contains text {$query}]
case "mentioned" return "/tei:entry[1]//tei:mentioned/(tei:m|tei:w|tei:phr|tei:s)" case "mentioned" return $docs/tei:entry[1]//tei:mentioned/(tei:m|tei:w|tei:phr|tei:s)[. contains text {$query}]
case "gloss" return "tei:entry[1]//tei:gloss" case "gloss" return $docs/tei:entry[1]//tei:gloss[. contains text {$query}]
case "etym" return "tei:entry[1]/tei:etym[1]//text()" case "etym" return $docs/tei:entry[1]/tei:etym[1][. contains text {$query}]
default return "//text()") default return $docs//text()[. contains text {$query}]
) return array:build(for $hit in $hits
return array{for $doc in collection(`abaevdict_{$db-lang}/xml`) let $entry-id := db:get($db-name,db:path($hit))/tei:entry[1]/string(@xml:id)
let $hits := for $node in xquery:eval($pexpr, {'': $doc}) group by $entry-id
where $node contains text {$query} order by abv-m:sortKey(abv-m:entry-form-by-id($entry-id))
return path($node) return {'entry': $entry-id, 'nodes': array:build(for $node in $hit return path($node))})
where count($hits) > 0
order by abv-m:sortKey($doc/tei:entry[1]/tei:form[1]/tei:orth[1])
return {'entry_id': string($doc/tei:entry[1]/@xml:id),
'path': array:build($hits)}}
}; };

View file

@ -174,6 +174,10 @@ declare %rest:path("/favicon.ico")
(: ======================= SEARCH ENTRYPOINT ===================== :) (: ======================= SEARCH ENTRYPOINT ===================== :)
(: =============================================================== :) (: =============================================================== :)
declare function page:entry-for-node($lang as xs:string, $node as node()) {
db:get(`abaevdict_{$lang}`,db:path($node))/tei:entry[1]/string(@xml:id)
};
declare %rest:path("/search/{$path=.+}") declare %rest:path("/search/{$path=.+}")
%rest:GET %rest:GET
function page:search-default($path as xs:string) { function page:search-default($path as xs:string) {
@ -191,10 +195,11 @@ declare %rest:path("{$lang}/search/new")
let $r2 := session:set('searchQuery', $searchQuery) let $r2 := session:set('searchQuery', $searchQuery)
let $r3 := session:set('searchN', 1) let $r3 := session:set('searchN', 1)
let $r4 := session:set('searchData', $sd) let $r4 := session:set('searchData', $sd)
return if(array:size($sd) > 0) then return
if(array:size($sd) > 0) then
page:by-id($lang, page:by-id($lang,
$sd(1)('entry_id'), $sd(1)('entry'),
string-join($sd(1)('path')?*,"|")) string-join($sd(1)('nodes'),'|'))
else else
web:redirect('../search/clear') web:redirect('../search/clear')
}; };
@ -206,7 +211,7 @@ declare %rest:path("{$lang}/search/next")
let $n as xs:integer := session:get('searchN')+1 let $n as xs:integer := session:get('searchN')+1
let $r1 := session:set('searchN', $n) let $r1 := session:set('searchN', $n)
let $sd := session:get('searchData') let $sd := session:get('searchData')
return page:by-id($lang, $sd($n)('entry_id'), string-join($sd($n)('path')?*,"|")) return page:by-id($lang, $sd($n)('entry'), string-join($sd($n)('nodes'),'|'))
}; };
declare %rest:path("{$lang}/search/prev") declare %rest:path("{$lang}/search/prev")
@ -216,7 +221,8 @@ declare %rest:path("{$lang}/search/prev")
let $n as xs:integer := session:get('searchN')-1 let $n as xs:integer := session:get('searchN')-1
let $r1 := session:set('searchN', $n) let $r1 := session:set('searchN', $n)
let $sd := session:get('searchData') let $sd := session:get('searchData')
return page:by-id($lang, $sd($n)('entry_id'), string-join($sd($n)('path')?*,"|")) let $node := db:get-id(`abaevdict_{$lang}`,$sd[$n])
return page:by-id($lang, $sd($n)('entry'), string-join($sd($n)('nodes'),'|'))
}; };
declare %rest:path("{$lang}/search/position") declare %rest:path("{$lang}/search/position")
@ -226,7 +232,8 @@ declare %rest:path("{$lang}/search/position")
function page:search-position($lang, $p as xs:integer) { function page:search-position($lang, $p as xs:integer) {
let $r1 := session:set('searchN', $p) let $r1 := session:set('searchN', $p)
let $sd := session:get('searchData') let $sd := session:get('searchData')
return page:by-id($lang, $sd($p)('entry_id'), string-join($sd($p)('path')?*,"|")) let $node := db:get-id(`abaevdict_{$lang}`,$sd[$p])
return page:by-id($lang, $sd($p)('entry'), string-join($sd($p)('nodes'),'|'))
}; };
declare %rest:path("{$lang}/search/clear") declare %rest:path("{$lang}/search/clear")
@ -584,13 +591,14 @@ declare %rest:path("{$lang}/dict")
<td> <td>
<a href="./search/position?p={$pos}"> <a href="./search/position?p={$pos}">
{ {
abv-m:entry-form-by-id($res('entry_id')) abv-m:entry-form-by-id($res('entry'))
} }
</a> </a>
</td> </td>
<td> <td>
{ {
xquery:eval($res('path')(1), {'': doc(`abaevdict_{$lang}/xml/{$res('entry_id')}.xml`)}) xquery:eval($res('nodes')(1),
{'': doc(`abaevdict_{$lang}/xml/{$res('entry')}.xml`)})
} }
</td> </td>
</tr> </tr>