0

Lets say I have index with two text fields:

"properties" : {
   "name" : {
      "type" : "text"
    },
    "description" : {
      "type" : "text"
    }
}

I need to implement full text search by these two fields with priority. First by name, and after that by description. The point is that I don't need to search in description, if I have already found something by name. I can implement two queries, and check if search by name returns 0 result, than search by description. But is there way to implement it with single query?

UPD. My query is pretty simple:

"query": {
  "bool": {
    "should": [
      {
        "match": {
          "name": {
            "query": "some user input here",
            "operator": "and"
          }
        }
      },

      {
        "match": {
          "description": {
            "query": "some user input here",
            "operator": "and"
          }
        }
      }
    ]
  }
}

The refined question is, how to group result of single query in two groups: those who match by name, and those who match by description? First group should be scored higher than second. I need to know total count for both groups to implement paging behavior in my application. So when first group is empty, I will show the second, but not only then. Otherwise I will hide the second group from user. Also there should be relevance(by provided analyzer) scoring within a group in order to provide reasonable results of search to users.

KozhevnikovDmitry
  • 1,660
  • 12
  • 27
  • One approach would be to query both fields with a `bool` query, and apply a large boost to a match on `name` to score matches much higher. Can you add some detail about the type of query that you're running? – Russ Cam Sep 15 '18 at 01:54
  • Please take a look on updated question. – KozhevnikovDmitry Sep 17 '18 at 05:48

0 Answers0