<commit_before>_ = require 'underscore'
sd = require('sharify').data
Backbone = require 'backbone'
SearchResult = require '../../../models/search_result.coffee'

class Results extends Backbone.Collection
  model: SearchResult

module.exports = class Search
  constructor: (options = {}) ->
    { @mode, @restrictType, @fairId, @includePrivateResults } = options
    @results = new Results

  url: ->
    [
      "#{sd.API_URL}/api/v1/match/"
      @mode or ''
      '?visible_to_public=true'
      if @fairId then "&fair_id=#{@fairId}" else ''
      '&term=%QUERY'
      "&size=3"
    ].join ''

  parseResults: (items) ->
    if @restrictType?
      @restrictType = [@restrictType] unless _.isArray @restrictType
      _.filter items, (item) =>
        _.contains @restrictType, item?.owner_type
    else
      items

  parse: (items, query) ->
    # Remove tags
    items = _.filter(items, (item) ->
      item.model != 'tag'
    )

    @results.reset _.map @parseResults(items), (item) =>
      item.model = @mode?.slice(0,-1) unless item.model?
      item<commit_msg>switch to new autocomplete api<commit_after>_ = require 'underscore'
sd = require('sharify').data
Backbone = require 'backbone'
SearchResult = require '../../../models/search_result.coffee'

class Results extends Backbone.Collection
  model: SearchResult

module.exports = class Search
  constructor: (options = {}) ->
    { @mode, @restrictType, @fairId, @includePrivateResults } = options
    @results = new Results

  url: ->
    [
      "#{sd.API_URL}/api/v1/match/suggest"
      @mode or ''
      '?visible_to_public=true'
      if @fairId then "&fair_id=#{@fairId}" else ''
      '&term=%QUERY'
      "&size=3"
    ].join ''

  parseResults: (items) ->
    if @restrictType?
      @restrictType = [@restrictType] unless _.isArray @restrictType
      _.filter items, (item) =>
        _.contains @restrictType, item?.owner_type
    else
      items

  parse: (items, query) ->
    # Remove tags
    items = _.filter(items, (item) ->
      item.model != 'tag'
    )

    @results.reset _.map @parseResults(items), (item) =>
      item.model = @mode?.slice(0,-1) unless item.model?
      item
{'fraction_non_alphanumeric': 0.15327793167128348, 'fraction_numerical': 0.003693444136657433, 'mean_word_length': 2.8490230905861456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4884289', 'n_tokens_mistral': 718, 'n_tokens_neox': 699, 'n_words': 161}