A misunderstanding about functionQuery

        When I was sharing solr (lucene) with others before, when I was talking about some special queries, such as prefixquery and wildcardQuery, I once said a sentence: query is to get the term in the dictionary table, and then judge whether it meets the conditions , Some queries (such as termquery) only judge whether the word in the dictionary table is equal to the word to be searched, prefixQuery is to judge whether it starts with a specified string, wildcardQuery is to judge whether it conforms to a regular expression, etc. , we can implement more complex query to achieve more complex conditions, such as function query. Of course, this statement is purely my own judgment, and there is no actual example to prove it.

       After reading the code of function query today, I found that I was wrong, funcitonQuery does not exist to implement complex judgment logic. Let me talk about what he did first, he has a vlaueSource in it (that is, to get some values ​​in the index, which may be from an attribute of the doc, or some values ​​that need to be calculated now, in the following There will be an introduction), and then he uses the value of the doc for a certain id in this valueSouce when calculating the score, which is added as a number to the default score. The default score has only one parameter, which is the query used. The boost, his result is to return all the doc, look at the scorer method of FunctionWeight generated by FunctionQuery,

@Override
public Scorer scorer(AtomicReaderContext context, Bits acceptDocs) throws IOException {
      return new AllScorer(context, acceptDocs, this, queryWeight);
}

He returns an AllScorer, as the name suggests, he also knows that he is the scorer that returns all doc, look at his next method:

@Override
public int nextDoc() throws IOException {
  for(;;) {
    ++doc;
    if (doc>=maxDoc) {
      return doc=NO_MORE_DOCS;
    }
    if (acceptDocs != null && !acceptDocs.get(doc)) continue;
    return doc;
  }
}

  It can be seen that as long as a doc is not deleted, it will be returned, so it can be said that this AllScorer returns all doc.

I made an example with solr, using function query, and found that he got all the doc, the example is: queryParser using edismax, q is set to: {!func}sub(someField,22), in the case of debugQuery, I see When it arrives, he does generate a FunctionQuery.

 

  The choice of the complex inverted table I mentioned above means this: for example, I want to find people whose height and weight meet certain conditions in the lucene index of a talent (for example, the sum is 300, if a person is 170, 130 pounds) , then it should be recalled), originally I thought that function query was to implement this kind of complex logic, but after I read his source code, I found that he did not do this. Because he only uses valueSource prominently, valueSource comes from the field cache (that is, dictionary table or docValue), and this obviously cannot be related to the inverted list, so I can say for sure that the function query is very simple. , is to use valueSource to calculate a simple score.

  So since the function of FunctionQuery is so small, what is it used for?

 

  Very simple, as a condition for sorting, because it can only calculate the value of valueSource and its own boost, it is very convenient to use it to calculate a value as a sorting criterion. Using it in solr is a good example!

 

  Since the value source is used, let's just say that the value source is to obtain a certain value of a doc, for example, it can be the value of a domain, or the value function value of multiple domains (such as the tf of some terms in the domain), It can also be the value of a specified constant (such as valueSource of constant type), which can well understand those functions in solr, such as max, sub, div, which are the values ​​of multiple fields of a doc After performing a function operation, the final result is used to calculate the score. So in this way, it is very simple to understand those functions of solr, and we can implement some functions ourselves to achieve more complex functions!

 

  Finally, let me repeat, functionquery is not a complicated query, I misunderstood it before, correct this misunderstanding!

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326120979&siteId=291194637
Recommended