Dynamic combination search on specific path in BaseX

Monday, 14 November 2022 · 5 messages · last reply 15 Nov 2022
DS
DK Singh14 Nov 2022 17:15 #
HI I want to perform combination (AND OR) search on the basis of the provided parameter by the user, where user will select the field name e.g.(title, isbn, author) and their value from front-end so here field name will be the parameter for the BaseX. Currently i am asking front-end person the generate below combination query ( (title = "United States" or isbn = "2345371242192") and author ="Jhon" ) Now the all parameter value will be compared to specific XPATH , like below.
let $Author := 'David Hare'
let $title := "United States "
let $isbn:= '2345371242192 '

return
db:open('test')/*[((
normalize-space(string-join((*:info/*:author)[1]/*:personname//text())
) = $Author or $title = *:info/*:title ) and
$isbn="*:info/isban")]/base-uri()
In the above code XPATH is hardcoded, but I want it to concatenate on the fly once string query comes front-end then it will be passed to Basex . Is there any other way to achieve this? or how should i ask combinations query from front-end so that i could achieve this. Thanks Dharmendra Kumar Singh
CG
Christian Grün15 Nov 2022 14:13 #
Hi Dharmendra, Thanks for writing to the list.
Quoted text · DK Singh · 4 lines
> I want to perform combination (AND OR) search on the basis of the provided parameter by the user, where user will select the field name e.g.(title, isbn, author) and their value from front-end so here field name will be the parameter for the BaseX. > > Currently i am asking front-end person the generate below combination query > ( (title = "United States" or isbn = "2345371242192") and author ="Jhon" )
Could you please give us more details on how this string is generated by the frontend? Next, your XPath expression differs in many aspects from the frontend representation. Some examples: • It seems that "author" needs to be mapped to: normalize-space(string-join((*:info/*:author)[1]/*:personname//text()) • "isbn" was mapped to $isbn="*:info/isban" (shouldn’t it be *:info/*:isbn, and without quotes?) I assume that it’s not sufficient to map each frontend field to an element name? Thanks in advance, Christian
DS
DK Singh15 Nov 2022 14:48 #
Thanks Christian for your kind reply Currently front-end not generating the search combination ( (title = "United States" or isbn = "2345371242192") and author ="Jhon" ) just i am thinking, if i could achieve this way, so that i could ask fron-end person to send this way. As per implementation, the front-end is not going to send XPATH for any field, I am thinking to generate XPATH on the fly and concatenating it with the search query which will be sent from front-end, is it possible? The XPATH will be on assumption as per field e.g.(if user select the field author then i have to look on XPATH (*:info/*:author)[1]/*:personname) For this search ( (title = "United States" or isbn = "2345371242192") and author ="Jhon" ) how should i ask to generate the query from front-end so that combination search can be achieved. Note: I have to create REST API for this service. Thanks
Quoted text · Christian Grün · 33 lines
On Tue, Nov 15, 2022 at 6:43 PM Christian Grün <christian.gruen at gmail.com> wrote: > Hi Dharmendra, > > Thanks for writing to the list. > > > I want to perform combination (AND OR) search on the basis of the > provided parameter by the user, where user will select the field name > e.g.(title, isbn, author) and their value from front-end so here field name > will be the parameter for the BaseX. > > > > Currently i am asking front-end person the generate below combination > query > > ( (title = "United States" or isbn = "2345371242192") and author ="Jhon" > ) > > Could you please give us more details on how this string is generated > by the frontend? > > Next, your XPath expression differs in many aspects from the frontend > representation. Some examples: > > • It seems that "author" needs to be mapped to: > normalize-space(string-join((*:info/*:author)[1]/*:personname//text()) > • "isbn" was mapped to $isbn="*:info/isban" (shouldn’t it be > *:info/*:isbn, and without quotes?) > > I assume that it’s not sufficient to map each frontend field to an element > name? > > Thanks in advance, > Christian >
CG
Christian Grün15 Nov 2022 15:05 #
Quoted text · DK Singh · 1 line
> Currently front-end not generating the search combination ( (title = "United States" or isbn = "2345371242192") and author ="Jhon" ) just i am thinking, if i could achieve this way, so that i could ask fron-end person to send this way.
If your frontend is JavaScript-based, it may be easier and cleaner to create a JSON object that contains the search fields. Something like:
{
  "AND": {
    "OR": {
      "isbn" : "2345371242192",
      "title": "United States"
    },
    "author": "Jhon"
  }
}
If you POST this snippet as 'application/json' to a RESTXQ backend, the resulting XML structure will be:
<json type="object">
  <AND type="object">
    <OR type="object">
      <isbn>2345371242192</isbn>
      <title>United States</title>
    </OR>
    <author>Jhon</author>
  </AND>
</json>
You can then parse this XML fragment with XQuery to either: a) recursively build a query string, which will be evaluated via xquery:eval (as proposed on StackOverflow), or b) process the query tree recursively for each element of your document. The first variant will probably be more efficient. You’ll probably need to invest some time to get this realized (I don’t think there’s a solution that can be used out of the box). Best, Christian
DS
DK Singh15 Nov 2022 15:15 #
Hi Christian, Thanks for your suggestion
Quoted text · Christian Grün · 1 line
> a) recursively build a query string, which will be evaluated via
xquery:eval (as proposed on StackOverflow) on this solution i could work if i get the expected result then will update on stackoverflow Thanks for your time Dharmendra kumar Singh
Quoted text · Christian Grün · 47 lines
On Tue, Nov 15, 2022 at 7:35 PM Christian Grün <christian.gruen at gmail.com> wrote: > > Currently front-end not generating the search combination ( (title = > "United States" or isbn = "2345371242192") and author ="Jhon" ) just i am > thinking, if i could achieve this way, so that i could ask fron-end person > to send this way. > > If your frontend is JavaScript-based, it may be easier and cleaner to > create a JSON object that contains the search fields. Something like: > > { > "AND": { > "OR": { > "isbn" : "2345371242192", > "title": "United States" > }, > "author": "Jhon" > } > } > > If you POST this snippet as 'application/json' to a RESTXQ backend, > the resulting XML structure will be: > > <json type="object"> > <AND type="object"> > <OR type="object"> > <isbn>2345371242192</isbn> > <title>United States</title> > </OR> > <author>Jhon</author> > </AND> > </json> > > You can then parse this XML fragment with XQuery to either: > > a) recursively build a query string, which will be evaluated via > xquery:eval (as proposed on StackOverflow), or > b) process the query tree recursively for each element of your document. > > The first variant will probably be more efficient. > > You’ll probably need to invest some time to get this realized (I don’t > think there’s a solution that can be used out of the box). > > Best, > Christian >

⚡Generated with XQuery