You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Ryan Yacyshyn <ry...@gmail.com> on 2014/12/06 08:24:16 UTC

Creating a Custom Query Response Writer

Hey Everyone,

I'm a little stuck on building a custom query response writer. I want to
create a response writer similar to the one explained in the book, Taming
Text, on the TypeAheadResponseWriter. I know I need to implement the
QueryResponseWriter, but I'm not sure where to find the Solr JAR files I
need to include. Where can I find these?

Thanks,
Ryan

Re: Creating a Custom Query Response Writer

Posted by Ryan Yacyshyn <ry...@gmail.com>.
Thanks Erik. That's what I did in the end and it works great. I thought I'd
need to create a custom response to remove unnecessary fields but was able
to make the request return pretty much only what I need, even adding
omitHeader=true. I'm using the EdgeNGramFilterFactory during indexing on
the title field, and then returning the just the title on the matching set.

For example, a call to http://localhost:8983/solr/movies/suggest_movie?q=sav,
will return:

{
    "response": {
        "numFound": 2,
        "start": 0,
        "docs": [
            {
                "title": "Saving Private Ryan"
            },
            {
                "title": "Into the Breach: 'Saving Private Ryan'"
            }
        ]
    }
}

Thanks for your help!

Ryan




On Sun Dec 07 2014 at 7:14:23 AM Erik Hatcher <er...@gmail.com>
wrote:

> I wouldn’t personally do anything custom for JSON - but rather just pull
> out what you need client-side (and make the request such that it doesn’t
> return more than you need).  Doing a custom JSON format for this would
> limit your later flexibility in case you wanted to get different pieces of
> the response, possibly.   Also, if you had your own custom JSON format
> writer it could become problematic later on if Solr’s internal APIs changed
> and you needed to upgrade (or worse, maintain multiple versions).
>
>         Erik
>
> > On Dec 6, 2014, at 5:13 PM, Ryan Yacyshyn <ry...@gmail.com>
> wrote:
> >
> > Hi Erik,
> >
> > Wow that's great. Thanks for the explanation, I tried using the
> > VelocityResponseWriter with your template provided and it worked as
> > expected, makes sense!
> >
> > What if I want to return a custom JSON response back, rather than HTML
> for
> > auto-suggesting? I'm thinking about using Twitter's typeahead jQuery
> plugin
> > <https://twitter.github.io/typeahead.js/examples/#custom-templates <
> https://twitter.github.io/typeahead.js/examples/#custom-templates>> and
> > passing it JSON, and have a template on the front-end to the autosuggest.
> >
> > Ryan
> >
> >
> >
> > On Sat Dec 06 2014 at 3:41:38 AM Erik Hatcher <erik.hatcher@gmail.com
> <ma...@gmail.com>>
> > wrote:
> >
> >> Ryan - I just pulled Taming Text off my shelf and refreshed my memory of
> >> this custom response writer.
> >>
> >> While having a custom writer is a neat example, it’s unnecessary for
> that
> >> particular functionality.  Solr has a built-in templatable response
> writer,
> >> the VelocityResponseWriter.  You can see it in action for a similar
> suggest
> >> feature in Solr’s example /browse interface (type “ip” and wait a
> second in
> >> the /browse UI with the sample data indexed).  In there is a little bit
> of
> >> jQuery autocomplete plugin usage that calls back to the /terms handler,
> >> using a suggest.vm template (in conf/velocity).  The difference with the
> >> Taming Text example is that it is returns stored fields of a standard
> >> search rather than just raw terms; with a little adjustment you can get
> >> basically the same thing as TT.  Leveraging the Solr example (v4.10.2
> for
> >> me here), I created a conf/velocity/typeahead.vm:
> >>
> >>  <ul>
> >>    #foreach($doc in $response.results)
> >>      <li>$doc.name</li>
> >>    #end
> >>  </ul>
> >>
> >> (the docs in the example data have a ‘name’ field)
> >>
> >> This request  http://localhost:8983/solr/collection1/select?q=name%
> >> 3Aip*&wt=velocity&v.template=typeahead <http://localhost:8983/solr/ <
> http://localhost:8983/solr/>
> >> collection1/select?q=name:ip*&wt=velocity&v.template=typeahead> results
> >> in this response:
> >>
> >>  <ul>
> >>      <li>Belkin Mobile Power Cord for iPod w/ Dock</li>
> >>      <li>iPod & iPod Mini USB 2.0 Cable</li>
> >>      <li>Apple 60 GB iPod with Video Playback Black</li>
> >>  </ul>
> >>
> >>        Erik
> >>
> >>
> >>> On Dec 6, 2014, at 2:24 AM, Ryan Yacyshyn <ry...@gmail.com>
> >> wrote:
> >>>
> >>> Hey Everyone,
> >>>
> >>> I'm a little stuck on building a custom query response writer. I want
> to
> >>> create a response writer similar to the one explained in the book,
> Taming
> >>> Text, on the TypeAheadResponseWriter. I know I need to implement the
> >>> QueryResponseWriter, but I'm not sure where to find the Solr JAR files
> I
> >>> need to include. Where can I find these?
> >>>
> >>> Thanks,
> >>> Ryan
>
>

