facets and doing some filtering based on facets

浪尽此生 提交于 2021-01-29 15:50:16

问题


I have a question about facets and doing some filtering based on facets. i know this is a repeated question but i am unable find the answer.

i would like to know how can i implement the same functionality in elastic search.

lets asume that I have an index about cars and some facets -- eg. model and color.

color

[ ] red (10)

[ ] blue (5)

[ ] green (2)

model

[ ] bmw (4)

[ ] vw (5)

[ ] ford (8)

if I select a model I would like to get only color facets for that model, but I still would like to get facets for all models. eg:

color

[ ] red (2)

[ ] blue (2)

[ ] green (1)

model

[ ] bmw (4)

[x] vw (5)

[ ] ford (8)

I have searched I did not find an example about this use-case. Is this possible and if yes, how do I filter a query to get these results?

Kind regards


回答1:


I'm sure this has been answered multiple here but let's take your concrete example.

Create an index

PUT lalit
{
  "mappings": {
    "properties": {
      "model": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "color": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

Ingest a few docs

POST lalit/_doc
{"color":"red","model":"bmw"}
POST lalit/_doc
{"color":"blue","model":"bmw"}
POST lalit/_doc
{"color":"red","model":"vw"}
POST lalit/_doc
{"color":"green","model":"vw"}
POST lalit/_doc
{"color":"blue","model":"ford"} 

Apply a combination of a terms agg + a filter terms agg

GET lalit/_search
{
  "size": 0,
  "aggs": {
    "all_models": {
      "terms": {
        "field": "model.keyword"
      }
    },
    "all_colors": {
      "terms": {
        "field": "color.keyword"
      }
    },
    "model_filtered_colors": {
      "filter": {
        "term": {
          "model.keyword": "vw"
        }
      },
      "aggs": {
        "actual_aggs": {
          "terms": {
            "field": "color.keyword"
          }
        }
      }
    }
  }
}

Yielding

"aggregations" : {
    "model_filtered_colors" : {
      "doc_count" : 2,
      "actual_aggs" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : "green",
            "doc_count" : 1
          },
          {
            "key" : "red",
            "doc_count" : 1
          }
        ]
      }
    },
    "all_models" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "bmw",
          "doc_count" : 2
        },
        {
          "key" : "vw",
          "doc_count" : 2
        },
        {
          "key" : "ford",
          "doc_count" : 1
        }
      ]
    },
    "all_colors" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "blue",
          "doc_count" : 2
        },
        {
          "key" : "red",
          "doc_count" : 2
        },
        {
          "key" : "green",
          "doc_count" : 1
        }
      ]
    }
  }

model_filtered_colors gives you all vws by color while the other 2 aggregations give you the totals across the board (w/o the vw filter).



来源:https://stackoverflow.com/questions/61014867/facets-and-doing-some-filtering-based-on-facets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!