package Ġnet . d ank ito . documents . search . index Ċ Ċ import Ġorg . apache . lucene . document . Long Point Ċ import Ġorg . apache . lucene . index . Term Ċ import Ġorg . apache . lucene . query parser . classic . QueryParser Ċ import Ġorg . apache . lucene . search .* Ċ import Ġjava . util .* ĊĊ Ċ open Ġclass ĠQuery Builder Ġ{ ĊĊ ĉ open Ġfun Ġall Documents (): ĠQuery Ġ{ Ċĉ ĉ return ĠMatch All Docs Query () Ċ ĉ } ĊĊĊ ĉ open Ġfun Ġcreate Queries For Single Terms ( search Term : ĠString , Ġsingle Term QueryBuilder : Ġ( single Term : ĠString ) Ġ-> ĠList < Query >): ĠQuery Ġ{ Ċĉ ĉ if Ġ( search Term . isBlank ()) Ġ{ Ċĉĉ ĉ return Ġall Documents () Ċĉ ĉ } ĊĊĉ ĉ val Ġsingle Terms Ġ= Ġsearch Term . split (" Ġ"). filter Ġ{ Ġit . isNotBlank () Ġ} ĊĊĉ ĉ val Ġquery Builder Ġ= ĠBoolean Query . Builder () ĊĊĉ ĉ single Terms . forEach Ġ{ Ġsingle Term Ġ-> Ċĉĉ ĉ query Builder . add ( createQuery For Single Term ( single Term , Ġsingle Term QueryBuilder ), ĠBoolean Clause . Occ ur . MUST ) Ċĉ ĉ } ĊĊĉ ĉ return Ġquery Builder . build () Ċ ĉ } ĊĊ ĉ protected Ġopen Ġfun Ġcreate Query For Single Term ( single Term : ĠString , Ġsingle Term QueryBuilder : Ġ( single Term : ĠString ) Ġ-> ĠList < Query >): ĠQuery Ġ{ Ċĉ ĉ val Ġsingle Term Queries Ġ= Ġsingle Term QueryBuilder ( single Term ) ĊĊĉ ĉ val Ġquery Builder Ġ= ĠBoolean Query . Builder () ĊĊĉ ĉ single Term Queries . forEach Ġ{ Ċĉĉ ĉ query Builder . add ( it , ĠBoolean Clause . Occ ur . SHO ULD ) Ċĉ ĉ } ĊĊĉ ĉ return Ġquery Builder . build () Ċ ĉ } ĊĊĊ ĉ /* ĉ ĉ String Ġqueries ĉ ĉ */ ĊĊ ĉ open Ġfun Ġfull text Query ( fieldName : ĠString , Ġsearch Term : ĠString ): ĠQuery Ġ{ Ċĉ ĉ return ĠPrefix Query ( Term ( fieldName , Ġadjust Search Term ( search Term , Ġfalse ))) Ċ ĉ } ĊĊ ĉ @ Jvm Over loads Ċ ĉ open Ġfun Ġstart sWith ( fieldName : ĠString , Ġsearch Term : ĠString , Ġcase Ins ensitive : ĠBoolean Ġ= Ġtrue ): ĠQuery Ġ{ Ċĉ ĉ return Ġwildcard Query ( fieldName , Ġsearch Term , Ġcase Ins ensitive , Ġtrue , Ġfalse ) Ċ ĉ } ĊĊ ĉ @ Jvm Over loads Ċ ĉ open Ġfun Ġcontains ( fieldName : ĠString , Ġsearch Term : ĠString , Ġcase Ins ensitive : ĠBoolean Ġ= Ġtrue ): ĠQuery Ġ{ Ċĉ ĉ return Ġwildcard Query ( fieldName , Ġsearch Term , Ġcase Ins ensitive ) Ċ ĉ } ĊĊ ĉ @ Jvm Over loads Ċ ĉ open Ġfun Ġend sWith ( fieldName : ĠString , Ġsearch Term : ĠString , Ġcase Ins ensitive : ĠBoolean Ġ= Ġtrue ): ĠQuery Ġ{ Ċĉ ĉ return Ġwildcard Query ( fieldName , Ġsearch Term , Ġcase Ins ensitive , Ġfalse , Ġtrue ) Ċ ĉ } ĊĊ ĉ @ Jvm Over loads Ċ ĉ open Ġfun Ġwildcard Query ( fieldName : ĠString , Ġsearch Term : ĠString , Ġcase Ins ensitive : ĠBoolean Ġ= Ġtrue ): ĠQuery Ġ{ Ċĉ ĉ return Ġwildcard Query ( fieldName , Ġsearch Term , Ġcase Ins ensitive , Ġtrue , Ġtrue ) Ċ ĉ } ĊĊ ĉ open Ġfun Ġwildcard Query ( fieldName : ĠString , Ġsearch Term : ĠString , Ġcase Ins ensitive : ĠBoolean Ġ, ĊĉĉĉĉĉĉĠĠ Ġprefix Wildcard : ĠBoolean , Ġsuffix Wildcard : ĠBoolean ): ĠQuery Ġ{ Ċĉ ĉ val Ġadjusted Search Term Ġ= Ġadjust Search Term For Wildcard Query ( search Term , Ġcase Ins ensitive , Ġprefix Wildcard , Ġsuffix Wildcard ) ĊĊĉ ĉ return ĠW ildcard Query ( Term ( fieldName , Ġadjusted Search Term )) Ċ ĉ } ĊĊ ĉ protected Ġopen Ġfun Ġadjust Search Term For Wildcard Query ( search Term : ĠString , Ġcase Ins ensitive : ĠBoolean , Ċĉĉĉĉĉĉĉĉĉĉĉĉĉ ĉ prefix Wildcard : ĠBoolean , Ġsuffix Wildcard : ĠBoolean ): ĠString Ġ{ Ċĉ ĉ val Ġadjusted Search Term Ġ= Ġadjust Search Term ( search Term , Ġcase Ins ensitive ) ĊĊĉ ĉ if Ġ( prefix Wildcard Ġ&& Ġsuffix Wildcard ) Ġ{ Ċĉĉ ĉ return Ġ"* $ adjusted Search Term *" Ċĉ ĉ } Ċĉ ĉ else Ġif Ġ( prefix Wildcard ) Ġ{ Ċĉĉ ĉ return Ġ"$ adjusted Search Term *" Ċĉ ĉ } Ċĉ ĉ else Ġ{ Ċĉĉ ĉ return Ġ"* $ adjusted Search Term " Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ protected Ġopen Ġfun Ġadjust Search Term ( search Term : ĠString , Ġcase Ins ensitive : ĠBoolean ): ĠString Ġ{ Ċĉ ĉ val Ġadjusted Search Term Ġ= ĠQuery Parser . escape ( search Term ) ĊĊĉ ĉ return Ġif Ġ( case Ins ensitive ) Ġ{ Ċĉĉ ĉ adjusted Search Term . toLowerCase () Ċĉ ĉ } Ċĉ ĉ else Ġ{ Ċĉĉ ĉ adjusted Search Term Ċĉ ĉ } Ċ ĉ } ĊĊĊ ĉ /* ĉ ĉ Date Ġqueries ĉ ĉ */ ĊĊ ĉ open Ġfun Ġexact Date Query ( fieldName : ĠString , Ġdate : ĠDate ): ĠQuery Ġ{ Ċĉ ĉ return ĠLong Point . new Exact Query ( fieldName , Ġdate . time ) Ċ ĉ } ĊĊ ĉ open Ġfun Ġafter Date Query ( fieldName : ĠString , Ġdate After This Inclusive : ĠDate ): ĠQuery Ġ{ Ċĉ ĉ return ĠLong Point . new Range Query ( fieldName , Ġdate After This Inclusive . time , ĠLong . MAX _ VALUE ) Ċ ĉ } ĊĊ ĉ open Ġfun Ġbefore Date Query ( fieldName : ĠString , Ġdate Before This Inclusive : ĠDate ): ĠQuery Ġ{ Ċĉ ĉ return ĠLong Point . new Range Query ( fieldName , ĠLong . MIN _ VALUE , Ġdate Before This Inclusive . time ) Ċ ĉ } Ċ Ċ }