# How can I use match\_phrase correctly?

**URL:** https://discuss.elastic.co/t/how-can-i-use-match-phrase-correctly/352526
**Category:** Elastic Search
**Tags:** elastic-app-search
**Created:** [February 5, 2024, 11:21am UTC](https://discuss.elastic.co/t/how-can-i-use-match-phrase-correctly/352526 "2024-02-05T11:21:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Aukid](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aukid/32/131418_2.png) [@Aukid](https://discuss.elastic.co/u/Aukid)
#### Post date: [February 5, 2024, 11:21am UTC](https://discuss.elastic.co/t/how-can-i-use-match-phrase-correctly/352526/1 "2024-02-05T11:21:25Z")

</div>

Hi,I got an issue,when I post a query like this

```auto
POST /scene_dev/_search
{
  "query": {
              "match_phrase": {
            "labels": "test-qzw"
          }    
  }
} 

```

and got the response like this

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 30.4391,
    "hits": [
      {
        "_index": "scene_dev",
        "_id": "5GG9eI0Bke7j5TSbBlKj",
        "_score": 30.4391,
        "_source": {
          "labels": "test qzw"
        }
      },
      {
        "_index": "scene_dev",
        "_id": "42G8eI0Bke7j5TSbR1LQ",
        "_score": 23.916782,
        "_source": {
          "labels": "qzw-test test qzw"
        }
      }
    ]
  }
}

```

but if I just modify my query to this

```auto
POST /scene_dev/_search
{
  "query": {
              "match_phrase": {
            "labels": "qzw-test"
          }
    
  }
}

```

I'll get just the second json above,why？I I want to search the docs contains exact"qzw-test",how should I build my query?

---

<div class="post-metadata">

### Author: ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)
#### Post date: [February 5, 2024, 2:44pm UTC](https://discuss.elastic.co/t/how-can-i-use-match-phrase-correctly/352526/2 "2024-02-05T14:44:09Z")

</div>

I'm guessing you're using the default standard analyzer, which strips the hyphen and understands your string as two separate tokens. You can test this using the [analyze api](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html):

```auto
GET /_analyze
{
  "analyzer": "standard",
  "text": "test-qzw"
}

```

returns:

```auto
{
  "tokens": [
    {
      "token": "test",
      "start_offset": 0,
      "end_offset": 4,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "qzw",
      "start_offset": 5,
      "end_offset": 8,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}

```

If you want to do a text search interpreting hyphens in a different way, you'll need to use a different analyzer. Note that since these have been indexed with the standard analyzer already using a whitespace analyzer or something at search time won't work the way you want.

Here's more information on how to [specify an analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/specify-analyzer.html).

---

<div class="post-metadata">

### Author: ![Aukid](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aukid/32/131418_2.png) [@Aukid](https://discuss.elastic.co/u/Aukid)
#### Post date: [February 6, 2024, 2:05am UTC](https://discuss.elastic.co/t/how-can-i-use-match-phrase-correctly/352526/3 "2024-02-06T02:05:16Z")

</div>

Thanks for reply, so the tokenizer will still apply to my search content in `math_phrase`,but what I need is to make a search to hit "test-qzw" when I search for "test", so I still need the tokenizer to use hyphen to tokenize my string, but I alse need a way to hit only stings containing "test-qzw" but not "test qzw"  
(thanks to my god damn app scenario). Is there any possible to make this realized by Elasticsearch?

---

<div class="post-metadata">

### Author: ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)
#### Post date: [February 6, 2024, 1:23pm UTC](https://discuss.elastic.co/t/how-can-i-use-match-phrase-correctly/352526/4 "2024-02-06T13:23:16Z")

</div>

I think you could do this with a custom [tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-tokenizers.html) and [analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html) with some fiddling. But if you don't want to go down that route the only other option would be to use keyword fields which it doesn't sound like you want.

---

<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: [March 5, 2024, 1:23pm UTC](https://discuss.elastic.co/t/how-can-i-use-match-phrase-correctly/352526/5 "2024-03-05T13:23:52Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
