# This test creates one huge mapping in the setup
# Every test should use its own field to make sure it works

setup:

  - do:
      indices.create:
          index: test
          body:
            mappings:
              test:
                "properties":
                  "location":
                    "type": "geo_point"
                  "suggest_context":
                     "type" : "completion"
                     "contexts":
                        -
                          "name" : "color"
                          "type" : "category"
                  "suggest_context_with_path":
                     "type" : "completion"
                     "contexts":
                        -
                          "name" : "color"
                          "type" : "category"
                          "path" : "color"
                  "suggest_geo":
                     "type" : "completion"
                     "contexts":
                        -
                          "name" : "location"
                          "type" : "geo"
                          "precision" : "5km"
                  "suggest_multi_contexts":
                     "type" : "completion"
                     "contexts":
                        -
                          "name" : "location"
                          "type" : "geo"
                          "precision" : "5km"
                          "path" : "location"
                        -
                          "name" : "color"
                          "type" : "category"
                          "path" : "color"

---
"Simple context suggestion should work":

  - do:
      index:
        index: test
        type:  test
        id:    1
        body:
          suggest_context:
            input: "foo red"
            contexts:
              color: "red"

  - do:
      index:
        index: test
        type:  test
        id:    2
        body:
          suggest_context:
            input: "foo blue"
            contexts:
              color: "blue"

  - do:
      indices.refresh: {}

  - do:
      search:
        body:
          suggest:
            result:
              text: "foo"
              completion:
                field: suggest_context
                contexts:
                  color: "red"

  - length: { suggest.result: 1  }
  - length: { suggest.result.0.options: 1  }
  - match:  { suggest.result.0.options.0.text: "foo red" }

---
"Category suggest context from path should work":

  - do:
      index:
        index: test
        type:  test
        id:    1
        body:
          suggest_context_with_path:
            input: "Foo red"
            contexts:
              color: "red"

  - do:
      index:
        index: test
        type:  test
        id:    2
        body:
          suggest_context_with_path: "Foo blue"
          color: "blue"

  - do:
      indices.refresh: {}

  - do:
      search:
        body:
          suggest:
            result:
              text: "foo"
              completion:
                field: suggest_context_with_path
                contexts:
                  color: "red"

  - length: { suggest.result: 1  }
  - length: { suggest.result.0.options: 1  }
  - match:  { suggest.result.0.options.0.text: "Foo red" }

  - do:
      search:
        body:
          suggest:
            result:
              text: "foo"
              completion:
                field: suggest_context_with_path
                contexts:
                  color: "blue"

  - length: { suggest.result: 1  }
  - length: { suggest.result.0.options: 1  }
  - match:  { suggest.result.0.options.0.text: "Foo blue" }

  - do:
      search:
        body:
          suggest:
            result:
              text: "foo"
              completion:
                field: suggest_context_with_path
                contexts:
                  color: ["blue", "red"]

  - length: { suggest.result: 1  }
  - length: { suggest.result.0.options: 2  }

---
"Geo suggest should work":

  - do:
      index:
        index: test
        type:  test
        id:    1
        body:
          suggest_geo:
            input: "Marriot in Amsterdam"
            contexts:
              location:
                lat : 52.22
                lon : 4.53

  - do:
      index:
        index: test
        type:  test
        id:    2
        body:
          suggest_geo:
            input: "Marriot in Berlin"
            contexts:
              location:
                lat : 53.31
                lon : 13.24

  - do:
      indices.refresh: {}

  - do:
      indices.get_mapping: {}

  - do:
      search:
        index: test
        body:
          suggest:
            result:
              text: "mar"
              completion:
                field: suggest_geo
                contexts:
                  location:
                    lat : 52.2263
                    lon : 4.543

  - length: { suggest.result: 1  }
  - length: { suggest.result.0.options: 1  }
  - match:  { suggest.result.0.options.0.text: "Marriot in Amsterdam" }

---
"Multi contexts should work":

  - do:
      index:
        index: test
        type:  test
        id:    1
        body:
          suggest_multi_contexts: "Marriot in Amsterdam"
          location:
            lat : 52.22
            lon : 4.53
          color: "red"

  - do:
      index:
        index: test
        type:  test
        id:    2
        body:
          suggest_multi_contexts: "Marriot in Berlin"
          location:
            lat : 53.31
            lon : 13.24
          color: "blue"

  - do:
      indices.refresh: {}

  - do:
      indices.get_mapping: {}

  - do:
      search:
        index: test
        body:
          suggest:
            result:
              text: "mar"
              completion:
                field: suggest_multi_contexts
                contexts:
                  location:
                    lat : 52.22
                    lon : 4.53

  - length: { suggest.result: 1  }
  - length: { suggest.result.0.options: 1  }
  - match:  { suggest.result.0.options.0.text: "Marriot in Amsterdam" }

  - do:
      search:
        index: test
        body:
          suggest:
            result:
              text: "mar"
              completion:
                field: suggest_multi_contexts
                contexts:
                  color: "blue"

  - length: { suggest.result: 1  }
  - length: { suggest.result.0.options: 1  }
  - match:  { suggest.result.0.options.0.text: "Marriot in Berlin"  }

---
"Skip duplicates with contexts should work":
  - skip:
      version: " - 6.0.99"
      reason: skip_duplicates was added in 6.1

  - do:
      index:
        index: test
        type:  test
        id:    1
        body:
          suggest_context:
            input: "foo"
            contexts:
              color: "red"

  - do:
      index:
        index: test
        type:  test
        id:    1
        body:
          suggest_context:
            input: "foo"
            contexts:
              color: "red"

  - do:
      index:
        index: test
        type:  test
        id:    2
        body:
          suggest_context:
            input: "foo"
            contexts:
              color: "blue"

  - do:
      indices.refresh: {}

  - do:
      search:
        body:
          suggest:
            result:
              text: "foo"
              completion:
                field: suggest_context
                skip_duplicates: true
                contexts:
                  color: "red"

  - length: { suggest.result: 1  }
  - length: { suggest.result.0.options: 1  }
  - match:  { suggest.result.0.options.0.text: "foo" }

  - do:
       search:
        body:
          suggest:
            result:
              text: "foo"
              completion:
                skip_duplicates: true
                field: suggest_context

  - length: { suggest.result: 1  }
  - length: { suggest.result.0.options: 1  }
  - match:  { suggest.result.0.options.0.text: "foo" }
