Take the 2-minute tour ×

My use for Solr is to generate a primary search system : we basically feed a large set of documents in small batches and search a pre-specified query on it. Each of these documents is scanned for this particular query and if found, we need to store the database the file index id, path and hit count of that string in that document. I have searched online for ways to extract hit count values from solr for each document, but all I have understood so far is that Solr automatically sorts its results on the basis of hit-count and a variety of other factors, which you can edit using boosts and function query parameters.

  1. Is there an established way of extracting hit count from Solr?

  2. If not, is it possible to alter Solr's scoring formula such that it ONLY considers hit count, and then ask Solr to return the score (which would essentially be the hit-count in this case)

(I'm sorry that my question appeared a little confusing. I only want the hit count returned from Solr for each document so that I can store it in my database. Is that directly possible through solr? By hit-count, i mean that if i'm searching for a particular keyword, the no. of occurences in the indexed fields for each document in the Solr index.)

SOLR Results are actually sorted on the basis of the document's relevancy score right, which includes term frequency and a lot of other smaller factors.... i want only the hit-count to be returned : I was wondering if there is either a direct way to get the hit-count, or to alter how Solr scores documents so that it only scores on the basis of term frequency factor and get the term frequency value for each doc in my SOLR output

share|improve this question
add comment

2 Answers

Can I Extract Hit Count Value from Solr? sorts its results on the basis of hit-count

Your headline topic is about "Hit Count", but by reading your text, it seams, you are interested in the solr score - because by default solr sorts by score - is this, what you mean with "hit count"?

Is there an established way of extracting hit count from Solr?

Yes, it is possible to get the "score" value of an searched document (by the way it's also possible to get the hit-count)

To get the score, simple expand the "field list" parameter (fl) by "score". http://wiki.apache.org/solr/CommonQueryParameters#fl Example if you have the fields DOCUMENT,ID,PTAH - add score like that: http://localhost:8080/solr/select/?fl=DOCUMENT,ID,PTAH,score Example:

  <lst name="responseHeader">
  <int name="QTime">5</int>
    <lst name="params">
    <str name="start">0</str>
    <str name="fl">DOCUMENT,ID,PTAH,score</str>


the no. of occurences

How often an (key)word exits at the index? At an specific field? So facet search will count for you: http://wiki.apache.org/solr/SolrFacetingOverview

//Update 2:

2nd update - if you like to count the number of keywords inside one document in order to receive the document ID and the number of words for this document, you can use facet search in combination with facet range query range. Example: look at all documents for manufacturer "dell" and return the frequency of this keyword for every document(ID)

ID -> "dell" exits how many times?
241 -> 2
242 -> 0
243 -> 5

For this, use the following search parameter:

<str name="facet.field">YOUR_TEXTFIELD</str>
<str name="facet.range">ID</str>        <--- ID=field woth the document ID
<str name="f.ID.facet.range.gap">1</str> <--- count ID in step of 1
<str name="f.ID.facet.range.start">0</str>   <--- start ID for faceted search
<str name="q">dell</str>                   <---string, "keyword" to look/count for
<str name="f.ID.facet.range.end">1000</str> <--- end ID for faceted search
<str name="facet">true</str>
<str name="facet.method">enum</str>
share|improve this answer
i want the number of occurences of the keyword in a single document : For example, if document A has 4 occurences of that string, document B has 7 occurences, then my solr server should return to me : 1)the info I want from the document (in my case, just the id) 2) the values 4 and 7. –  Vivek Kuruvilla Jun 29 '11 at 6:30
i believe the faceted search only tells you how many documents satisfied that particular query .. for example if i have 7 documents relating to a company say dell, then faceted search on 'dell' will return to me the list of documents and the value 7 : not the number of times dell occurs in each document! –  Vivek Kuruvilla Jun 29 '11 at 6:33
do those parameters have to be defined in the solrconfig.xml under the standard search request handler? or are they passed by url to the server when the query is generated? –  Vivek Kuruvilla Jun 29 '11 at 11:38
you can put them into the url, like &f.ID.facet.range.gap=1&f.ID.facet.range.start=1000 ...end so on –  The Bndr Jun 29 '11 at 12:21
i tried your suggestion, but it isn't working : What i wanted was the document id that resulted in a positive match for the query 'dell' which was repeated 4 times in a document out of 16 documents, with none of the others having dell in them. I set the text field as facet.field and testid (an int field with the same data as document id) as facet.range (other parameters as u'd specified) : What i got as a result is a set of values of text fields with 1 marked against them if it was there and 0 if it wasn't there in the result, and a set of document ids, with only count for 1009 being 1. –  Vivek Kuruvilla Jun 30 '11 at 6:57
show 3 more comments

It is relatively easy using solr 4.0 just add a pseudo field to the fl parameter:


You can name the returned value if you like too:

share|improve this answer
add comment

Your Answer


