Search code examples
searchsolrfull-text-searchfull-text-indexing

How does termFreq function query work in Apache Solr


I am trying to query solr index using its API,

http://localhost:8983/solr/documents/select?defType=func&q=termfreq(contents,'hello)&wt=json

I have indexed 3 documents and 2 documents/records have the term "hello" but it returns all the documents.

{
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "q":"termfreq(contents,'hello')",
      "defType":"func",
      "indent":"on",
      "wt":["json",
        "json"],
      "_":"1538568705504"}},
  "response":{"numFound":3,"start":0,"docs":[
      {*here I have docs*}
  ]
  }

I was expecting only the documents which contain the word hello and its occurrence in those documents.

Am I correct or have I not understood this function properly?


Solution

  • You can't use functions like that in Solr. To retrieve only documents that have the term hello present, and get the count as the score, use:

    q=content:hello _val_:"termfreq(contents,'hello')"
    

    The first part of your query limits the result set to the documents that have hello in the content field, while the second part invokes the function query parser through the magic _val_ field. The result of that function is assigned as the score for the document, effectively returning both the documents that match and the count of the given term in those documents.

    You should also be able to use termfreq(contents, 'hello') directly in your field list (fl=termfreq(contents,'hello'),score,foo) if you don't want to assign it as the score.