I want to get the total number of keyword hits for each document in the query results

My field mapping settings are as follows

"functionPoint": {
  "type": "text",
  "index": true,
  "analyzer": "ik_smart",
  "search_analyzer": "ik_smart",
  "fielddata": true,
  "fielddata_frequency_filter": {
    "min": 0.01,
    "max": 0.5,
    "min_segment_size": 500
  },
  "index_options": "offsets",
  "boost": 5
}

My elasticsearch query DSL is as follows

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "success",
            "fields": ["transName", "testChkKey", "testSteps", "functionPoint", "previewCondi"],
            "type": "phrase"
          }
        },
        {
          "multi_match": {
            "query": "error",
            "fields": ["transName", "testChkKey", "testSteps", "functionPoint", "previewCondi"],
            "type": "phrase"
          }
        },
        {
          "multi_match": {
            "query": "bug",
            "fields": ["transName", "testChkKey", "testSteps", "functionPoint", "previewCondi"],
            "type": "phrase"
          }
        }
      ],
      "minimum_should_match": 2
    }
  },
  "sort": [
    {
      "_script": {
        "script": {
          "source": """
            def keywords = ['success', 'error', 'bug'];
            int hitCount = 0;
            def fields = ['transName', 'testChkKey', 'testSteps', 'functionPoint', 'previewCondi'];
            for (def field : fields) {
              if (doc.containsKey(field) && doc[field].size() > 0) {
                def currentValue = doc[field].value;
                for (def keyword : keywords) {
                  hitCount += currentValue.contains(keyword) ? 1 : 0;
                }
              }
            }
            return hitCount;
          """,
          "lang": "painless"
        },
        "type": "number",
        "order": "desc"
      }
    },
    {
      "fitversion": {
        "order": "desc",
        "unmapped_type": "keyword"
      }
    },
    { "_score": { "order": "desc" } }
  ],
  "highlight": {
    "pre_tags": [ "<em>" ],
    "post_tags": [ "</em>" ],
    "fields": {
      "transName": {},
      "testChkKey": {},
      "testSteps": {},
      "functionPoint": {},
      "previewCondi": {}
    }
  }
}

My two documents, document A source document content is as follows

{"transName": "Transaction Name 1",
"testChkKey": "Trade keyword 1error financial management test",
"testSteps": "Test step 1 success test c",
"functionPoint": "Test Key Point",
"previewCondi": "Test precondition 1bug"} ,

The content of the document B source document is as follows

{"transName": "Transaction name 2 success",
"testChkKey": "Trade key 1 error bug test",
"testSteps": "Test step 1 success test c",
"functionPoint": "Test key point 2 bug",
"previewCondi": "Test precondition 2 bug"}

In document A, testChkKey contains the keyword error testSteps contains the keyword success previewCondi contains the keyword bug and the keyword appears 3 times in total
The transName in document B contains the keyword success testChkKey contains the keyword error bug testSteps contains the keyword success functionPoint contains the keyword bug previewCondi contains the keyword bug The keyword appears 6 times in total. Why do I customize the sort after using the query DSL to hit these two documents? The calculated value of the script is 1, please give why the calculation is wrong, give possible reasons and how to fix it

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