Example #1
0
/*****************************************************************************
*  function name : SearchFieldByString
*  params : query field
*  return :
*
*  description : 在指定字段检索相应的query,用于正常检索
*
******************************************************************************/
func (this *IndexSet) SearchFieldByString(query string, field string) ([]utils.DocIdInfo, bool) {

	var terms []string
	if this.FieldInfo[field].SType == 9 {
		terms = this.IvtIndex[field].GetCustomInterface().SegmentFunc(query, true)
	} else {
		terms = this.Segmenter.SegmentByType(query, this.FieldInfo[field].SType, false)
	}

	//fmt.Printf("terms : %v \n",terms)
	/*
		var terms []string

		switch this.FieldInfo[field].SType {
		case 1: //正常切词
			terms = utils.RemoveDuplicatesAndEmpty(this.Segmenter.Segment(query, false))
		case 2: //按单个字符进行切词
			terms = utils.RemoveDuplicatesAndEmpty(strings.Split(query, ""))
		case 3: //按规定的分隔符进行切词
			terms = utils.RemoveDuplicatesAndEmpty(strings.Split(query, ","))
		}
	*/

	//按照最大切分进行切词
	//terms := utils.RemoveDuplicatesAndEmpty(this.Segmenter.Segment(query, false))
	//this.Logger.Info("TERMS :: %v ", terms)
	//首先按照字段检索
	//交集结果
	var res_list []utils.DocIdInfo
	_, ok := this.IvtIndex[field]
	if !ok {
		return nil, false
	}
	isFound := true
	for index, term := range terms {
		l, ok := this.IvtIndex[field].Find(term)
		if !ok {
			isFound = false
			break
		}
		//this.Logger.Info("[Term : %v ] [Field: %v ] DocIDs : %v", term, field, l)
		//求交集
		if index == 0 {
			res_list = l
		} else {
			res_list, ok = utils.Interaction(l, res_list)
			if !ok {
				isFound = false
				break
			}
		}

	}
	if len(res_list) > 0 && isFound == true {
		return res_list, true
	}
	return nil, false
}
Example #2
0
func (this *invert) query(key interface{}) ([]utils.DocIdNode, bool) {

	//this.Logger.Info("[DEBUG] invert Query %v", key)
	keystr, ok := key.(string)
	if !ok {
		return nil, false
	}

	//全词匹配模式
	if this.fieldType == utils.IDX_TYPE_STRING || this.fieldType == utils.GATHER_TYPE {
		return this.queryTerm(keystr)
	}

	var queryterms []string
	switch this.fieldType {
	case utils.IDX_TYPE_STRING_LIST: //分号切割模式
		queryterms = strings.Split(keystr, ";")
	case utils.IDX_TYPE_STRING_SINGLE: //单字模式
		queryterms = utils.GSegmenter.SegmentSingle(keystr)
	case utils.IDX_TYPE_STRING_SEG: //分词模式
		queryterms = utils.GSegmenter.Segment(keystr, false)
	default:
		return this.queryTerm(keystr)
	}
	if len(queryterms) == 1 {
		return this.queryTerm(queryterms[0])
	}
	var fDocids []utils.DocIdNode
	// var sDocids []utils.DocIdNode
	var hasRes bool
	var match bool
	fDocids, match = this.queryTerm(queryterms[0])
	//fDocids=append(fDocids,sDocids...)
	if match {
		for _, term := range queryterms[1:] {
			subDocids, ok := this.queryTerm(term)
			if !ok {
				return nil, false
			}
			fDocids, hasRes = utils.Interaction(fDocids, subDocids)
			if !hasRes {
				return nil, false
			}
		}
	}

	if len(fDocids) == 0 {
		return nil, false
	}
	return fDocids, true

}
Example #3
0
func (this *IndexSet) SearchByRules(rules /*map[string]interface{}*/ []SearchRule) ([]utils.DocIdInfo, bool) {
	functime := utils.InitTime()
	fmt.Printf("SearchByRules: %v \n", functime("Start"))
	var res []utils.DocIdInfo
	for index, rule := range rules {
		var sub_res []utils.DocIdInfo
		var ok bool
		if rule.Field == "query" {
			sub_res, ok = this.Search(rule.Query)
		} else {
			//this.Logger.Info(" Field : %v Query : %v",rule.Field, rule.Query)
			fmt.Printf("SearchByRules: %v \n", functime("Start SearchField"))
			sub_res, ok = this.SearchField(rule.Query, rule.Field)
			fmt.Printf("SearchByRules: %v \n", functime("End SearchField"))
		}
		if !ok {
			return nil, false
		}
		if index == 0 {
			res = sub_res
		} else {
			fmt.Printf("SearchByRules: %v \n", functime("Start Interaction"))
			res, ok = utils.Interaction(res, sub_res)
			fmt.Printf("SearchByRules: %v \n", functime("End Interaction"))
			if !ok {
				return nil, false
			}
		}
		//this.Logger.Info(" RES :: %v ", res)
	}

	//BitMap过滤失效的doc_id
	//this.Logger.Info(" %v ",res)
	fmt.Printf("SearchByRules: %v \n", functime("Start Bitmap"))
	r := make([]utils.DocIdInfo, len(res))
	r_index := 0
	for i, _ := range res {
		//this.Logger.Info(" %v ",res[i].DocId)
		if this.BitMap.GetBit(uint64(res[i].DocId)) == 0 {
			r[r_index] = res[i]
			r_index++
			//r = append(r,res[i])
		}
	}
	fmt.Printf("SearchByRules: %v \n", functime("End Bitmap"))

	//TODO 自定义过滤
	fmt.Printf("SearchByRules: %v \n", functime("End SearchByRules"))
	return r[:r_index], true
}
Example #4
0
/*****************************************************************************
*  function name : SearchString
*  params : query
*  return : doc链
*
*  description : 检索函数,根据关键字进行检索,先在同一个字段中检索,如果没有结果进行跨字段检索
*
******************************************************************************/
func (this *IndexSet) SearchString(query string) ([]utils.DocIdInfo, bool) {

	//按照最大切分进行切词
	//terms := utils.RemoveDuplicatesAndEmpty(this.Segmenter.Segment(query,false))

	//首先按照字段检索
	var res_list []utils.DocIdInfo
	var ok bool
	for key, _ := range this.IvtIndex {
		if this.IvtIndex[key].GetType() != 1 {
			continue
		}
		res_list, ok = this.SearchFieldByString(query, key)
		if ok {
			return res_list, true
		}

	}

	//如果数量不够,跨字段检索 TODO
	var res_merge []utils.DocIdInfo
	if len(res_list) == 0 {
		terms := utils.RemoveDuplicatesAndEmpty(this.Segmenter.Segment(query, false))
		//this.Logger.Info("OUT TERMS :: %v ",terms)
		for index, term := range terms {
			l, ok := this.SearchFieldsByTerm(term)
			if !ok {
				return nil, false
			}

			if index == 0 {
				res_merge = l
			} else {
				res_merge, ok = utils.Interaction(res_merge, l)
				//this.Logger.Info("Interaction Term:%v Docids: %v",term,res_merge)
				if !ok {
					return nil, false
				}
			}
		}
	}

	return res_merge, true

}
Example #5
0
func (this *IndexSet) SearchByRules(rules /*map[string]interface{}*/ []SearchRule) ([]utils.DocIdInfo, bool) {

	var res []utils.DocIdInfo
	for index, rule := range rules {
		var sub_res []utils.DocIdInfo
		var ok bool
		if rule.Field == "query" {
			sub_res, ok = this.Search(rule.Query)
		} else {
			//this.Logger.Info(" Field : %v Query : %v",rule.Field, rule.Query)
			sub_res, ok = this.SearchField(rule.Query, rule.Field)
		}
		if !ok {
			return nil, false
		}
		if index == 0 {
			res = sub_res
		} else {
			res, ok = utils.Interaction(res, sub_res)
			if !ok {
				return nil, false
			}
		}
		//this.Logger.Info(" RES :: %v ", res)
	}

	//BitMap过滤失效的doc_id
	this.Logger.Info(" %v ", res)
	r := make([]utils.DocIdInfo, 0)
	for i, _ := range res {
		//this.Logger.Info(" %v ",res[i].DocId)
		if this.BitMap.GetBit(uint64(res[i].DocId)) == 0 {
			r = append(r, res[i])
		}
	}

	//TODO 自定义过滤

	return r, true
}
Example #6
0
func (this *IndexSet) SearchByRules(rules /*map[string]interface{}*/ []SearchRule) ([]utils.DocIdInfo, bool) {
	//functime := utils.InitTime()
	//fmt.Printf("SearchByRules: %v \n", functime("Start"))
	var res []utils.DocIdInfo
	for index, rule := range rules {
		var sub_res []utils.DocIdInfo
		var ok bool
		if rule.Field == "query" {
			sub_res, ok = this.Search(rule.Query)
		} else {
			sub_res, ok = this.SearchField(rule.Query, rule.Field)
		}
		if !ok {
			return nil, false
		}
		if index == 0 {
			res = sub_res
		} else {
			res, ok = utils.Interaction(res, sub_res)

			if !ok {
				return nil, false
			}
		}
	}

	//BitMap过滤失效的doc_id
	r := make([]utils.DocIdInfo, len(res))
	r_index := 0
	for i, _ := range res {

		if this.BitMap.GetBit(uint64(res[i].DocId)) == 0 {
			r[r_index] = res[i]
			r_index++

		}
	}
	//TODO 自定义过滤
	return r[:r_index], true
}
Example #7
0
/*****************************************************************************
*  function name : SearchFieldByString
*  params : query field
*  return :
*
*  description : 在指定字段检索相应的query,用于正常检索
*
******************************************************************************/
func (this *IndexSet) SearchFieldByString(query string, field string) ([]utils.DocIdInfo, bool) {

	//按照最大切分进行切词
	terms := utils.RemoveDuplicatesAndEmpty(this.Segmenter.Segment(query, false))
	//this.Logger.Info("TERMS :: %v ", terms)
	//首先按照字段检索
	//交集结果
	var res_list []utils.DocIdInfo
	_, ok := this.IvtIndex[field]
	if !ok {
		return nil, false
	}
	isFound := true
	for index, term := range terms {
		l, ok := this.IvtIndex[field].Find(term)
		if !ok {
			isFound = false
			break
		}
		//this.Logger.Info("[Term : %v ] [Field: %v ] DocIDs : %v", term, field, l)
		//求交集
		if index == 0 {
			res_list = l
		} else {
			res_list, ok = utils.Interaction(l, res_list)
			if !ok {
				isFound = false
				break
			}
		}

	}
	if len(res_list) > 0 && isFound == true {
		return res_list, true
	}
	return nil, false
}