# Search/count for all documents for which field X contains a specific substring, with hyphens

**URL:** <https://discuss.elastic.co/t/search-count-for-all-documents-for-which-field-x-contains-a-specific-substring-with-hyphens/84330>\
**Category:** Elasticsearch\
**Created:** [May 2, 2017, 9:58pm UTC](https://discuss.elastic.co/t/search-count-for-all-documents-for-which-field-x-contains-a-specific-substring-with-hyphens/84330 "2017-05-02T21:58:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GuillaumeN](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guillaumen/32/81960_2.png) [@GuillaumeN](https://discuss.elastic.co/u/GuillaumeN)\
**Post date:** [May 2, 2017, 9:58pm UTC](https://discuss.elastic.co/t/search-count-for-all-documents-for-which-field-x-contains-a-specific-substring-with-hyphens/84330/1 "2017-05-02T21:58:02Z")

</div>

Hi all,

I'm trying to count the number of documents which fields contain a specific substring containing hyphens.

The documents were loaded using logstash to logstash-x indices, they are basically archived syslog messages.  
The indices were created using the bundled logstash elasticsearch template.  
More information on my index structure can be found here: [https://discuss.elastic.co/t/unable-to-query-keyword-fields-from-an-index-created-using-the-default-logstash-template/84185/3?u=guillaumen](https://discuss.elastic.co/t/unable-to-query-keyword-fields-from-an-index-created-using-the-default-logstash-template/84185/3?u=guillaumen)

Here are some example of syslog\_message fields values contained within my indices:

```auto
823475 bd [local7.notice] 1043983: Jan 3 00:02:01.748 EST: %MAB-5-SUCCESS: Authentication successful for client (1111111) on Interface Fa1/0/39 AuditSessionID 0A85450A00001499671B43F1
1121685 bd [local7.notice] 2083133: Jan 3 00:02:01.009 EST: %AUTHMGR-5-START: Starting 'dot1x' for client (2222222) on Interface Fa1/0/4 AuditSessionID 0AAD13FE0000462E415DA56D
912046 bd [local7.notice] 1236504: Jan 3 00:02:02.122 EST: %AUTHMGR-5-START: Starting 'mab' for client (33333333) on Interface Fa1/0/11 AuditSessionID 0A85350A0000966810D8F206

```

I would like to be able to search for all the documents which syslog\_message field contains the exact substring "-5-", for instance those 3 above should be found.

Any idea on how I could achieve this?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 3, 2017, 8:38pm UTC](https://discuss.elastic.co/t/search-count-for-all-documents-for-which-field-x-contains-a-specific-substring-with-hyphens/84330/2 "2017-05-03T20:38:45Z")

</div>

Since your field is mapped with a keyword field, the path of least resistance would be a simple wildcard. Here's an example:

```auto
PUT syslog
{
  "mappings": {
    "data": {
      "properties": {
        "syslog_message": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

POST syslog/data
{
  "syslog_message": "823475 bd [local7.notice] 1043983: Jan 3 00:02:01.748 EST: %MAB-5-SUCCESS: Authentication successful for client (1111111) on Interface Fa1/0/39 AuditSessionID 0A85450A00001499671B43F1"
}
POST syslog/data
{
  "syslog_message": "1121685 bd [local7.notice] 2083133: Jan 3 00:02:01.009 EST: %AUTHMGR-5-START: Starting 'dot1x' for client (2222222) on Interface Fa1/0/4"
}
POST syslog/data
{
  "syslog_message": "AuditSessionID 0AAD13FE0000462E415DA56D912046 bd [local7.notice] 1236504: Jan 3 00:02:02.122 EST: %AUTHMGR-5-START: Starting 'mab' for client (33333333) on Interface Fa1/0/11 AuditSessionID 0A85350A0000966810D8F206"
}

GET /syslog/data/_search
{
  "query": {
    "wildcard": {
      "syslog_message.keyword": {
        "value": "*-5-*"
      }
    }
  }
}

```

It's not the fastest query in the world since that leading wildcard is going to be expensive, but it'll get the job done without having to reindex anything.

---

<div class="post-metadata">

**Author:** ![GuillaumeN](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guillaumen/32/81960_2.png) [@GuillaumeN](https://discuss.elastic.co/u/GuillaumeN)\
**Post date:** [May 4, 2017, 2:14am UTC](https://discuss.elastic.co/t/search-count-for-all-documents-for-which-field-x-contains-a-specific-substring-with-hyphens/84330/3 "2017-05-04T02:14:04Z")

</div>

Thanks a lot Zachary!  
It works perfectly 😀

---

<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:** [June 1, 2017, 2:16am UTC](https://discuss.elastic.co/t/search-count-for-all-documents-for-which-field-x-contains-a-specific-substring-with-hyphens/84330/4 "2017-06-01T02:16:07Z")

</div>

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