user3650103
user3650103

Reputation: 23

Elasticsearch filtered query with script for term frequency

I'm using the attachment plugin: https://github.com/elastic/elasticsearch-mapper-attachments

I'm able to find documents with a specific word in 1 or more fields but unable to filter documents with a lower term frequency than searched for.

This works:

POST /crm/employee/_search
{
"query": {"filtered": {
    "query": {"match": {
       "employee.cv.content": "transitie"
    }}, 
    "filter": {
       "bool": {
           "should": [
              {"terms": {
                 "employee.listEmployeeType.id": [
                    2
                 ]
              }}
           ]
       }
   }
}},
"highlight": {"fields": {"employee.cv.content" : {}}}
}

After a long search, I've found the following:

"script": {
          "script": "crm['employee.cv.content'][lookup].tf() > occurrence",
          "params": {
              "lookup": "transitie",
              "occurrence": 1
          }
       },

I'm unable to implement it unfortunately. I hope i've explained the issue good enough for someone to give me a push in the right direction!

Upvotes: 2

Views: 588

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

{
  "query": {
    "filtered": {
      "query": {
        "match": {
          "employee.cv.content": "transitie"
        }
      },
      "filter": {
        "bool": {
          "should": [
            {
              "terms": {
                "employee.listEmployeeType.id": [
                  2
                ]
              }
            }
          ],
          "must": [
            {
              "script": {
                "script": "_index['employee.cv.content'][lookup].tf() > occurrence",
                "params": {
                  "lookup": "transitie",
                  "occurrence": 1
                }
              }
            }
          ]
        }
      }
    }
  },
  "highlight": {
    "fields": {
      "employee.cv.content": {}
    }
  }
}

Upvotes: 1

Related Questions