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 Kissue Kissue <ki...@gmail.com> on 2011/05/23 17:01:56 UTC

Including Score in Solr POJO

Hi,

I am currently using Solr and indexing/reading my documents as POJO. The
question i have is how can i include the score in the POJO for each document
found in the index?

Thanks.

Re: Including Score in Solr POJO

Posted by Anuj Kumar <an...@gmail.com>.
Hi,

I have not tried that but if you add a field annotation for "score" in your
POJO and set the query object to include the score.
Doesn't it populate the POJOs score field?

Are you using-
http://lucene.apache.org/solr/api/org/apache/solr/client/solrj/beans/DocumentObjectBinder.html
for appropriate mapping between the fields of SolrDocument and POJOs?

Please try that. In the meantime, I will give it a shot.

Regards,
Anuj

On Tue, May 24, 2011 at 2:17 PM, Kissue Kissue <ki...@gmail.com> wrote:

> Hi Anuj,
>
> Thanks for your response. I am actually doing a bean search so am  doing
> the
> following:
>
>        SolrQuery solrQuery = new SolrQuery(query);
>        QueryResponse response = solr.query(solrQuery);
>        List<Product> beans = response.getBeans(Product.class);
>
> It is not immediately clear to me how to get the score doing a bean search.
>
> Thanks.
>
> On Mon, May 23, 2011 at 4:47 PM, Anuj Kumar <an...@gmail.com> wrote:
>
> > Hi,
> >
> > On Mon, May 23, 2011 at 8:52 PM, Kissue Kissue <ki...@gmail.com>
> > wrote:
> >
> > > Thanks Anuj for your reply. Would it then include it as a field in my
> > POJO?
> > >
> >
> > I meant the score given by Solr in response to the search query. Is it an
> > application specific score that you want to include?
> >
> >
> > > How do i define such field? I have a POJO with the @Field annotation
> > which
> > > is mapped to fields in my schema.
> > >
> >
> > At the time of indexing, you need not specify the score. The score is
> > calculated based on the relevance of the query against the matched
> > documents. If you have an application specific score or weight that you
> > want
> > to add, you can add it as a separate field but what I understand from
> your
> > query is that you want the score that Solr gives to each search results.
> In
> > that case, just setting the property IncludeScore to true while
> > constructing
> > the query object (as shown in the example that I gave earlier) will
> > suffice.
> >
> > From the query response, you can then query for the maximum score, as
> well
> > as each document's score. For example-
> >
> > // get the response
> > QueryResponse results = getSearchServer().query(query);
> > // get the documents
> > SolrDocumentList resultDocs = results.getResults();
> > // get the maximum score
> > float maxScore = resultDocs.getMaxScore();
> > // iterate through the documents to see the results
> > for(SolrDocument doc : resultDocs){
> > // get the score
> > Object score = doc.get("score");
> > }
> >
> > Hope that helps.
> >
> > Regards,
> > Anuj
> >
> > >
> > > Thanks.
> > >
> > > On Mon, May 23, 2011 at 4:10 PM, Anuj Kumar <an...@gmail.com>
> wrote:
> > >
> > > > Hi,
> > > >
> > > > If you mean SolrJ (as I understand by your description of POJOs), you
> > can
> > > > add the score by setting the property IncludeScore to true. For
> > example-
> > > >
> > > > SolrQuery query = new SolrQuery().
> > > >    setQuery(keyword).
> > > >      *setIncludeScore(true);*
> > > >
> > > > Regards,
> > > > Anuj
> > > >
> > > > On Mon, May 23, 2011 at 8:31 PM, Kissue Kissue <ki...@gmail.com>
> > > > wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > I am currently using Solr and indexing/reading my documents as
> POJO.
> > > The
> > > > > question i have is how can i include the score in the POJO for each
> > > > > document
> > > > > found in the index?
> > > > >
> > > > > Thanks.
> > > > >
> > > >
> > >
> >
>

Re: Including Score in Solr POJO

Posted by Kissue Kissue <ki...@gmail.com>.
Hi Anuj,

Thanks for your response. I am actually doing a bean search so am  doing the
following:

        SolrQuery solrQuery = new SolrQuery(query);
        QueryResponse response = solr.query(solrQuery);
        List<Product> beans = response.getBeans(Product.class);

It is not immediately clear to me how to get the score doing a bean search.

Thanks.

On Mon, May 23, 2011 at 4:47 PM, Anuj Kumar <an...@gmail.com> wrote:

