# How to match fields starting with phrase

**URL:** <https://discuss.elastic.co/t/how-to-match-fields-starting-with-phrase/46593>\
**Category:** Elasticsearch\
**Created:** [April 6, 2016, 8:13pm UTC](https://discuss.elastic.co/t/how-to-match-fields-starting-with-phrase/46593 "2016-04-06T20:13:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeteyPabPro](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@PeteyPabPro](https://discuss.elastic.co/u/PeteyPabPro)\
**Post date:** [April 6, 2016, 8:13pm UTC](https://discuss.elastic.co/t/how-to-match-fields-starting-with-phrase/46593/1 "2016-04-06T20:13:02Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [April 6, 2016, 10:12pm UTC](https://discuss.elastic.co/t/how-to-match-fields-starting-with-phrase/46593/2 "2016-04-06T22:12:02Z")

</div>

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:

```auto
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"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![PeteyPabPro](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@PeteyPabPro](https://discuss.elastic.co/u/PeteyPabPro)\
**Post date:** [April 6, 2016, 10:40pm UTC](https://discuss.elastic.co/t/how-to-match-fields-starting-with-phrase/46593/3 "2016-04-06T22:40:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:01pm UTC](https://discuss.elastic.co/t/how-to-match-fields-starting-with-phrase/46593/4 "2017-07-05T23:01:43Z")

</div>


