How to access idf values if index is having only one shard

I have tried accessing idf using explanation API. I'm getting idf values as required. But it consumes more time and my application performance time also increases. If we are able to access idf using explanation API there must be a way to access them directly. can anyone help me in this case?

Why do you want to know the IDF in your application?

Im using idf as word weight for each word of match query to boost the results

I figured it out I wrote this particular function which takes around 2 sec to fetch idf values using explanation API

**basic cleaning function**
def word_tokenize_sentence(title):
   
    title = "".join([w.lower() for w in title ])
    title=nltk.word_tokenize(title)
    title= [token for token in title if token not in stop_words and token not in string.punctuation]
    return title
**Actual idf function**
def idf_weights(titles,title_index):
    titles=" ".join(word_tokenize_sentence(titles))
    search_query=es.search(title_index,body={
      "size":50,     # find the match in 50 samples
      "explain": "true",
       "_source":"false", 
  "query": {

            "match" : {
                "title" : titles 
            }
        }

    })['hits']['hits'][0]['_explanation']['details']


    sf= pd.DataFrame(search_query)
    idf_weight={}
    for row in sf.itertuples():
        title=row[1].split()[0][13:]
        idf=row[2][0]['details'][0]['value']
        idf_weight[title]=idf

    return idf_weight


title =mi band hrx edition black strap size regular

   **output**:   idf_weights(title,"my_index")  # returns a dictionary of word and idf value

  {'mi': 6.2642317, 
   'band': 4.7116814, 
    'hrx': 10.545517, 
    'edition': 4.7454147, 
     'black': 2.0724149, 
     'strap': 3.8747506,  
     'size': 3.9695117,
     'regular': 4.4794087}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.