# Retrieve Search Results Based on the First Letter in a Specific Field

**URL:** <https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852>\
**Category:** Elasticsearch\
**Created:** [June 14, 2019, 12:11pm UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852 "2019-06-14T12:11:06Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![maryam\_abdullah](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@maryam\_abdullah](https://discuss.elastic.co/u/maryam_abdullah)\
**Post date:** [June 21, 2019, 6:52am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/21 "2019-06-21T06:52:19Z")

</div>

I installed the latest version and now it's working fine.

---

<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 21, 2019, 6:58am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/22 "2019-06-21T06:58:57Z")

</div>

Really? I don't remember my script was giving back both documents. Are you sure you tested it? Might be me though.

---

<div class="post-metadata">

**Author:** ![maryam\_abdullah](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@maryam\_abdullah](https://discuss.elastic.co/u/maryam_abdullah)\
**Post date:** [June 21, 2019, 7:05am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/23 "2019-06-21T07:05:18Z")

</div>

Yes, I did.  
The following is the result:

```
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "en",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "Articles"
        }
      },
      {
        "_index" : "en",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "Foo Articles"
        }
      }
    ]
  }
}
```

---

<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 21, 2019, 7:35am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/24 "2019-06-21T07:35:59Z")

</div>

Right. Sorry. Here is the right one:

```auto
DELETE en
PUT en
{
  "settings": {
    "analysis": {
      "analyzer": {
        "lowercase": {
          "type": "custom", 
          "tokenizer": "keyword",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "lowercase"
      }
    }
  }
}

PUT en/_doc/1
{
  "title": "Articles"
}
PUT en/_doc/2
{
  "title": "Foo Articles"
}

GET /_search
{
  "query": {
    "prefix": {
      "title": "a"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![maryam\_abdullah](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@maryam\_abdullah](https://discuss.elastic.co/u/maryam_abdullah)\
**Post date:** [June 21, 2019, 7:38am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/25 "2019-06-21T07:38:10Z")

</div>

Look I noticed that I need to use the keyword type for the field so that the prefix query works.

```
PUT en2
{
  "mappings": {
"properties": {
  "title": {
    "type": "keyword"
  }
}
  }
}

PUT en2/_doc/1
{
  "title": "Articles"
}
PUT en2/_doc/2
{
  "title": "Foo Articles"
}

GET en2/_search
{
  "query": {
    "prefix": {
      "title": "A"
    }
  }
}

```

I need the title to have 2 different analyzers and as stated above, it needs to be defined as a keyword. How is that even possible?  
Also, I noticed that if the query is 'a' as in lowercase, then no documents will be returned.

---

<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 21, 2019, 7:40am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/26 "2019-06-21T07:40:28Z")

</div>

> Also, I noticed that if the query is 'a' as in lowercase, then no documents will be returned.

Check the code I pasted.

---

<div class="post-metadata">

**Author:** ![maryam\_abdullah](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@maryam\_abdullah](https://discuss.elastic.co/u/maryam_abdullah)\
**Post date:** [July 1, 2019, 7:16am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/27 "2019-07-01T07:16:07Z")

</div>

Okay, Thanks for your help.

In case I need to update my code to include this, I need to use the multi fields, is that correct?

```
PUT en
{
  "mappings": {
"properties": {
  "Id": {
    "type": "text"
  },
  "Title": {
    "type": "text",
    "analyzer": "english_analyzer",
    "fields": {
      "exact": {
        "type": "text",
        "analyzer": "english_standard"
      },
"first_letter": {
"type": "text",
"analyzer": "lowercase"
}
    }
  }
}
  },
  "settings": {
"number_of_shards": 5,
"analysis": {
  "filter": {
    "english_stop": {
      "type": "stop",
      "stopwords": "_english_"
    },
    "english_stemmer": {
      "type": "stemmer",
      "language": "english"
    },
    "english_possessive_stemmer": {
      "type": "stemmer",
      "language": "possessive_english"
    }
  },
  "analyzer": {
    "english_analyzer": {
      "tokenizer": "standard",
      "filter": [
        "english_possessive_stemmer",
        "lowercase",
        "english_stop",
        "english_stemmer"
      ]
    },
"lowercase": {
      "type": "custom", 
      "tokenizer": "keyword",
      "filter": [
        "lowercase"
      ]
    },
    "english_standard": {
      "tokenizer": "standard",
      "filter": [
        "english_possessive_stemmer",
        "lowercase"
      ]
    }
  }
}
  }
}
```

---

<div class="post-metadata">

**Author:** ![maryam\_abdullah](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@maryam\_abdullah](https://discuss.elastic.co/u/maryam_abdullah)\
**Post date:** [July 1, 2019, 7:21am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/28 "2019-07-01T07:21:50Z")

</div>

but it isn't returning any results.

```
    PUT en/_doc/1
{
  "title": "Articles"
}
PUT en/_doc/2
{
  "title": "Foo Articles"
}

GET en/_search
{
  "query": {
    "prefix": {
      "Title.first_letter": "a"
    }
  }
}
```

---

<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:** [July 1, 2019, 11:19am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/29 "2019-07-01T11:19:47Z")

</div>

It works for me if you use the following instead:

```auto
PUT en/_doc/1
{
  "Title": "Articles"
}
PUT en/_doc/2
{
  "Title": "Foo Articles"
}

```

---

<div class="post-metadata">

**Author:** ![maryam\_abdullah](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@maryam\_abdullah](https://discuss.elastic.co/u/maryam_abdullah)\
**Post date:** [July 1, 2019, 11:50am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/30 "2019-07-01T11:50:32Z")

</div>

Yes!  
Thank you for your time:)

---

<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 29, 2019, 11:50am UTC](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852/31 "2019-07-29T11:50:32Z")

</div>

Closed on July 29, 2019, 11:50am UTC

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

[Previous page](https://discuss.elastic.co/t/retrieve-search-results-based-on-the-first-letter-in-a-specific-field/185852.md?page=1)
