# Use match keyword with sort

**URL:** <https://discuss.elastic.co/t/use-match-keyword-with-sort/221388>\
**Category:** Elasticsearch\
**Created:** [February 28, 2020, 8:32am UTC](https://discuss.elastic.co/t/use-match-keyword-with-sort/221388 "2020-02-28T08:32:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhattaihuynh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nhattaihuynh/32/63503_2.png) [@nhattaihuynh](https://discuss.elastic.co/u/nhattaihuynh)\
**Post date:** [February 28, 2020, 8:32am UTC](https://discuss.elastic.co/t/use-match-keyword-with-sort/221388/1 "2020-02-28T08:32:18Z")

</div>

Hi everyone,  
Right now I'm stuck with keeping old logic sort from MySQL to Elastic.  
In MySQL, I have a project table with field 'projectName'. When user search project using keyword, I use Match function with Boolean Mode in MySQL to sort result.  
When move it to Elastic, I want to keep that logic with code as below:  
"\_script": {

```
                "script": {

                    "inline": "if(doc['projectName'].value.toString().includes(params.factor)) return 1; else return 0;",

                    "params": {

                        "factor": keyword

                    }

                },

                "type": "number",

                "order": "desc"

            }

```

But it not work at all because "Set fielddata=true on [projectName] in order to load fielddata in memory by uninverting the inverted index. "

Any suggestions or other way to resolve it?  
Thank u,

---

<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:** [February 28, 2020, 2:53pm UTC](https://discuss.elastic.co/t/use-match-keyword-with-sort/221388/2 "2020-02-28T14:53:42Z")

</div>

Welcome!

You need to change the mapping and reindex your data and change the field `projectName` to `keyword` or add a subfield in the mapping with the `keyword` datatype.

But ideally, if possible, you should compute this at index time instead of running the script at search time.  
I'm not sure I understand the use case so sharing some practical example would be helpful to give you may be a better answer.

---

<div class="post-metadata">

**Author:** ![nhattaihuynh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nhattaihuynh/32/63503_2.png) [@nhattaihuynh](https://discuss.elastic.co/u/nhattaihuynh)\
**Post date:** [February 28, 2020, 3:12pm UTC](https://discuss.elastic.co/t/use-match-keyword-with-sort/221388/3 "2020-02-28T15:12:35Z")

</div>

Thanks for response. My English it limit so you may can not understand my case.  
Can not re-index because 'keyword' is what user input and it change every time user search for bunch of projects. It must be search time because it dynamic. I just want the projects have projectName MATCH with keyword have high position in search result.

---

<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:** [February 28, 2020, 3:36pm UTC](https://discuss.elastic.co/t/use-match-keyword-with-sort/221388/4 "2020-02-28T15:36:10Z")

</div>

What about this:

```auto
DELETE index
PUT index/_doc/1
{
  "projectName": "Foo",
  "description": "some text"
}
PUT index/_doc/2
{
  "projectName": "Bar",
  "description": "some text"
}
GET index/_search 
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "description": "text"
          }
        },
        {
          "term": {
            "projectName.keyword": {
              "value": "Foo",
              "boost": 10.0
            }
          }
        }
      ]
    }
  }
}

```

This gives:

```auto
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 7.1137934,
    "hits" : [
      {
        "_index" : "index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 7.1137934,
        "_source" : {
          "projectName" : "Foo",
          "description" : "some text"
        }
      },
      {
        "_index" : "index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_source" : {
          "projectName" : "Bar",
          "description" : "some text"
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![nhattaihuynh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nhattaihuynh/32/63503_2.png) [@nhattaihuynh](https://discuss.elastic.co/u/nhattaihuynh)\
**Post date:** [February 28, 2020, 4:00pm UTC](https://discuss.elastic.co/t/use-match-keyword-with-sort/221388/5 "2020-02-28T16:00:31Z")

</div>

I already think about your solution, but I need all project not match keyword to.  
MATCH just use for sort order, not just match keyword will return. Project have name not match with 'keyword' have lower position in result. Sorry, the logic here have a little bit weird.

---

<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:** [February 28, 2020, 8:56pm UTC](https://discuss.elastic.co/t/use-match-keyword-with-sort/221388/6 "2020-02-28T20:56:11Z")

</div>

That's what my example does I think.

If I'm misunderstanding, please 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:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 27, 2020, 8:56pm UTC](https://discuss.elastic.co/t/use-match-keyword-with-sort/221388/7 "2020-03-27T20:56:19Z")

</div>

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