# Search Exact and partial match

**URL:** <https://discuss.elastic.co/t/search-exact-and-partial-match/209282>\
**Category:** Elasticsearch\
**Created:** [November 25, 2019, 10:41am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282 "2019-11-25T10:41:13Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![KrishK](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@KrishK](https://discuss.elastic.co/u/KrishK)\
**Post date:** [November 25, 2019, 10:41am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/1 "2019-11-25T10:41:13Z")

</div>

Hi Support,

I have keyword like: 'ABC - DEF', 'ABC DEF', 'ABC is there any combination with DEF'.

Exact Match : "ABC-DEF" - Its gives perfect result ("ABC-DEF").  
Partial Match: "ABC-DE" - its gives more result (all 3 document). Last one should not come in search.

Can you please help me on above query?

How can get perfect result?

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:** [November 25, 2019, 5:45pm UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/2 "2019-11-25T17:45:50Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![KrishK](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@KrishK](https://discuss.elastic.co/u/KrishK)\
**Post date:** [December 3, 2019, 9:44am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/3 "2019-12-03T09:44:47Z")

</div>

Hi David,

Thanks for reply.

What type of full reproduction script you want? you mean to say mappings or settings or content?

```
DELETE testindex

PUT testindex

PUT testindex/_mapping
{
  "properties": {
    "filename": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}
POST testindex/_doc
{
  "filename": "704140-0001 FIT TO P1.pdf"
}
POST testindex/_doc
{
  "filename": "704140 FIT TO P4 MARCH 1994 DATA SUBMITTLE -0051.pdf"
}
POST testindex/_doc
{
  "filename": "704140 FIT TO P1 OCTOBER 2003 POINT OF FIT -0001.pdf"
}

POST _sql?format=txt
{
"query":""" SELECT filename FROM testindex WHERE QUERY('(filename:("704140"))')"""
}

```

it gives perfect exact match result.

```
POST _sql?format=txt
{
"query":""" SELECT filename FROM testindex WHERE QUERY('(filename:(70414*))')"""
}

```

it gives partial match result for single word.

```
POST _sql?format=txt
{
"query":""" SELECT filename FROM testindex WHERE QUERY('(filename:("704140-0001"))')"""
}

```

it gives perfect exact match result.

```
POST _sql?format=txt
{
"query":""" SELECT filename FROM testindex WHERE QUERY('(filename:(704140-00*))')"""
}

```

it gives no result.

I want partial match search. how it is possible?

Can you please help me?

Thanks

---

<div class="post-metadata">

**Author:** ![KrishK](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@KrishK](https://discuss.elastic.co/u/KrishK)\
**Post date:** [December 4, 2019, 4:42am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/4 "2019-12-04T04:42:05Z")

</div>

Any hope?

---

<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:** [December 4, 2019, 8:48am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/5 "2019-12-04T08:48:29Z")

</div>

Here is a way to fix it. Note that with this analyzer it's now case sensitive.  
If you don't want that, you'll need to provide a [custom analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html).  
Also `704140-0001` won't match anymore when you query with `704140`.

```auto
DELETE testindex
PUT testindex
{
  "mappings": {
    "properties": {
      "filename": {
        "type": "text",
        "analyzer": "whitespace"
      }
    }
  }
}
POST testindex/_doc
{
  "filename": "704140-0001 FIT TO P1.pdf"
}
POST testindex/_doc
{
  "filename": "704140 FIT TO P4 MARCH 1994 DATA SUBMITTLE -0051.pdf"
}
POST testindex/_doc
{
  "filename": "704140 FIT TO P1 OCTOBER 2003 POINT OF FIT -0001.pdf"
}

POST _sql?format=txt
{
"query":""" SELECT filename FROM testindex WHERE QUERY('(filename:("704140"))')"""
}
POST _sql?format=txt
{
"query":""" SELECT filename FROM testindex WHERE QUERY('(filename:(70414*))')"""
}
POST _sql?format=txt
{
"query":""" SELECT filename FROM testindex WHERE QUERY('(filename:(704140-00*))')"""
}

```

For more information, use the `_analyze` API to understand how everything is working behind the scene.

---

<div class="post-metadata">

**Author:** ![KrishK](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@KrishK](https://discuss.elastic.co/u/KrishK)\
**Post date:** [December 9, 2019, 8:44am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/6 "2019-12-09T08:44:39Z")

</div>

Thanks David,

Actually, i want to use filename column for full text search and keyword search.

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:** [December 9, 2019, 9:39am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/7 "2019-12-09T09:39:19Z")

</div>

Any example of the text you want to search for and the result you want?

---

<div class="post-metadata">

**Author:** ![KrishK](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@KrishK](https://discuss.elastic.co/u/KrishK)\
**Post date:** [December 9, 2019, 10:13am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/8 "2019-12-09T10:13:56Z")

</div>

Example here :

```
PUT testindex/_mapping
{
  "properties": {
    "filecontent": {
      "type": "text"
    },
    "filename": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}

POST testindex/_doc
{
  "filename": "704140-0001 FIT TO P1.pdf",
   "filecontent": "This is PO : 000704140"
}
POST testindex/_doc
{
  "filename": "704140 FIT TO P4 MARCH 1994 DATA SUBMITTLE -0051.pdf",
  "filecontent": "This is PO : 704140"
}

POST _sql?format=txt
{
"query":""" SELECT filename FROM testindex WHERE QUERY('(filecontent:(704140))') """
}

POST _sql?format=txt
{
"query":""" SELECT filename FROM testindex WHERE QUERY('(filecontent:(704140)) OR (filename:(704140))') """
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![KrishK](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@KrishK](https://discuss.elastic.co/u/KrishK)\
**Post date:** [December 10, 2019, 4:24am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/9 "2019-12-10T04:24:56Z")

</div>

Hi David,

I have provided example on above reply.  
Can you please help me on the same?

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:** [December 11, 2019, 11:35pm UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/10 "2019-12-11T23:35:47Z")

</div>

What do you expect to get back?

---

<div class="post-metadata">

**Author:** ![KrishK](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@KrishK](https://discuss.elastic.co/u/KrishK)\
**Post date:** [December 12, 2019, 8:58am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/11 "2019-12-12T08:58:18Z")

</div>

Hi David,

I expect that,

1. When I search document with keyword "704140-0001 FIT TO P1.pdf", it should search in filename(keyword).

2. When I search document with keyword "704140", it should search in filename(keyword and text).

I have mappings same as above, but my question is only how I search exact and partial search with keyword like "704140-00\*".

Its already working in sphinx environment. before turn into elastic search, I want to clear my doubt. it will work or not? if its work then how can achieve?

Thanks

---

<div class="post-metadata">

**Author:** ![KrishK](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@KrishK](https://discuss.elastic.co/u/KrishK)\
**Post date:** [December 23, 2019, 6:21am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/12 "2019-12-23T06:21:54Z")

</div>

Any Solution?

---

<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:** [January 7, 2020, 4:41pm UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/13 "2020-01-07T16:41:27Z")

</div>

How do you determine in which field you should search based on the user input?

I mean that I have hard time to understand the use case.  
You have one search box, right?

Why should it search in different fields in one case or another?  
Why not searching in both fields every time?

The more fields which match, better the score will be so best documents will be on the top of the response list.

---

<div class="post-metadata">

**Author:** ![KrishK](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@KrishK](https://discuss.elastic.co/u/KrishK)\
**Post date:** [January 10, 2020, 7:07am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/14 "2020-01-10T07:07:06Z")

</div>

Hi David,

Thanks for the reply.

> How do you determine in which field you should search based on the user input?

Yes - based on user selection.  
I gave 2 input box with check box [include filename too] selection. so user can search in filename too.

> You have one search box, right?

Yes

> Why should it search in different fields in one case or another?

Because we have existing search functionality in sphinx search.

> Why not searching in both fields every time?

User needs to search either in content or in filename OR in both fields.

I hope you understood now.

Can you please guide me in correct way?

How mapping should be for exact and partial match?

Cases:  
1 - If search type Partial with "ABCD-DEFG", result should come with "ABCD-DEFG" and "ABCD-DEFGHI".

2 - If search type Exact with "ABCD-DEFG", result should come with "ABCD-DEFG".

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:** [January 10, 2020, 7:35am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/15 "2020-01-10T07:35:50Z")

</div>

So if I'm trying to sum up:

- The user has 1 input text field `content` and a check box `include_filename_too` in the form.
- If the user search for `foo` without `include_filename_too` checked, it should search for `foo` only in `filecontent`.
- If the user search for `foo` with `include_filename_too` checked, it should search for `foo` in `filecontent` and `filename`.

Am I correct?

If I am correct, I don't understand how this works:

> [@KrishK](#):
>
> - When I search document with keyword "704140-0001 FIT TO P1.pdf", it should search in filename(keyword).
> - When I search document with keyword "704140", it should search in filename(keyword and text).

How do you know that you should search (`keyword`) or (`keyword` and `text`)?

> [@KrishK](#):
>
> Because we have existing search functionality in sphinx search.

Yeah but may be you could also think of giving the user a better experience. I mean that without having the user to click on a checkbox like `include_filename_too`. Just search everywhere and use the scoring system to give first the more relevant results to the user. Just like you do everyday with google.

---

<div class="post-metadata">

**Author:** ![KrishK](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@KrishK](https://discuss.elastic.co/u/KrishK)\
**Post date:** [January 10, 2020, 7:44am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/16 "2020-01-10T07:44:38Z")

</div>

> [@dadoonet](#):
>
> - The user has 1 input text field `content` and a check box `include_filename_too` in the form.
> - If the user search for `foo` without `include_filename_too` checked, it should search for `foo` only in `filecontent` .
> - If the user search for `foo` with `include_filename_too` checked, it should search for `foo` in `filecontent` and `filename` .

Yes, you understood perfect.

Now Just need to understand, above mapping will work ?

With these cases:  
1 - If search type Partial with "ABCD-DEFG", result should come with "ABCD-DEFG" and "ABCD-DEFGHI".

2 - If search type Exact with "ABCD-DEFG", result should come with "ABCD-DEFG".

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:** [January 10, 2020, 7:55am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/17 "2020-01-10T07:55:33Z")

</div>

But you did not answer this question:

> [@dadoonet](#):
>
> How do you know that you should search ( `keyword` ) or ( `keyword` and `text` )?

---

<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:** [February 7, 2020, 7:57am UTC](https://discuss.elastic.co/t/search-exact-and-partial-match/209282/18 "2020-02-07T07:57:01Z")

</div>

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