문제

I have a need to have a Predefined Range filter facet. The Range filter will be for a school's grade range. I want K-2, 3-5, 6-8, 9-12, Everyone. My records have MinGrade and MaxGrade fields. They can have values K, 1, 2... ,12 or Everyone.I figured I might use a range filter. But I could not figure out how to use 2 different fields combined, to fall into the predefined ranges above. Any suggestions? I hope this makes sense.

I want the facet output to look like this....

  • K-2 (xxx)
  • 3-5 (xxx)
  • 6-8 (xxx)
  • 9-12 (xxx)
  • Everyone else (xxx)

Since I changed to 1 DB column, I have the range query working, Also curious if it is possible to add a custom Label like below "facets": { "Grade": { "range": { "field": "Grade", "ranges": [ {"label" : "K-2", "gte": 0, "lte": 2}, {"label" : "3-5", "gte": 3, "lte": 5}, {"label" : "6-8", "gte": 6, "lte": 8}, {"label" : "9-12", "gte": 9, "lte": 12}, {"label" : "Above 12", "gte": 13} ] } } }

도움이 되었습니까?

해결책

From the comment back-and-forth I believe I understand what you want.

I.e.: a record A may be shown for every grade between minGrade and maxGrade.

For example: a record 123 may be shown for every grade between minGrade = 2 and maxGrade = 6.

Queries:

  1. for a given grade x, return all records for which minGrade <= x <= maxGrade
  2. asked here I believe: for a given range on grade, say [x1. x2], determine all records that have a non-empty overlap given their range [minGrade, maxGrade]

as follows:

  1. Given grade x (in pseudo code): minGrade <= x AND x <= maxGrade
  2. You need to construct separate filter-facets for each bucket. e.g.: for 6-8 you could do either 1 of the following:

    1. (minGrade <= 6 AND 6 <= maxGrade) OR (minGrade <= 8 AND 8 <= maxGrade)
    2. (6 <= minGrade AND minGrade <= 8) OR (6 <= maxGrade AND maxGrade <= 8)

2.2. could be implemented using 2 rangeQueries: 1 on mingrade and 1 on maxgrade combined with an OR

So an example 1 of filter facet could be (untested):

    {
        "facets": {
            "grade_6-8": {
                "filter": {
                    "or": [{
                        "range": {
                            "minGrade": {
                                "gte": 6,
                                "lte": 8
                            }
                        }
                    },
                    {
                        "range": {
                            "maxGrade": {
                                "gte": 6,
                                "lte": 8
                            }
                        }
                    }]
                }
            }
        }
    }

EDIT: the above is a:

  • facet called grade_6-8
  • of type "filter facet"
  • that contains a or- filter
  • which in turn contains 2 range filters . 1 for minGrade and 1 for maxGrade.
  • NOTE You should repeat this for all the ranges you need separately

EDIT 2 when defining only 1 Grade per document instead of minGrade and maxGrade the solution changes. It becomes possible to do this with a simple rangequery. Something like:

    {
        "query": {
            "match_all": {}
        },
        "facets": {
            "grade_ranges": {
                "range": {
                    "field": "grade",
                    "ranges": [{
                        "to": 2
                    }, {
                        "from": 3,
                        "to": 5
                    }, {
                        "from": 6,
                        "to": 8
                    }, {
                        "from": 9,
                        "to": 12
                    }, {
                        "from": 13
                    }]
                }
            }
        }
    }

If this isn't what you're after please be more specific what you want.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top