admin管理员组

文章数量:1302557

Let's say I have a query that looks something like this:

"query": {
   "bool": {
      "must": [
          {
             "bool": {
                "_name": "fooMatches",
                "minimum_should_match":1,
                "should": [
                    {
                       "terms": {
                          "foo":["a"]
                       }
                    },
                    {
                       "terms": {
                          "foo":["b"]
                       }
                    },
                    {
                       "terms": {
                          "foo":["c"]
                       }
                    },
                    {
                       "terms": {
                          "foo":["d"]
                       }
                    }
                ]
             }
          },
          // multiple other clauses
      ]  
   }
}

It would give results maybe like this:

"hits": [
    {
       "_score": 5,
       ...
       "matched_queries": {
          "fooMatches": 3
       }
    }
]

Indicating that it matched 3 of the terms in foo, but it matched some other clauses to give us an eventual score of 5.

The results of the query are sorted by _score by default, but I would like to sort them by fooMatches under matched_queries. Is that possible? What would be the correct sort to add?

Something like:

"sort": [
   { "fooMatches._score":"desc" },
   "_score"
]

And several variants I've tried on that don't work. So how would you do that?

Edit: I would like to be able to sort ties by the rest of the query, so I know I could just put everything except this bit in a filter, which is okay, but not perfect.

Let's say I have a query that looks something like this:

"query": {
   "bool": {
      "must": [
          {
             "bool": {
                "_name": "fooMatches",
                "minimum_should_match":1,
                "should": [
                    {
                       "terms": {
                          "foo":["a"]
                       }
                    },
                    {
                       "terms": {
                          "foo":["b"]
                       }
                    },
                    {
                       "terms": {
                          "foo":["c"]
                       }
                    },
                    {
                       "terms": {
                          "foo":["d"]
                       }
                    }
                ]
             }
          },
          // multiple other clauses
      ]  
   }
}

It would give results maybe like this:

"hits": [
    {
       "_score": 5,
       ...
       "matched_queries": {
          "fooMatches": 3
       }
    }
]

Indicating that it matched 3 of the terms in foo, but it matched some other clauses to give us an eventual score of 5.

The results of the query are sorted by _score by default, but I would like to sort them by fooMatches under matched_queries. Is that possible? What would be the correct sort to add?

Something like:

"sort": [
   { "fooMatches._score":"desc" },
   "_score"
]

And several variants I've tried on that don't work. So how would you do that?

Edit: I would like to be able to sort ties by the rest of the query, so I know I could just put everything except this bit in a filter, which is okay, but not perfect.

Share edited Feb 10 at 20:42 Matt Burland asked Feb 10 at 20:24 Matt BurlandMatt Burland 45.2k18 gold badges107 silver badges179 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Tldr;

I don't think you can access those values. But maybe you can work around this limitation via boosting ?

Solution

In order to first sort by Foo, then by Bar. I would boost Foo by an order or magnitude (10) compared to Bar.

This way, you would be:

  • sorting on _score
  • solving ties
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "_name": "foo",
            "boost": 10,
            "should": [
              {
                "terms": {
                  "tags.keyword": [
                    "a"
                  ]
                }
              },
              {
                "terms": {
                  "tags.keyword": [
                    "b"
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "_name": "bar",
            "boost": 1,
            "should": [
              {
                "terms": {
                  "tags.keyword": [
                    "c"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}
  • a, b, c would give 21
  • b, c => 11
  • b, d => 10
  • c, d => 1

本文标签: elasticsearchSort by matchedqueries scoreStack Overflow