Re: Creating a Custom Query Response Writer

Posted by Erik Hatcher <er...@gmail.com>.
I wouldn’t personally do anything custom for JSON - but rather just pull out what you need client-side (and make the request such that it doesn’t return more than you need).  Doing a custom JSON format for this would limit your later flexibility in case you wanted to get different pieces of the response, possibly.   Also, if you had your own custom JSON format writer it could become problematic later on if Solr’s internal APIs changed and you needed to upgrade (or worse, maintain multiple versions). 

	Erik

> On Dec 6, 2014, at 5:13 PM, Ryan Yacyshyn <ry...@gmail.com> wrote:
> 
> Hi Erik,
> 
> Wow that's great. Thanks for the explanation, I tried using the
> VelocityResponseWriter with your template provided and it worked as
> expected, makes sense!
> 
> What if I want to return a custom JSON response back, rather than HTML for
> auto-suggesting? I'm thinking about using Twitter's typeahead jQuery plugin
> <https://twitter.github.io/typeahead.js/examples/#custom-templates <https://twitter.github.io/typeahead.js/examples/#custom-templates>> and
> passing it JSON, and have a template on the front-end to the autosuggest.
> 
> Ryan
> 
> 
> 
> On Sat Dec 06 2014 at 3:41:38 AM Erik Hatcher <erik.hatcher@gmail.com <ma...@gmail.com>>
> wrote:
> 
>> Ryan - I just pulled Taming Text off my shelf and refreshed my memory of
>> this custom response writer.
>> 
>> While having a custom writer is a neat example, it’s unnecessary for that
>> particular functionality.  Solr has a built-in templatable response writer,
>> the VelocityResponseWriter.  You can see it in action for a similar suggest
>> feature in Solr’s example /browse interface (type “ip” and wait a second in
>> the /browse UI with the sample data indexed).  In there is a little bit of
>> jQuery autocomplete plugin usage that calls back to the /terms handler,
>> using a suggest.vm template (in conf/velocity).  The difference with the
>> Taming Text example is that it is returns stored fields of a standard
>> search rather than just raw terms; with a little adjustment you can get
>> basically the same thing as TT.  Leveraging the Solr example (v4.10.2 for
>> me here), I created a conf/velocity/typeahead.vm:
>> 
>>  <ul>
>>    #foreach($doc in $response.results)
>>      <li>$doc.name</li>
>>    #end
>>  </ul>
>> 
>> (the docs in the example data have a ‘name’ field)
>> 
>> This request  http://localhost:8983/solr/collection1/select?q=name%
>> 3Aip*&wt=velocity&v.template=typeahead <http://localhost:8983/solr/ <http://localhost:8983/solr/>
>> collection1/select?q=name:ip*&wt=velocity&v.template=typeahead> results
>> in this response:
>> 
>>  <ul>
>>      <li>Belkin Mobile Power Cord for iPod w/ Dock</li>
>>      <li>iPod & iPod Mini USB 2.0 Cable</li>
>>      <li>Apple 60 GB iPod with Video Playback Black</li>
>>  </ul>
>> 
>>        Erik
>> 
>> 
>>> On Dec 6, 2014, at 2:24 AM, Ryan Yacyshyn <ry...@gmail.com>
>> wrote:
>>> 
>>> Hey Everyone,
>>> 
>>> I'm a little stuck on building a custom query response writer. I want to
>>> create a response writer similar to the one explained in the book, Taming
>>> Text, on the TypeAheadResponseWriter. I know I need to implement the
>>> QueryResponseWriter, but I'm not sure where to find the Solr JAR files I
>>> need to include. Where can I find these?
>>> 
>>> Thanks,
>>> Ryan


Re: Creating a Custom Query Response Writer

Posted by Ryan Yacyshyn <ry...@gmail.com>.
Hi Erik,

Wow that's great. Thanks for the explanation, I tried using the
VelocityResponseWriter with your template provided and it worked as
expected, makes sense!

