# Integration tests for the phrase suggester with a few analyzers

setup:
  - do:
      indices.create:
        index: test
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 1
            analysis:
              analyzer:
                body:
                  tokenizer: standard
                  filter: [lowercase]
                bigram:
                  tokenizer: standard
                  filter: [lowercase, bigram]
                ngram:
                  tokenizer: standard
                  filter: [lowercase, ngram]
                reverse:
                  tokenizer: standard
                  filter: [lowercase, reverse]
              filter:
                bigram:
                  type: shingle
                  output_unigrams: false
                  min_shingle_size: 2
                  max_shingle_size: 2
                ngram:
                  type: shingle
                  output_unigrams: true
                  min_shingle_size: 2
                  max_shingle_size: 2
          mappings:
            properties:
              body:
                type: text
                analyzer: body
                fields:
                  bigram:
                    type: text
                    analyzer: bigram
                  ngram:
                    type: text
                    analyzer: ngram
                  reverse:
                    type: text
                    analyzer: reverse

  - do:
      bulk:
        index: test
        refresh: true
        body: |
          { "index": {} }
          { "body": "Xorr the God-Jewel" }
          { "index": {} }
          { "body": "Xorn" }
          { "index": {} }
          { "body": "Arthur, King of the Britons" }
          { "index": {} }
          { "body": "Sir Lancelot the Brave" }
          { "index": {} }
          { "body": "Patsy, Arthur's Servant" }
          { "index": {} }
          { "body": "Sir Robin the Not-Quite-So-Brave-as-Sir-Lancelot" }
          { "index": {} }
          { "body": "Sir Bedevere the Wise" }
          { "index": {} }
          { "body": "Sir Galahad the Pure" }
          { "index": {} }
          { "body": "Miss Islington, the Witch" }
          { "index": {} }
          { "body": "Zoot" }
          { "index": {} }
          { "body": "Leader of Robin's Minstrels" }
          { "index": {} }
          { "body": "Old Crone" }
          { "index": {} }
          { "body": "Frank, the Historian" }
          { "index": {} }
          { "body": "Frank's Wife" }
          { "index": {} }
          { "body": "Dr. Piglet" }
          { "index": {} }
          { "body": "Dr. Winston" }
          { "index": {} }
          { "body": "Sir Robin (Stand-in)" }
          { "index": {} }
          { "body": "Knight Who Says Ni" }
          { "index": {} }
          { "body": "Police sergeant who stops the film" }

---
"sorts by score":
  - do:
      search:
        rest_total_hits_as_int: true
        size: 0
        index: test
        body:
          suggest:
            text: xor the got-jewel
            test:
              phrase:
                field: body.ngram
                force_unigrams: true
                max_errors: 0.5
                direct_generator:
                  - field: body.ngram
                    min_word_length: 1
                    suggest_mode: always

  - match: {suggest.test.0.options.0.text: xorr the god jewel}
  - match: {suggest.test.0.options.1.text: xorn the god jewel}

---
"breaks ties by sorting terms":
  # This runs the suggester without bigrams so we can be sure of the sort order
  - do:
      search:
        rest_total_hits_as_int: true
        size: 0
        index: test
        body:
          suggest:
            text: xor the got-jewel
            test:
              phrase:
                field: body
                analyzer: body
                force_unigrams: true
                max_errors: 0.5
                direct_generator:
                  - field: body
                    min_word_length: 1
                    suggest_mode: always

  # The scores are identical but xorn comes first because it sorts first
  - match: {suggest.test.0.options.0.text: xorn the god jewel}
  - match: {suggest.test.0.options.1.text: xorr the god jewel}
  - match: {suggest.test.0.options.0.score: $body.suggest.test.0.options.0.score}

---
"fails when asked to run on a field without unigrams":
  - do:
      catch: /since it doesn't emit unigrams/
      search:
        rest_total_hits_as_int: true
        size: 0
        index: test
        body:
          suggest:
            text: xor the got-jewel
            test:
              phrase:
                field: body.bigram

  - do:
      catch: /since it doesn't emit unigrams/
      search:
        rest_total_hits_as_int: true
        size: 0
        index: test
        body:
          suggest:
            text: xor the got-jewel
            test:
              phrase:
                field: body.bigram
                analyzer: bigram

---
"doesn't fail when asked to run on a field without unigrams when force_unigrams=false":
  - do:
      search:
        rest_total_hits_as_int: true
        size: 0
        index: test
        body:
          suggest:
            text: xor the got-jewel
            test:
              phrase:
                field: body.bigram
                force_unigrams: false

  - do:
      search:
        rest_total_hits_as_int: true
        size: 0
        index: test
        body:
          suggest:
            text: xor the got-jewel
            test:
              phrase:
                field: body.bigram
                analyzer: bigram
                force_unigrams: false

---
"reverse suggestions":
  - do:
      search:
        rest_total_hits_as_int: true
        size: 0
        index: test
        body:
          suggest:
            text: Artur, Ging of the Britons
            test:
              phrase:
                field: body.ngram
                force_unigrams: true
                max_errors: 0.5
                direct_generator:
                  - field: body.reverse
                    min_word_length: 1
                    suggest_mode: always
                    pre_filter: reverse
                    post_filter: reverse

  - match: {suggest.test.0.options.0.text: arthur king of the britons}
