You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by "Goławski, Paweł" <Pa...@cgm.com> on 2022/05/24 10:01:09 UTC

Build Full Text Search query using SelectBuilder

Hi.

There is a simple query with text:query:
PREFIX   ex: http://www.example.org/resources#
PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema#
PREFIX text: http://jena.apache.org/text#
SELECT ?s ?lbl
WHERE {
    ?s a ex:Product ;
       text:query (rdfs:label 'printer');
       rdfs:label ?lbl
}

Now I want to build such query SelectBuilder:


final Query query = new SelectBuilder()
        .setDistinct(true)
        .addPrefixes(NS_PREFIX_MAP)
        .addVar("s")
        .addVar("lbl")
        .addWhere(makeVar("s"), "rdf:type", "ex:Product ")
        .addWhere(makeVar("lbl"), "text:query", "(rdfs:label 'printer')")
        .addWhere(makeVar("lbl"), "rdfs:label", "?lbl")
        .build();

Result is:

PREFIX   ex: http://www.example.org/resources#
PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema#
PREFIX text: http://jena.apache.org/text#
SELECT ?s ?lbl
WHERE {
    ?s rdf:type ex:Product ;
       text:query "(rdfs:label 'printer')";
       rdfs:label ?lbl
}


which is incorrect because (rdfs:label 'printer') is treated as literal, so in result it's wrapped by quotes.

Is there is a way to do it right?

Thank in advance.
Paweł

RE: Build Full Text Search query using SelectBuilder

Posted by "Goławski, Paweł" <Pa...@cgm.com>.
From: Claude Warren <cl...@xenei.com>
Sent: Monday, May 30, 2022 12:41 PM
To: users@jena.apache.org
Cc: Goławski, Paweł <Pa...@cgm.com>
Subject: Re: Build Full Text Search query using SelectBuilder
On 24/05/2022 11:01, Goławski, Paweł wrote:
PREFIX   ex: http://www.example.org/resources#<http://www.example.org/resources>
PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema#<http://www.w3.org/2000/01/rdf-schema>
PREFIX text: http://jena.apache.org/text#<http://jena.apache.org/text>
SELECT ?s ?lbl
WHERE {
    ?s a ex:Product ;
       text:query (rdfs:label 'printer');
       rdfs:label ?lbl
}



To generate the above the following should work:

        final SelectBuilder builder = new SelectBuilder();

        final Query query = builder.setDistinct(true)

                .addPrefix("ex", "http://www.example.org/resources#"<http://www.example.org/resources>)

                .addPrefix("rdf", RDF.uri).addPrefix("rdfs", RDFS.uri)

                .addPrefix("text", "http://jena.apache.org/text#"<http://jena.apache.org/text>)

                .addVar("s")

                .addVar("lbl")

                .addWhere("?s", "rdf:type", "ex:Product ")

                .addWhere("?s", "text:query", builder.list("rdfs:label", "printer"))

                .addWhere("?s", "rdfs:label", "?lbl")

                .build();



There are a couple of changes from your code:

  1.  I removed the makeVar() calls and replaced with "?var".
  2.  Used the build list() method to create the list of variables.
  3.  Added the prefixes by hand since I didn't have your variable.

The query can also be written using Vars as:

        final Var s = Converters.makeVar( "s" );

        final Var lbl = Converters.makeVar( "lbl" );

        final SelectBuilder builder = new SelectBuilder();

        final Query query = builder.setDistinct(true)

                .addPrefix("ex", "http://www.example.org/resources#"<http://www.example.org/resources>)

                .addPrefix("rdf", RDF.uri).addPrefix("rdfs", RDFS.uri)

                .addPrefix("text", "http://jena.apache.org/text#"<http://jena.apache.org/text>)

                .addVar(s)

                .addVar(lbl)

                .addWhere(s, "rdf:type", "ex:Product ")

                .addWhere(s, "text:query", builder.list("rdfs:label", "printer"))

                .addWhere(s, "rdfs:label", lbl)

                .build();



Hope this helps,

Claude





Hi Claude.

Looks that is works.

Thanks a lot 😊

Re: Build Full Text Search query using SelectBuilder

Posted by Claude Warren <cl...@xenei.com>.
On 24/05/2022 11:01, Goławski, Paweł wrote:
>
> PREFIX   ex: http://www.example.org/resources# 
> <http://www.example.org/resources#>
>
> PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema# 
> <http://www.w3.org/2000/01/rdf-schema#>
>
> PREFIX text: http://jena.apache.org/text# <http://jena.apache.org/text#>
>
> SELECT ?s ?lbl
>
> WHERE {
>
>     ?s a ex:Product ;
>
>        text:query (rdfs:label 'printer');
>
> rdfs:label ?lbl
>
> }
>

To generate the above the following should work:

         final SelectBuilder builder = new SelectBuilder();
         final Query query = builder.setDistinct(true)
                 .addPrefix("ex","http://www.example.org/resources#")
                 .addPrefix("rdf", RDF.uri).addPrefix("rdfs", RDFS.uri)
                 .addPrefix("text","http://jena.apache.org/text#")
                 .addVar("s")
                 .addVar("lbl")
                 .addWhere("?s", "rdf:type", "ex:Product ")
                 .addWhere("?s", "text:query", builder.list("rdfs:label", "printer"))
                 .addWhere("?s", "rdfs:label", "?lbl")
                 .build();

There are a couple of changes from your code:

 1. I removed the makeVar() calls and replaced with "?var".
 2. Used the build list() method to create the list of variables.
 3. Added the prefixes by hand since I didn't have your variable.

The query can also be written using Vars as:

         final Var s = Converters.makeVar( "s" );
         final Var lbl = Converters.makeVar( "lbl" );
         final SelectBuilder builder = new SelectBuilder();
         final Query query = builder.setDistinct(true)
                 .addPrefix("ex","http://www.example.org/resources#")
                 .addPrefix("rdf", RDF.uri).addPrefix("rdfs", RDFS.uri)
                 .addPrefix("text","http://jena.apache.org/text#")
                 .addVar(s)
                 .addVar(lbl)
                 .addWhere(s, "rdf:type", "ex:Product ")
                 .addWhere(s, "text:query", builder.list("rdfs:label", "printer"))
                 .addWhere(s, "rdfs:label", lbl)
                 .build();

Hope this helps,
Claude