org.fastcatsearch.ir.search.HitMerger.java Source code

Java tutorial

Introduction

Here is the source code for org.fastcatsearch.ir.search.HitMerger.java

Source

/*
 * 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.FixedHitReader;
import org.fastcatsearch.ir.io.FixedMinHeap;
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;

/**
 * ? ? ?  HitElement? ?    ? 
 * ? heap? pop   ? ??.
 * @see HitRanker HitRanker   ?
 * @author swsong
 *
 */
public class HitMerger extends FixedMinHeap<FixedHitReader> {
    private SortFunction[] sortFunctions;

    public HitMerger(int segmentSize) {
        super(segmentSize);
    }

    public HitMerger(List<Sort> querySortList, Schema schema, int segmentSize) throws IOException {
        super(segmentSize);

        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(FixedHitReader e) {
        if (e.read().getBundleKey() != null) {
            BytesRef bundleKey = e.read().getBundleKey();
            for (int i = 1; i <= size; i++) {
                if (bundleKey.equals(((HitElement) ((FixedHitReader) heap[i]).read()).getBundleKey())) {
                    //?? bundle ?  ? push .
                    logger.debug("?? Bundle found!!!");
                    return false;
                }
            }
        }
        //bundle?   ?? bundle?  push. 
        return super.push(e);
    }

    @Override
    protected int compareTo(FixedHitReader r1, FixedHitReader r2) {

        HitElement one = r1.read();
        HitElement two = r2.read();

        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;
            }
        }

        if (r1.collectionId() == r2.collectionId()) {
            // ?? ?   ? . 
            //         return two.docNo() - one.docNo();
            return one.compareTo(two);
        } else {
            //? shard  ?  ? .
            return 0;
        }
    }

}