setup:
  - do:
      indices.create:
        index: test
        body:
          settings:
            index:
              number_of_shards: 1     # keep scoring stable
            analysis:
              filter:
                syns:
                  type: synonym
                  synonyms: [ "wtf, what the fudge", "foo, bar baz" ]
                graph_syns:
                  type: synonym_graph
                  synonyms: [ "wtf, what the fudge", "foo, bar baz" ]
              analyzer:
                lower_syns:
                  type: custom
                  tokenizer: standard
                  filter: [ lowercase, syns ]
                lower_graph_syns:
                  type: custom
                  tokenizer: standard
                  filter: [ lowercase, graph_syns ]
          mappings:
            properties:
              field:
                type: text
              phrase_field:
                type: text
                index_phrases: true

  - do:
      index:
          index: test
          id: "1"
          body:
            text: say wtf happened foo
  - do:
      index:
          index: test
          id: "2"
          body:
            text: bar baz what the fudge man

  - do:
      index:
          index: test
          id: "3"
          body:
            text: wtf

  - do:
      index:
          index: test
          id: "4"
          body:
            text: what is the name for fudge

  - do:
      index:
          index: test
          id: "5"
          body:
            text: bar two three

  - do:
      index:
          index: test
          id: "6"
          body:
            text: bar baz two three
          refresh: true

---
"simple multiterm phrase":
  - do:
      search:
        rest_total_hits_as_int: true
        body:
          query:
            match_phrase:
              text:
                query: foo two three
                analyzer: lower_syns
  - match: { hits.total: 1 }
  - match: { hits.hits.0._id: "5" }  # incorrect match because we're not using graph synonyms

  - do:
      search:
        rest_total_hits_as_int: true
        body:
          query:
            match_phrase:
              text:
                query: foo two three
                analyzer: lower_graph_syns
  - match: { hits.total: 1 }
  - match: { hits.hits.0._id: "6" }  # correct match because we're using graph synonyms

---
"simple multiterm and":
  - do:
      search:
        rest_total_hits_as_int: true
        body:
          query:
            match:
              text:
                query: say what the fudge
                analyzer: lower_syns
                operator: and
  - match: { hits.total: 1 }
  - match: { hits.hits.0._id: "1" }   # non-graph synonyms coincidentally give us the correct answer here

  - do:
      search:
        rest_total_hits_as_int: true
        body:
          query:
            match:
              text:
                query: say what the fudge
                analyzer: lower_graph_syns
                operator: and
  - match: { hits.total: 1 }
  - match: { hits.hits.0._id: "1" }

---
"minimum should match":
  - do:
      search:
        rest_total_hits_as_int: true
        body:
          query:
            match:
              text:
                query: three what the fudge foo
                operator: or
                analyzer: lower_graph_syns
                auto_generate_synonyms_phrase_query: false
  - match: { hits.total: 6 }

  - do:
      search:
        rest_total_hits_as_int: true
        body:
          query:
            match:
              text:
                query: three what the fudge foo
                operator: or
                analyzer: lower_graph_syns
                minimum_should_match: 80%
  - match: { hits.total: 3 }
  - match: { hits.hits.0._id: "2" }
  - match: { hits.hits.1._id: "6" }
  - match: { hits.hits.2._id: "1" }

---
"multiterm synonyms phrase":
  - do:
      search:
        rest_total_hits_as_int: true
        body:
          query:
            match:
              text:
                query: wtf
                operator: and
                analyzer: lower_graph_syns
  - match: { hits.total: 3 }
  - match: { hits.hits.0._id: "2" }
  - match: { hits.hits.1._id: "3" }
  - match: { hits.hits.2._id: "1" }

---
"phrase prefix":
  - do:
      index:
          index: test
          id: "7"
          body:
            text: "WTFD!"

  - do:
      index:
          index: test
          id: "8"
          body:
            text: "Weird Al's WHAT THE FUDGESICLE"
          refresh: true

  - do:
      search:
        rest_total_hits_as_int: true
        body:
          query:
            match_phrase_prefix:
              text:
                query: wtf
                analyzer: lower_graph_syns
  - match: { hits.total: 5 }
  - match: { hits.hits.0._id: "3" }
  - match: { hits.hits.1._id: "7" }
  - match: { hits.hits.2._id: "1" }
  - match: { hits.hits.3._id: "8" }
  - match: { hits.hits.4._id: "2" }

---
"index_phrases":

  - do:
      index:
        index: test
        id: "9"
        body:
          phrase_field: "bar baz"
        refresh: true

  - do:
      search:
        rest_total_hits_as_int: true
        body:
          query:
            match:
              phrase_field:
                query: bar baz
                analyzer: lower_graph_syns
  - match: { hits.total: 1 }

