<commit_before>const fp = require('lodash/fp')

const elastic = require('./index')

const toBody = message =>
  fp.pick([
    'timestamp',
    'from',
    'to',
    'text',
  ], message)

const indexMessage = message => {
  return elastic.index({
    index: 'messages',
    type: 'message',
    id: message.id,
    body: toBody(message),
  })
}

const indexMessages = messages => {
  return elastic.bulk({
    body: fp.flatMap(message => [
      { index: { _index: 'messages', _type: 'message', _id: message.id }},
      toBody(message),
    ], messages),
  })
}

const searchMessages = async (channel, query, limit, afterTimestamp) => {
  let body = {
    size: limit,
    sort: [{ timestamp: { order: 'desc' }}],
    query: {
      bool: {
        filter: fp.concat(
          { term: { to: channel }},
          !query.nick ? [] : { term: { from: query.nick }}
        ),
        must: !query.text ? [] : [{
          match: { text: query.text },
        }],
      },
    },
  }

  if (afterTimestamp) {
    body.search_after = [+afterTimestamp]
  }
<commit_msg>Enable fuzziness + AND phrases when searching text<commit_after>const fp = require('lodash/fp')

const elastic = require('./index')

const toBody = message =>
  fp.pick([
    'timestamp',
    'from',
    'to',
    'text',
  ], message)

const indexMessage = message => {
  return elastic.index({
    index: 'messages',
    type: 'message',
    id: message.id,
    body: toBody(message),
  })
}

const indexMessages = messages => {
  return elastic.bulk({
    body: fp.flatMap(message => [
      { index: { _index: 'messages', _type: 'message', _id: message.id }},
      toBody(message),
    ], messages),
  })
}

const searchMessages = async (channel, query, limit, afterTimestamp) => {
  let body = {
    size: limit,
    sort: [{ timestamp: { order: 'desc' }}],
    query: {
      bool: {
        filter: fp.concat(
          { term: { to: channel }},
          !query.nick ? [] : { term: { from: query.nick }}
        ),
        must: !query.text ? [] : [{
          match: {
            text: {
              query: query.text,
              operator: 'and',
              fuzziness: 'auto',
            }
          },
        }],
      },
    },
  }

  if (afterTimestamp) {
    body.search_after = [+afterTimestamp]
  }

{'fraction_non_alphanumeric': 0.16892186817983412, 'fraction_numerical': 0.0, 'mean_word_length': 2.1354309165526675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2620193', 'n_tokens_mistral': 745, 'n_tokens_neox': 736, 'n_words': 169}