What if I want to return a custom JSON response back, rather than HTML for
auto-suggesting? I'm thinking about using Twitter's typeahead jQuery plugin
<https://twitter.github.io/typeahead.js/examples/#custom-templates> and
passing it JSON, and have a template on the front-end to the autosuggest.

Ryan



On Sat Dec 06 2014 at 3:41:38 AM Erik Hatcher <er...@gmail.com>
wrote:

> Ryan - I just pulled Taming Text off my shelf and refreshed my memory of
> this custom response writer.
>
> While having a custom writer is a neat example, it’s unnecessary for that
> particular functionality.  Solr has a built-in templatable response writer,
> the VelocityResponseWriter.  You can see it in action for a similar suggest
> feature in Solr’s example /browse interface (type “ip” and wait a second in
> the /browse UI with the sample data indexed).  In there is a little bit of
> jQuery autocomplete plugin usage that calls back to the /terms handler,
> using a suggest.vm template (in conf/velocity).  The difference with the
> Taming Text example is that it is returns stored fields of a standard
> search rather than just raw terms; with a little adjustment you can get
> basically the same thing as TT.  Leveraging the Solr example (v4.10.2 for
> me here), I created a conf/velocity/typeahead.vm:
>
>   <ul>
>     #foreach($doc in $response.results)
>       <li>$doc.name</li>
>     #end
>   </ul>
>
> (the docs in the example data have a ‘name’ field)
>
> This request  http://localhost:8983/solr/collection1/select?q=name%
> 3Aip*&wt=velocity&v.template=typeahead <http://localhost:8983/solr/
> collection1/select?q=name:ip*&wt=velocity&v.template=typeahead> results
> in this response:
>
>   <ul>
>       <li>Belkin Mobile Power Cord for iPod w/ Dock</li>
>       <li>iPod & iPod Mini USB 2.0 Cable</li>
>       <li>Apple 60 GB iPod with Video Playback Black</li>
>   </ul>
>
>         Erik
>
>
> > On Dec 6, 2014, at 2:24 AM, Ryan Yacyshyn <ry...@gmail.com>
> wrote:
> >
> > Hey Everyone,
> >
> > I'm a little stuck on building a custom query response writer. I want to
> > create a response writer similar to the one explained in the book, Taming
> > Text, on the TypeAheadResponseWriter. I know I need to implement the
> > QueryResponseWriter, but I'm not sure where to find the Solr JAR files I
> > need to include. Where can I find these?
> >
> > Thanks,
> > Ryan
>
>

Re: Creating a Custom Query Response Writer

Posted by Erik Hatcher <er...@gmail.com>.
Ryan - I just pulled Taming Text off my shelf and refreshed my memory of this custom response writer.  

While having a custom writer is a neat example, it’s unnecessary for that particular functionality.  Solr has a built-in templatable response writer, the VelocityResponseWriter.  You can see it in action for a similar suggest feature in Solr’s example /browse interface (type “ip” and wait a second in the /browse UI with the sample data indexed).  In there is a little bit of jQuery autocomplete plugin usage that calls back to the /terms handler, using a suggest.vm template (in conf/velocity).  The difference with the Taming Text example is that it is returns stored fields of a standard search rather than just raw terms; with a little adjustment you can get basically the same thing as TT.  Leveraging the Solr example (v4.10.2 for me here), I created a conf/velocity/typeahead.vm:

  <ul>
    #foreach($doc in $response.results)
      <li>$doc.name</li>
    #end
  </ul>

(the docs in the example data have a ‘name’ field)

This request  http://localhost:8983/solr/collection1/select?q=name%3Aip*&wt=velocity&v.template=typeahead <http://localhost:8983/solr/collection1/select?q=name:ip*&wt=velocity&v.template=typeahead> results in this response:

  <ul>
      <li>Belkin Mobile Power Cord for iPod w/ Dock</li>
      <li>iPod & iPod Mini USB 2.0 Cable</li>
      <li>Apple 60 GB iPod with Video Playback Black</li>
  </ul>

	Erik


> On Dec 6, 2014, at 2:24 AM, Ryan Yacyshyn <ry...@gmail.com> wrote:
> 
> Hey Everyone,
> 
> I'm a little stuck on building a custom query response writer. I want to
> create a response writer similar to the one explained in the book, Taming
> Text, on the TypeAheadResponseWriter. I know I need to implement the
> QueryResponseWriter, but I'm not sure where to find the Solr JAR files I
> need to include. Where can I find these?
> 
> Thanks,
> Ryan