Return-Path: Delivered-To: apmail-lucene-java-commits-archive@www.apache.org Received: (qmail 33389 invoked from network); 28 Sep 2006 07:10:39 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 28 Sep 2006 07:10:39 -0000 Received: (qmail 5341 invoked by uid 500); 28 Sep 2006 07:10:36 -0000 Delivered-To: apmail-lucene-java-commits-archive@lucene.apache.org Received: (qmail 5104 invoked by uid 500); 28 Sep 2006 07:10:36 -0000 Mailing-List: contact java-commits-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: java-dev@lucene.apache.org Delivered-To: mailing list java-commits@lucene.apache.org Received: (qmail 5001 invoked by uid 99); 28 Sep 2006 07:10:35 -0000 Received: from idunn.apache.osuosl.org (HELO idunn.apache.osuosl.org) (140.211.166.84) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Sep 2006 00:10:35 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=5.0 tests=ALL_TRUSTED,NO_REAL_NAME Received: from [140.211.166.113] ([140.211.166.113:63702] helo=eris.apache.org) by idunn.apache.osuosl.org (ecelerity 2.1.1.8 r(12930)) with ESMTP id 47/29-05478-A657B154 for ; Thu, 28 Sep 2006 00:10:34 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 71C171A981D; Thu, 28 Sep 2006 00:10:31 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r450724 - /lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java Date: Thu, 28 Sep 2006 07:10:31 -0000 To: java-commits@lucene.apache.org From: whoschek@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20060928071031.71C171A981D@eris.apache.org> X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: whoschek Date: Thu Sep 28 00:10:30 2006 New Revision: 450724 URL: http://svn.apache.org/viewvc?view=rev&rev=450724 Log: added support for per field boost factor Modified: lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java Modified: lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java?view=diff&rev=450724&r1=450723&r2=450724 ============================================================================== --- lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java (original) +++ lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java Thu Sep 28 00:10:30 2006 @@ -20,6 +20,7 @@ import org.apache.lucene.analysis.Token; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldSelector; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; @@ -169,6 +170,9 @@ /** pos: positions[3*i], startOffset: positions[3*i +1], endOffset: positions[3*i +2] */ private final int stride; + /** Could be made configurable; See {@link Document#setBoost(float)} */ + private static final float docBoost = 1.0f; + private static final long serialVersionUID = 2782195016849084649L; private static final boolean DEBUG = false; @@ -274,6 +278,18 @@ } /** + * Equivalent to addField(fieldName, stream, 1.0f). + * + * @param fieldName + * a name to be associated with the text + * @param stream + * the token stream to retrieve tokens from + */ + public void addField(String fieldName, TokenStream stream) { + addField(fieldName, stream, 1.0f); + } + + /** * Iterates over the given token stream and adds the resulting terms to the index; * Equivalent to adding a tokenized, indexed, termVectorStored, unstored, * Lucene {@link org.apache.lucene.document.Field}. @@ -284,8 +300,11 @@ * a name to be associated with the text * @param stream * the token stream to retrieve tokens from. + * @param boost + * the boost factor for hits for this field + * @see Field#setBoost(float) */ - public void addField(String fieldName, TokenStream stream) { + public void addField(String fieldName, TokenStream stream, float boost) { /* * Note that this method signature avoids having a user call new * o.a.l.d.Field(...) which would be much too expensive due to the @@ -308,7 +327,9 @@ if (fieldName == null) throw new IllegalArgumentException("fieldName must not be null"); if (stream == null) - throw new IllegalArgumentException("token stream must not be null"); + throw new IllegalArgumentException("token stream must not be null"); + if (boost <= 0.0f) + throw new IllegalArgumentException("boost factor must be greater than 0.0"); if (fields.get(fieldName) != null) throw new IllegalArgumentException("field must not be added more than once"); @@ -338,7 +359,8 @@ // ensure infos.numTokens > 0 invariant; needed for correct operation of terms() if (numTokens > 0) { - fields.put(fieldName, new Info(terms, numTokens)); + boost = boost * docBoost; // see DocumentWriter.addDocument(...) + fields.put(fieldName, new Info(terms, numTokens, boost)); sortedFields = null; // invalidate sorted view, if any } } catch (IOException e) { // can never happen @@ -435,7 +457,7 @@ while (iter.hasNext()) { // for each Field Info Map.Entry entry = (Map.Entry) iter.next(); Info info = (Info) entry.getValue(); - size += HEADER + 4 + PTR + PTR + PTR; // Info instance vars + size += HEADER + 4 + 4 + PTR + PTR + PTR; // Info instance vars if (info.sortedTerms != null) size += ARR + PTR * info.sortedTerms.length; int len = info.terms.size(); @@ -545,14 +567,18 @@ /** Number of added tokens for this field */ private final int numTokens; + /** Boost factor for hits for this field */ + private final float boost; + /** Term for this field's fieldName, lazily computed on demand */ public transient Term template; private static final long serialVersionUID = 2882195016849084649L; - public Info(HashMap terms, int numTokens) { + public Info(HashMap terms, int numTokens, float boost) { this.terms = terms; this.numTokens = numTokens; + this.boost = boost; } /** @@ -577,6 +603,10 @@ return (ArrayIntList) sortedTerms[pos].getValue(); } + public float getBoost() { + return boost; + } + } @@ -970,6 +1000,8 @@ Info info = getInfo(fieldName); int numTokens = info != null ? info.numTokens : 0; float n = sim.lengthNorm(fieldName, numTokens); + float boost = info != null ? info.getBoost() : 1.0f; + n = n * boost; // see DocumentWriter.writeNorms(String segment) byte norm = Similarity.encodeNorm(n); norms = new byte[] {norm};