0

I'm working on Elastic Search and facing an issue regarding Array field. I've index named test-index with following mapping.

{
  "test-index": {
    "mappings": {
      "properties": {
        "courses": {
          "type": "keyword"
        }
      }
    }
  }
}

My elastic search documents looks like this.

"hits": [
      {
        "_index": "test-index",
        "_id": "1ac:0000000000_1",
        "_score": 1,
        "_source": {
          "courses": [
            "Course-1A",
            "Course-1B",
            "Course-1C",
            "Course-1D",
            "Course-1E",
            "Course-1F"
          ]
        }
      },
      {
        "_index": "test-index",
        "_id": "1ac:0000000000_2",
        "_score": 1,
        "_source": {
          "courses": [
            "Course-2A",
            "Course-2B",
            "Course-2C",
            "Course-1A"
          ]
        }
      }
    ]

The document _id is my student ID. I want to get results with the maximum/highest relevance at the top and lowest on the bottom.

e.g

If I'm searching for courses ["Course-2A","Course-2B","Course-1C"] then user 1ac:0000000000_2 should appear at the top and user 1ac:0000000000_1 at the bottom.

I've tried following queries.

GET test-index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "courses": [
              "Course-1A",
              "Course-2A",
              "Course-2B"
            ]
          }
        }
      ]
    }
  }
}

User 1ac:0000000000_1 at the top and other at the bottom.

GET test-index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "courses": "Course-1A",
          }
        },
        {
          "term": {
            "courses": "Course-2A",
          }
        },
        {
          "term": {
            "courses": "Course-2B",
          }
        }
      ],
      "minimum_should_match": "70%"
    }
  }
}

This gives me some desired results but not sure for larger dataset.

M Usman Wahab
  • 53
  • 1
  • 10

0 Answers0