Java tutorial
/* * Copyright 2013 Websquared, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.fastcatsearch.ir.search; import java.io.IOException; import java.util.List; import org.apache.lucene.util.BytesRef; import org.fastcatsearch.ir.field.HitField; import org.fastcatsearch.ir.field.ScoreField; import org.fastcatsearch.ir.io.FixedMaxPriorityQueue; import org.fastcatsearch.ir.query.Sort; import org.fastcatsearch.ir.settings.FieldIndexSetting; import org.fastcatsearch.ir.settings.FieldSetting; import org.fastcatsearch.ir.settings.Schema; import org.fastcatsearch.ir.sort.SortFunction; /** * ? Sorts? ? . * HitElement ? byte[] ?? ? ?? sort? ? ? SortFunction? ?. * ? heap? pop ??. * * 2014-7-30 bundle key ?? push ?. * @see HitMerger * @author swsong * */ public class HitRanker extends FixedMaxPriorityQueue<HitElement> { private SortFunction[] sortFunctions; public HitRanker(List<Sort> querySortList, Schema schema, int maxSize) throws IOException { super(maxSize); int size = querySortList.size(); sortFunctions = new SortFunction[size]; for (int i = 0; i < size; i++) { Sort sort = querySortList.get(i); String fieldIndexId = sort.fieldIndexId(); int idx = schema.getFieldIndexSequence(fieldIndexId); ////////_HIT , _SCORE ? . if (idx == -1) { if (fieldIndexId.equalsIgnoreCase(ScoreField.fieldName)) { sortFunctions[i] = sort.createSortFunction(ScoreField.field); } else if (fieldIndexId.equalsIgnoreCase(HitField.fieldName)) { sortFunctions[i] = sort.createSortFunction(HitField.field); } else { throw new IOException("Unknown sort field name = " + fieldIndexId); } } else { FieldIndexSetting fieldIndexSetting = schema.getFieldIndexSetting(fieldIndexId); String refId = fieldIndexSetting.getRef(); FieldSetting fieldSetting = schema.getFieldSetting(refId); sortFunctions[i] = sort.createSortFunction(fieldSetting); } logger.debug("sortFunctions[{}]=[{}]=", i, sortFunctions[i]); } } @Override public boolean push(HitElement e) { if (e.getBundleKey() != null) { BytesRef bundleKey = e.getBundleKey(); for (int i = 1; i <= size; i++) { if (bundleKey.equals(((HitElement) heap[i]).getBundleKey())) { /* * ?? bundle ? ?? ? ? ?. */ if (compare(e, (HitElement) heap[i]) < 0) { // ? . // . replaceEl(i, e); // break; return true; } // logger.debug("Do no push > {}", e.docNo()); return false; } } } // logger.debug("Continue to push > {}", e.docNo()); // bundle? ?? bundle? push. return super.push(e); } @Override protected int compare(HitElement one, HitElement two) { for (int i = 0; i < sortFunctions.length; i++) { // ? ? funtion? ? 0? ? ?. int r = sortFunctions[i].compare(one.rankData(i), two.rankData(i)); if (r != 0) { return r; } } return one.compareTo(two); // // . // if(one.segmentSequence() != two.segmentSequence()){ // return two.segmentSequence() - one.segmentSequence(); // } // // // ?? ? ? . // return two.docNo() - one.docNo(); } }