'TermFreq'에 해당되는 글 1건

  1. 2018.01.31 [검색] re-ranking 시 사용하는 함수에 주의 하세요.

[검색] re-ranking 시 사용하는 함수에 주의 하세요.

ITWeb/검색일반 2018. 1. 31. 11:19

Elasticsearch 나 Solr 나 모두 내장 함수를 제공 하고 있습니다. (Lucene function 이기도 합니다.)

그런데 이 내장 함수를 이용해서 re-ranking 작업을 많이들 하시는 데요.

하는건 문제가 없지만 시스템 리소스에 영향을 주거나 성능적으로 문제가 되는 함수는 사용하지 않도록 주의 하셔야 합니다.

그냥 무심코 사용했다가 왜 성능이 안나오지 하고 맨붕에 빠지실 수 있습니다.

Function Score Query 나 Script 를 이용한 re-ranking 시 꼭 검토하세요.

re-ranking 은 보통 질의 시점에 수행을 하기 때문에 기본적으로 operation cost 가 비쌉니다.


lucene/queries/function/valuesource

TermFreqValueSource.java)

/**
* Function that returns {@link org.apache.lucene.index.PostingsEnum#freq()} for the
* supplied term in every document.
* <p>
* If the term does not exist in the document, returns 0.
* If frequencies are omitted, returns 1.
*/
public class TermFreqValueSource extends DocFreqValueSource {
public TermFreqValueSource(String field, String val, String indexedField, BytesRef indexedBytes) {
super(field, val, indexedField, indexedBytes);
}

@Override
public String name() {
return "termfreq";
}

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
Fields fields = readerContext.reader().fields();
final Terms terms = fields.terms(indexedField);

return new IntDocValues(this) {
PostingsEnum docs ;
int atDoc;
int lastDocRequested = -1;

{ reset(); }

public void reset() throws IOException {
// no one should call us for deleted docs?

if (terms != null) {
final TermsEnum termsEnum = terms.iterator();
if (termsEnum.seekExact(indexedBytes)) {
docs = termsEnum.postings(null);
} else {
docs = null;
}
} else {
docs = null;
}

if (docs == null) {
docs = new PostingsEnum() {
@Override
public int freq() {
return 0;
}

@Override
public int nextPosition() throws IOException {
return -1;
}

@Override
public int startOffset() throws IOException {
return -1;
}

@Override
public int endOffset() throws IOException {
return -1;
}

@Override
public BytesRef getPayload() throws IOException {
throw new UnsupportedOperationException();
}

@Override
public int docID() {
return DocIdSetIterator.NO_MORE_DOCS;
}

@Override
public int nextDoc() {
return DocIdSetIterator.NO_MORE_DOCS;
}

@Override
public int advance(int target) {
return DocIdSetIterator.NO_MORE_DOCS;
}

@Override
public long cost() {
return 0;
}
};
}
atDoc = -1;
}

@Override
public int intVal(int doc) {
try {
if (doc < lastDocRequested) {
// out-of-order access.... reset
reset();
}
lastDocRequested = doc;

if (atDoc < doc) {
atDoc = docs.advance(doc);
}

if (atDoc > doc) {
// term doesn't match this document... either because we hit the
// end, or because the next doc is after this doc.
return 0;
}

// a match!
return docs.freq();
} catch (IOException e) {
throw new RuntimeException("caught exception in function "+description()+" : doc="+doc, e);
}
}
};
}
}



: