# How should I write this statement?

**URL:** <https://discuss.elastic.co/t/how-should-i-write-this-statement/371584>\
**Category:** Elasticsearch\
**Created:** [December 6, 2024, 9:30am UTC](https://discuss.elastic.co/t/how-should-i-write-this-statement/371584 "2024-12-06T09:30:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nIxedoahz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nixedoahz/32/139741_2.png) [@nIxedoahz](https://discuss.elastic.co/u/nIxedoahz)\
**Post date:** [December 6, 2024, 9:30am UTC](https://discuss.elastic.co/t/how-should-i-write-this-statement/371584/1 "2024-12-06T09:30:13Z")

</div>

# Create an index

···  
PUT /new\_index  
{  
"mappings": {  
"properties": {  
"data": {  
"type": "keyword"  
},  
"hash": {  
"type": "integer"  
},  
"hash\_mapping": {  
"type": "keyword"  
},  
"task\_id": {  
"type": "keyword"  
}  
}  
}  
}  
···

# Insert data

···  
POST /new\_index/\_doc  
{  
"data": "111 123 124 456 789",  
"hash": 12345,  
"hash\_mapping": "Sample mapping value",  
"task\_id": "Task No. 1"  
}  
···

The table structure in Elasticsearch is as above.

Now it is necessary to set up a fuzzy search.

Examples:

1. When searching for data "111 123 124 456 789", it can be found with a matching degree of 100%.
2. When searching for data "111 123 124 456", it can be found with a matching degree of 100%.
3. When searching for data "111 123 124 333 789", it can be found with a matching degree of 80%.
4. When searching for data "222 123 124 333 789", it cannot be found with a matching degree of 60%.

Return the matching data when the matching degree is greater than or equal to 80%.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 6, 2024, 9:44am UTC](https://discuss.elastic.co/t/how-should-i-write-this-statement/371584/2 "2024-12-06T09:44:11Z")

</div>

It sounds like you may want to use [multi field](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html) to map the `data` field as both a `keyword` and as an analysed `text` field using the whitespace tokeniser. The tokenised field would mostly give the result you describe, but the last example would match at 60% as 3 out of 5 terms would match.

---

<div class="post-metadata">

**Author:** ![nIxedoahz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nixedoahz/32/139741_2.png) [@nIxedoahz](https://discuss.elastic.co/u/nIxedoahz)\
**Post date:** [December 6, 2024, 9:49am UTC](https://discuss.elastic.co/t/how-should-i-write-this-statement/371584/3 "2024-12-06T09:49:11Z")

</div>

Since I'm new to Elasticsearch, I'm not quite clear about some concepts yet.

This structure can be modified and the "keyword" type doesn't have to be used. As long as this function can be achieved, it will be okay. Do you know how to do it?  
thanks

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 6, 2024, 9:51am UTC](https://discuss.elastic.co/t/how-should-i-write-this-statement/371584/4 "2024-12-06T09:51:06Z")

</div>

I would recommend mapping the field using multi field so you have both keyword and text mapping. For some types of queries one my work better than the other.

---

<div class="post-metadata">

**Author:** ![nIxedoahz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nixedoahz/32/139741_2.png) [@nIxedoahz](https://discuss.elastic.co/u/nIxedoahz)\
**Post date:** [December 6, 2024, 9:53am UTC](https://discuss.elastic.co/t/how-should-i-write-this-statement/371584/5 "2024-12-06T09:53:48Z")

</div>

Would it be convenient for you to write a sample? Thank you very much.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 6, 2024, 9:56am UTC](https://discuss.elastic.co/t/how-should-i-write-this-statement/371584/6 "2024-12-06T09:56:48Z")

</div>

Did you look at the documentation I linked to? This contains a very similar example.
