func performIndexSearch(ctx context.Context, userQuery string, options *SearchOptions) ([]Item, map[string]RefinementCollection, error) { | |
index, err := search.Open(searchIndexName) | |
if err != nil { | |
return nil, nil, errors.Wrap(err, "Search Index Unavailable") | |
} | |
var keys []*datastore.Key | |
query := "" | |
searchFragments := strings.Split(userQuery, " ") | |
for _, fragment := range searchFragments { | |
var n int | |
n, err = strconv.Atoi(fragment) | |
if err == nil { | |
query = fmt.Sprintf("%s (Year > %d Year < %d) ", query, n-searchYearDelta, n+searchYearDelta) | |
} else { | |
query = fmt.Sprintf("%s ~%s", query, fragment) | |
} | |
} | |
if options != nil { | |
if itemType, ok := options.StringParameters[searchOptionType]; ok { | |
query = query + "ListingType: " + itemType | |
} | |
if profileID, ok := options.IntParameters[searchOptionProfileID]; ok { | |
query = query + "ProfileID: " + strconv.Itoa(profileID) | |
} | |
} | |
log.Debugf(ctx, "Final Query: %s", query) | |
iterator := index.Search(ctx, query, &search.SearchOptions{ | |
IDsOnly: true, | |
Facets: []search.FacetSearchOption{ | |
search.AutoFacetDiscovery(0, 0), | |
}, | |
}) | |
facets, err := iterator.Facets() | |
for t := iterator; ; { | |
var doc SearchableItem | |
id, sErr := t.Next(&doc) | |
if sErr == search.Done { | |
break | |
} | |
if sErr != nil { | |
return nil, nil, errors.Wrap(sErr, "Search Error") | |
} | |
intID, _ := strconv.Atoi(id) | |
if intID != 0 { | |
log.Debugf(ctx, "Appending ID %#v", id) | |
tmpKey := datastore.NewKey(ctx, kindItem, "", int64(intID), ancestorListingKey(ctx, kindItem)) | |
keys = append(keys, tmpKey) | |
} | |
} | |
log.Debugf(ctx, "Facet Count %d", len(facets)) | |
items := make([]Item, len(keys)) | |
err = nds.GetMulti(ctx, keys, items) | |
if err != nil { | |
return nil, nil, errors.Wrap(err, "Get Multi on Search Results Failed") | |
} | |
items, refinements, err := enhanceAndRefineResults(ctx, items, keys, facets) | |
if err != nil { | |
return nil, nil, errors.Wrap(err, "Error occurred while producing search refinements") | |
} | |
return items, refinements, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment