# Searching for multiple hyphenated terms

**URL:** https://discuss.elastic.co/t/searching-for-multiple-hyphenated-terms/89997
**Category:** Elasticsearch
**Created:** [June 19, 2017, 9:36pm UTC](https://discuss.elastic.co/t/searching-for-multiple-hyphenated-terms/89997 "2017-06-19T21:36:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![lrietze](https://avatars.discourse-cdn.com/v4/letter/l/e8c25b/32.png) [@lrietze](https://discuss.elastic.co/u/lrietze)
#### Post date: [June 19, 2017, 9:36pm UTC](https://discuss.elastic.co/t/searching-for-multiple-hyphenated-terms/89997/1 "2017-06-19T21:36:49Z")

</div>

Hi all,

I am new to the ELK stack, and am in need of some help with querying with Elasticsearch. In essence what I am trying to do is perform a query like such:

- select \* from source where (message = abc-def OR def-ghi OR abc-jkl OR ...)

The problem I am having though is I can get to a point where I can successfully find matches for just one hyphenated tern, but not multiples. I was wondering if there is a query related solution that wouldn't involve me skipping analysing the field, or changing the mappings?

Thanks!

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 19, 2017, 10:13pm UTC](https://discuss.elastic.co/t/searching-for-multiple-hyphenated-terms/89997/2 "2017-06-19T22:13:37Z")

</div>

You can try a match phrase with "abc-def" or "abc def". That should work in your case.

But the best option is to define the right mapping/analyzer for your fields.

---

<div class="post-metadata">

### Author: ![lrietze](https://avatars.discourse-cdn.com/v4/letter/l/e8c25b/32.png) [@lrietze](https://discuss.elastic.co/u/lrietze)
#### Post date: [June 20, 2017, 3:44pm UTC](https://discuss.elastic.co/t/searching-for-multiple-hyphenated-terms/89997/3 "2017-06-20T15:44:26Z")

</div>

Thanks for the quick reply dadoonet. The match phrase helped me match on one term. I ended up following the idea in the ["Combining Filters" guide](https://www.elastic.co/guide/en/elasticsearch/guide/current/combining-filters.html) which - with the match phrase - seems to have solved my problem.

> GET \_search  
> {  
> "size": 5000,  
> "query": {  
> "bool": {  
> "must": {  
> "bool": {  
> "should": [  
> {  
> "match":{  
> "message":{  
> "query":"abc-def",  
> "operator": "AND"  
> }  
> }  
> },  
> {  
> "match":{  
> "message":{  
> "query":"abc-ghi",  
> "operator": "AND"  
> }  
> }  
> },  
> {  
> "match":{  
> "message":{  
> "query":"abc-jkl",  
> "operator": "AND"  
> }  
> }  
> },  
> {  
> "match":{  
> "message":{  
> "query":"def - 34",  
> "operator": "AND"  
> }  
> }  
> },  
> {  
> "match":{  
> "message":{  
> "query":"hij - 27",  
> "operator": "AND"  
> }  
> }  
> },  
> {  
> "match":{  
> "message":{  
> "query":"31083",  
> "operator": "AND"  
> }  
> }  
> },  
> {  
> "match":{  
> "message":{  
> "query":"31070",  
> "operator": "AND"  
> }  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 20, 2017, 4:18pm UTC](https://discuss.elastic.co/t/searching-for-multiple-hyphenated-terms/89997/4 "2017-06-20T16:18:29Z")

</div>

Well. You are doing match phrase but match all the terms whatever the order is.

Which means that abc def will match as well as def abc.

Not exactly a phrase here

---

<div class="post-metadata">

### Author: ![lrietze](https://avatars.discourse-cdn.com/v4/letter/l/e8c25b/32.png) [@lrietze](https://discuss.elastic.co/u/lrietze)
#### Post date: [June 20, 2017, 5:53pm UTC](https://discuss.elastic.co/t/searching-for-multiple-hyphenated-terms/89997/5 "2017-06-20T17:53:04Z")

</div>

Hmm, I guess I haven't run into a case where they are flipped because I don't have that in my test data. Any suggestions on how to fix my match phrase? Do I need to create a custom analyser? (I'm not entirely familiar with the analyser / mapping features)

Thanks!

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 20, 2017, 9:07pm UTC](https://discuss.elastic.co/t/searching-for-multiple-hyphenated-terms/89997/6 "2017-06-20T21:07:00Z")

</div>

As I said, "the best option is to define the right mapping/analyzer for your fields."

Use a `keyword` type instead of `text`. Then you will only be able to search for the full and complete string.

```auto
DELETE test
PUT test 
{
  "mappings": {
    "doc": {
      "properties": {
        "message": {
          "type": "keyword"
        }
      }
    }
  }
}
PUT test/doc/1
{
  "message": "abc-def"
}

```

Then try to use a match query with `abc-def` (should match) and `def-abc` (should not match).

HTH

---

<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 18, 2017, 9:07pm UTC](https://discuss.elastic.co/t/searching-for-multiple-hyphenated-terms/89997/7 "2017-07-18T21:07:19Z")

</div>

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