MCPcopy Create free account
hub / github.com/apache/answer-plugins / SearchQuestions

Method SearchQuestions

search-algolia/algolia.go:115–171  ·  view source on GitHub ↗
(ctx context.Context, cond *plugin.SearchBasicCond)

Source from the content-addressed store, hash-verified

113}
114
115func (s *SearchAlgolia) SearchQuestions(ctx context.Context, cond *plugin.SearchBasicCond) (res []plugin.SearchResult, total int64, err error) {
116 var (
117 filters = "status<10 AND type:question"
118 tagFilters []string
119 userIDFilter string
120 viewsFilter string
121 answersFilter string
122 )
123 if len(cond.TagIDs) > 0 {
124 for _, tagID := range cond.TagIDs {
125 tagFilters = append(tagFilters, "tags:"+tagID)
126 }
127 if len(tagFilters) > 0 {
128 filters += " AND " + strings.Join(tagFilters, " AND ")
129 }
130 }
131 if cond.QuestionAccepted == plugin.AcceptedCondFalse {
132 userIDFilter = "hasAccepted:false"
133 filters += " AND " + userIDFilter
134 }
135
136 if cond.ViewAmount > -1 {
137 viewsFilter = "views>=" + strconv.Itoa(cond.ViewAmount)
138 filters += " AND " + viewsFilter
139 }
140
141 // check answers
142 if cond.AnswerAmount == 0 {
143 answersFilter = "answers=0"
144 filters += " AND " + answersFilter
145 } else if cond.AnswerAmount > 0 {
146 answersFilter = "answers>=" + strconv.Itoa(cond.AnswerAmount)
147 filters += " AND " + answersFilter
148 }
149
150 var (
151 query = strings.TrimSpace(strings.Join(cond.Words, " "))
152 opts = []interface{}{
153 opt.AttributesToRetrieve("objectID", "type"),
154 opt.Filters(filters),
155 opt.Page(cond.Page - 1),
156 opt.HitsPerPage(cond.PageSize),
157 }
158 qres search.QueryRes
159 )
160
161 qres, err = s.getIndex(string(cond.Order)).Search(query, opts...)
162 for _, hit := range qres.Hits {
163 res = append(res, plugin.SearchResult{
164 ID: hit["objectID"].(string),
165 Type: hit["type"].(string),
166 })
167 }
168
169 total = int64(qres.NbHits)
170 return res, total, err
171}
172

Callers

nothing calls this directly

Calls 1

getIndexMethod · 0.95

Tested by

no test coverage detected