ishaan arora
ishaan arora

Reputation: 541

SolrJ Getting document score of all the resulting documents from solr query

I am able to fetch all the documents for a solr query in Solr 6.3.0 using the JAVA API SolrJ.I want an additional field of correct "score" calculated by solr(using tf,idf and field norm) to rank the documents.I am getting the score field as 1.0 for all the documents.Can you help me get the correct "score" field.

Below is my code snippet and the output.

        String urlString = "http://localhost:8983/solr/mycore2";
        SolrClient solr = new HttpSolrClient.Builder(urlString).build();
        SolrQuery query = new SolrQuery();
        query.setQuery( "*" );
        query.set("fl", "id,house,postcode,score");
        String s="house=".concat(address.getHouseNumber().getCoveredText());
        query.addFilterQuery(s);
        QueryResponse resp = solr.query(query);
        SolrDocumentList list = resp.getResults();

        if(list!=null)
        {
            System.out.println(list.toString());
        }

Output

{numFound=4,start=0,maxScore=1.0,docs=[SolrDocument{id=1, house=[150-151], postcode=[641044], score=1.0}, SolrDocument{id=2, house=[150/151], postcode=[641044], score=1.0}, SolrDocument{id=3, house=[151/150], postcode=[641044], score=1.0}, SolrDocument{id=4, house=[151/150], postcode=[641044], score=1.0}]}

Edit After MatsLindh's suggestion,here is the tweaked code and the output.

String urlString = "http://localhost:8983/solr/mycore2";
        SolrClient solr = new HttpSolrClient.Builder(urlString).build();
        SolrQuery query = new SolrQuery();
        query.setQuery(address.getHouseNumber().getCoveredText().concat(" ").concat(address.getPostcode().getCoveredText()));
        query.set("fl", "id,house,postcode,score");
        QueryResponse resp = solr.query(query);
        SolrDocumentList list = resp.getResults();
        if(list!=null)
        {
            System.out.println(list.toString());
        }

Output

{numFound=3,start=0,maxScore=2.4800222,docs=[SolrDocument{id=6, house=[34], postcode=[641006], score=2.4800222}, SolrDocument{id=5, house=[34], postcode=[641005], score=1.2400111}, SolrDocument{id=7, house=[2-11A], postcode=[641006], score=1.1138368}]}

Upvotes: 0

Views: 1421

Answers (1)

MatsLindh
MatsLindh

Reputation: 52802

Since you're not querying for anything, you're not getting a score (each score is the same, 1.0f). You're only applying a filter, which does not affect the score.

There is no tf/idf (but remember that Solr now uses BM25 as the default similarity model and not tf/idf) score to calculate if there are no tokens to match in the actual query.

Upvotes: 2

Related Questions