How to match fields starting with phrase

Say I have an analyzed field, and I want to do a search that matches on that field if the field starts with the supplied phrase. For example, say I have two documents, each with a single field:

doc1 : {
    name: "the dog is happy"
}

doc2: {
    name: "happy the dog is"
}

Say my query string is "the dog is". I want to match doc1 and not doc2. How can I do this?

Thanks!

Hi,

One idea would be to set up the field as a multi field, so you have one version for standard analyzed search and another non-analyzed field. On that you can then do e.g. a Prefix-Query:

PUT /index
{
  "mappings": {
    "type": {
      "properties": {
        "name": {
          "type": "multi_field",
          "fields": {
            "name": {
              "type": "string"
            },
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}

PUT /index/type/1
{
  "name" : "the dog is happy"
}

PUT /index/type/2
{
  "name" : "happy the dog is"
}

GET /index/type/_search
{
  "query": {
    "prefix": {
      "name.raw": {
        "value": "the dog"
      }
    }
  }
}

Hi Cristoph,

Yes, that was the only solution we could think of. However, it seems weird to have to make an non-analyzed version of the field, since the index already has all the information we need (based on the analyzed version only); namely, the words and positions.

Pete