> Hi,
>
> On Mon, May 23, 2011 at 8:52 PM, Kissue Kissue <ki...@gmail.com>
> wrote:
>
> > Thanks Anuj for your reply. Would it then include it as a field in my
> POJO?
> >
>
> I meant the score given by Solr in response to the search query. Is it an
> application specific score that you want to include?
>
>
> > How do i define such field? I have a POJO with the @Field annotation
> which
> > is mapped to fields in my schema.
> >
>
> At the time of indexing, you need not specify the score. The score is
> calculated based on the relevance of the query against the matched
> documents. If you have an application specific score or weight that you
> want
> to add, you can add it as a separate field but what I understand from your
> query is that you want the score that Solr gives to each search results. In
> that case, just setting the property IncludeScore to true while
> constructing
> the query object (as shown in the example that I gave earlier) will
> suffice.
>
> From the query response, you can then query for the maximum score, as well
> as each document's score. For example-
>
> // get the response
> QueryResponse results = getSearchServer().query(query);
> // get the documents
> SolrDocumentList resultDocs = results.getResults();
> // get the maximum score
> float maxScore = resultDocs.getMaxScore();
> // iterate through the documents to see the results
> for(SolrDocument doc : resultDocs){
> // get the score
> Object score = doc.get("score");
> }
>
> Hope that helps.
>
> Regards,
> Anuj
>
> >
> > Thanks.
> >
> > On Mon, May 23, 2011 at 4:10 PM, Anuj Kumar <an...@gmail.com> wrote:
> >
> > > Hi,
> > >
> > > If you mean SolrJ (as I understand by your description of POJOs), you
> can
> > > add the score by setting the property IncludeScore to true. For
> example-
> > >
> > > SolrQuery query = new SolrQuery().
> > >    setQuery(keyword).
> > >      *setIncludeScore(true);*
> > >
> > > Regards,
> > > Anuj
> > >
> > > On Mon, May 23, 2011 at 8:31 PM, Kissue Kissue <ki...@gmail.com>
> > > wrote:
> > >
> > > > Hi,
> > > >
> > > > I am currently using Solr and indexing/reading my documents as POJO.
> > The
> > > > question i have is how can i include the score in the POJO for each
> > > > document
> > > > found in the index?
> > > >
> > > > Thanks.
> > > >
> > >
> >
>

Re: Including Score in Solr POJO

Posted by Anuj Kumar <an...@gmail.com>.
Hi,

On Mon, May 23, 2011 at 8:52 PM, Kissue Kissue <ki...@gmail.com> wrote:

> Thanks Anuj for your reply. Would it then include it as a field in my POJO?
>

I meant the score given by Solr in response to the search query. Is it an
application specific score that you want to include?


> How do i define such field? I have a POJO with the @Field annotation which
> is mapped to fields in my schema.
>

At the time of indexing, you need not specify the score. The score is
calculated based on the relevance of the query against the matched
documents. If you have an application specific score or weight that you want
to add, you can add it as a separate field but what I understand from your
query is that you want the score that Solr gives to each search results. In
that case, just setting the property IncludeScore to true while constructing
the query object (as shown in the example that I gave earlier) will suffice.

>From the query response, you can then query for the maximum score, as well
as each document's score. For example-

// get the response
QueryResponse results = getSearchServer().query(query);
// get the documents
SolrDocumentList resultDocs = results.getResults();
// get the maximum score
float maxScore = resultDocs.getMaxScore();
// iterate through the documents to see the results
for(SolrDocument doc : resultDocs){
// get the score
Object score = doc.get("score");
}

Hope that helps.

Regards,
Anuj

>
> Thanks.
>
> On Mon, May 23, 2011 at 4:10 PM, Anuj Kumar <an...@gmail.com> wrote:
>
> > Hi,
> >
> > If you mean SolrJ (as I understand by your description of POJOs), you can
> > add the score by setting the property IncludeScore to true. For example-
> >
> > SolrQuery query = new SolrQuery().
> >    setQuery(keyword).
> >      *setIncludeScore(true);*
> >
> > Regards,
> > Anuj
> >
> > On Mon, May 23, 2011 at 8:31 PM, Kissue Kissue <ki...@gmail.com>
> > wrote:
> >
> > > Hi,
> > >
> > > I am currently using Solr and indexing/reading my documents as POJO.
> The
> > > question i have is how can i include the score in the POJO for each
> > > document
> > > found in the index?
> > >
> > > Thanks.
> > >
> >
>

Re: Including Score in Solr POJO

Posted by Kissue Kissue <ki...@gmail.com>.
Thanks Anuj for your reply. Would it then include it as a field in my POJO?
How do i define such field? I have a POJO with the @Field annotation which
is mapped to fields in my schema.

Thanks.

On Mon, May 23, 2011 at 4:10 PM, Anuj Kumar <an...@gmail.com> wrote:

> Hi,
>
> If you mean SolrJ (as I understand by your description of POJOs), you can
> add the score by setting the property IncludeScore to true. For example-
>
> SolrQuery query = new SolrQuery().
>    setQuery(keyword).
>      *setIncludeScore(true);*
>
> Regards,
> Anuj
>
> On Mon, May 23, 2011 at 8:31 PM, Kissue Kissue <ki...@gmail.com>
> wrote:
>
> > Hi,
> >
> > I am currently using Solr and indexing/reading my documents as POJO. The
> > question i have is how can i include the score in the POJO for each
> > document
> > found in the index?
> >
> > Thanks.
> >
>

Re: Including Score in Solr POJO

Posted by Anuj Kumar <an...@gmail.com>.
Hi,

If you mean SolrJ (as I understand by your description of POJOs), you can
add the score by setting the property IncludeScore to true. For example-

SolrQuery query = new SolrQuery().
    setQuery(keyword).
      *setIncludeScore(true);*

Regards,
Anuj

On Mon, May 23, 2011 at 8:31 PM, Kissue Kissue <ki...@gmail.com> wrote:

> Hi,
>
> I am currently using Solr and indexing/reading my documents as POJO. The
> question i have is how can i include the score in the POJO for each
> document
> found in the index?
>
> Thanks.
>