Use match keyword with sort

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,

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.

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.

What about this:

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:

{
  "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"
        }
      }
    ]
  }
}

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.

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. 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.

1 Like

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