Skip to content

Instantly share code, notes, and snippets.

@bearrundr
Forked from mattweber/README.txt
Created February 13, 2022 00:39
Show Gist options
  • Select an option

  • Save bearrundr/8c76d5827aa79d46dd497d49d7bc6f13 to your computer and use it in GitHub Desktop.

Select an option

Save bearrundr/8c76d5827aa79d46dd497d49d7bc6f13 to your computer and use it in GitHub Desktop.

Revisions

  1. @mattweber mattweber created this gist Mar 1, 2012.
    19 changes: 19 additions & 0 deletions README.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    This is an example how to perform multi-select faceting in ElasticSearch.

    Selecting multiple values from the same facet will result in an OR filter between each of the values:
    (facet1.value1 OR facet1.value2)

    Faceting on more than one facet will result in an AND filter between each facet:
    (facet1.value1 OR facet1.value2) AND (facet2.value1)

    I have chosen to update the counts for each facet the selected value DOES NOT belong to since we are performing an AND between each facet. I have included an example that shows how to keep the counts if you don't want to do this (filter0.sh).


    docs.sh - the example documents
    query.sh - the initial query
    filter0.sh - filter on tag "tag2", keeping counts
    filter1.sh - filter on tag "tag2", update counts
    filter2.sh - filter on tags "tag2" and "tag4", update counts
    filter3.sh - filter on tags "tag2" and "tag4", keyword "keyword0", update counts
    filter4.sh - filter on tags "tag2" and "tag4", keywords "keyword0" and "keyword2", update counts

    23 changes: 23 additions & 0 deletions docs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    curl -XPUT 'http://localhost:9200/multiselect/demo/1' -d '{
    "title": "One",
    "tags": ["tag1"],
    "keywords": ["keyword1","keyword2","keyword3"]
    }'

    curl -XPUT 'http://localhost:9200/multiselect/demo/2' -d '{
    "title": "Two",
    "tags": ["tag1","tag2"],
    "keywords": ["keyword1","keyword2"]
    }'

    curl -XPUT 'http://localhost:9200/multiselect/demo/3' -d '{
    "title": "Three",
    "tags": ["tag1","tag2","tag3"],
    "keywords": ["keyword1"]
    }'

    curl -XPUT 'http://localhost:9200/multiselect/demo/4' -d '{
    "title": "Four",
    "tags": ["tag4"],
    "keywords": ["keyword0"]
    }'
    24 changes: 24 additions & 0 deletions filter0.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    curl -XGET 'http://localhost:9200/multiselect/demo/_search?pretty=true' -d '{
    "query": {
    "match_all": {}
    },
    "filter": {
    "terms": {
    "tags": ["tag2"]
    }
    },
    "facets": {
    "tagFacet": {
    "terms": {
    "field": "tags",
    "all_terms": true
    }
    },
    "keywordFacet": {
    "terms": {
    "field": "keywords",
    "all_terms": true
    }
    }
    }
    }'
    29 changes: 29 additions & 0 deletions filter1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    curl -XGET 'http://localhost:9200/multiselect/demo/_search?pretty=true' -d '{
    "query": {
    "match_all": {}
    },
    "filter": {
    "terms": {
    "tags": ["tag2"]
    }
    },
    "facets": {
    "tagFacet": {
    "terms": {
    "field": "tags",
    "all_terms": true
    }
    },
    "keywordFacet": {
    "terms": {
    "field": "keywords",
    "all_terms": true
    },
    "facet_filter":{
    "terms": {
    "tags": ["tag2"]
    }
    }
    }
    }
    }'
    29 changes: 29 additions & 0 deletions filter2.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    curl -XGET 'http://localhost:9200/multiselect/demo/_search?pretty=true' -d '{
    "query": {
    "match_all": {}
    },
    "filter": {
    "terms": {
    "tags": ["tag2","tag4"]
    }
    },
    "facets": {
    "tagFacet": {
    "terms": {
    "field": "tags",
    "all_terms": true
    }
    },
    "keywordFacet": {
    "terms": {
    "field": "keywords",
    "all_terms": true
    },
    "facet_filter":{
    "terms": {
    "tags": ["tag2","tag4"]
    }
    }
    }
    }
    }'
    43 changes: 43 additions & 0 deletions filter3.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    curl -XGET 'http://localhost:9200/multiselect/demo/_search?pretty=true' -d '{
    "query": {
    "match_all": {}
    },
    "filter": {
    "and": [
    {
    "terms": {
    "tags": ["tag2","tag4"]
    }
    },
    {
    "terms": {
    "keywords": ["keyword0"]
    }
    }
    ]
    },
    "facets": {
    "tagFacet": {
    "terms": {
    "field": "tags",
    "all_terms": true
    },
    "facet_filter": {
    "terms": {
    "keywords": ["keyword0"]
    }
    }
    },
    "keywordFacet": {
    "terms": {
    "field": "keywords",
    "all_terms": true
    },
    "facet_filter":{
    "terms": {
    "tags": ["tag2","tag4"]
    }
    }
    }
    }
    }'
    43 changes: 43 additions & 0 deletions filter4.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    curl -XGET 'http://localhost:9200/multiselect/demo/_search?pretty=true' -d '{
    "query": {
    "match_all": {}
    },
    "filter": {
    "and": [
    {
    "terms": {
    "tags": ["tag2","tag4"]
    }
    },
    {
    "terms": {
    "keywords": ["keyword0","keyword2"]
    }
    }
    ]
    },
    "facets": {
    "tagFacet": {
    "terms": {
    "field": "tags",
    "all_terms": true
    },
    "facet_filter": {
    "terms": {
    "keywords": ["keyword0","keyword2"]
    }
    }
    },
    "keywordFacet": {
    "terms": {
    "field": "keywords",
    "all_terms": true
    },
    "facet_filter":{
    "terms": {
    "tags": ["tag2","tag4"]
    }
    }
    }
    }
    }'
    19 changes: 19 additions & 0 deletions query.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    curl -XGET 'http://localhost:9200/multiselect/demo/_search?pretty=true' -d '{
    "query": {
    "match_all": {}
    },
    "facets": {
    "tagFacet": {
    "terms": {
    "field": "tags",
    "all_terms": true
    }
    },
    "keywordFacet": {
    "terms": {
    "field": "keywords",
    "all_terms": true
    }
    }
    }
    }'