# How to search users by first, last name

**URL:** <https://discuss.elastic.co/t/how-to-search-users-by-first-last-name/198489>\
**Category:** Elasticsearch\
**Created:** [September 6, 2019, 11:30pm UTC](https://discuss.elastic.co/t/how-to-search-users-by-first-last-name/198489 "2019-09-06T23:30:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![undefinedman](https://avatars.discourse-cdn.com/v4/letter/u/6bbea6/32.png) [@undefinedman](https://discuss.elastic.co/u/undefinedman)\
**Post date:** [September 6, 2019, 11:30pm UTC](https://discuss.elastic.co/t/how-to-search-users-by-first-last-name/198489/1 "2019-09-06T23:30:12Z")

</div>

Hey Elasticsearch,

a year ago I wrote a topic here [What is the best way to query first and last name?](https://discuss.elastic.co/t/what-is-the-best-way-to-query-first-and-last-name/136142) to help me out with searching users in my database. At that time, I partially resolved the problem.

Now, I found this topic [How i search firstname + lastname in elasticsearch](https://discuss.elastic.co/t/how-i-search-firstname-lastname-in-elasticsearch/181807) written by @[arunkvinam](https://discuss.elastic.co/u/arunkvinam) where he did not find the solution.

I believe there are some experts in Elasticsearch forum, so I hope they will be able to answer few my questions.

I dag entire internet and there is no single example on how to search users.

I have one field "Search" where you can enter anything and it should find user based on the string you have written. However, the more chars you write, the more exact the result should be and:

**EXAMPLE 1** :

- "An" should return
  1. **An** na Abc (starts with `An`, high score)
  2. George **An** nado (again, high score)
  3. John van **An** nabis (high score, because third word starts with `An`)
  4. Adri **an** a Xyz (`an` is placed inside the word, low score)
  5. Jo **an** na Def (same as above)
  6. Michael Jo **an** (same as above)

What ElasticSearch does in this case it **returns properly**  **1 and 2 only** because the word starts with `An`, however 3, 4, 5 and 6 are not included in the output.

**EXAMPLE 2** :

- "Anna" should return
  1. **Anna** Abc (matches whole word, highest score)
  2. George **Anna** do (starts with `Anna`, high score)
  3. John van **Anna** bis (high score, because third word starts with `Anna`)
  4. Jo **anna** Def (matches part of the word, low score)

What ElasticSearch does in this case it **returns properly 1 and 2 only** because the word matches `Anna`, however 3 and 4 is not included in the response.

In perfect wor(l)d it should hit **highest score** ( **Anna** Abc) when full phrase matched the word, **high score** ( **An** na Abc) when beginning of the word is matching the word and **low score** (Jo **an** na Def) when phrase is inside the word.

Here is my configuration:

```
indexes:
app:
  settings:
    index:
      analysis:
        analyzer:
          search_analyzer:
            type: custom
            tokenizer: tokenizer
            filter: [lowercase, asciifolding]
        filter:
          asciifolding:
            type: asciifolding
            preserve_original: true
        tokenizer:
          tokenizer:
            type: keyword

```

....

```
        user:
          type: object
          properties:
            firstName:
              type: string
              analyzer: search_analyzer
            lastName:
              type: string
              analyzer: search_analyzer

```

and the query is defined as follows:

```
$boolQuery = new Query\BoolQuery();
$boolQuery
    ->addMust((new Query\MultiMatch())
    ->setFields(['firstName', 'lastName'])
    ->setType(Query\MultiMatch::TYPE_PHRASE_PREFIX)
    ->setOperator(Query\MultiMatch::OPERATOR_OR)
    ->setQuery('Anna')
);

$query = new Query();
$query->setQuery($boolQuery);
$result = $this->finder->find($query);

```

Would be super cool to get the results as closed as possible using Elasticsearch because there is no any example, how to make it perfectly when it comes to searching users.

Currently I switched back to MySQL LIKE query because it does the job pretty okay (not perfect) but I would like to have this done using Elasticsearch.

I hope I will get some advises.

---

<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:** [September 7, 2019, 2:57am UTC](https://discuss.elastic.co/t/how-to-search-users-by-first-last-name/198489/2 "2019-09-07T02:57:53Z")

</div>

Not exactly the answer for your use case but here is an example which combines at the end multiple strategies from partial match to exact phrases. That could help you hopefully.

> <https://gist.github.com/dadoonet/5179ee72ecbf08f12f53d4bda1b76bab>

---

<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:** [October 5, 2019, 2:57am UTC](https://discuss.elastic.co/t/how-to-search-users-by-first-last-name/198489/3 "2019-10-05T02:57:59Z")

</div>

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