Facet query with custom counts in facet navigation

Hi everyone,

I need to use facet navigation for filtering search result. By adding each
term I can build query filter like* (Attribute1=term1 OR Attribute1=term2)
AND (Attribute2=term4)*. So when I have such query, then in facet
navigation I'd like to have all available terms with counts showing
increase with new possible increase in final result set, in short user
would see how many new documents will be added to search result set.

Example:

  • Applied filter (Attribute1=term1 OR Attribute1=term2) AND
    (Attribute2=term4)

Facet navigation

  • Attribute 1
  • term1 (applied) - count here is 0, because I cannot add more documents
    to final result
    • term2 (applied) - count here is 0, because I cannot add more
      documents to final result
    • term3 - count would be the number of matching documents with filter
      Attribute2 = term4 AND Attribute=term3
  • Attribute 2
    • term4 (applied) - count here is 0, because I cannot add more
      documents to final result
    • term5 - count would be the number of matching documents with filter
      (Attribute1=term1 OR Attribute1=term2) AND Attribute2=term5

Can anyone advice how would search request to ES look like?

Thanks, Jaro.

--

Here an example how to replicate it and what I'd like to receive.

Thanks a lot. Jaro

On Sunday, December 2, 2012 12:41:06 AM UTC+2, Jaromír Müller wrote:

Hi everyone,

I need to use facet navigation for filtering search result. By adding each
term I can build query filter like* (Attribute1=term1 OR
Attribute1=term2) AND (Attribute2=term4)*. So when I have such query,
then in facet navigation I'd like to have all available terms with counts
showing increase with new possible increase in final result set, in short
user would see how many new documents will be added to search result set.

Example:

  • Applied filter (Attribute1=term1 OR Attribute1=term2) AND
    (Attribute2=term4)

Facet navigation

  • Attribute 1
  • term1 (applied) - count here is 0, because I cannot add more
    documents to final result
    • term2 (applied) - count here is 0, because I cannot add more
      documents to final result
    • term3 - count would be the number of matching documents with
      filter Attribute2 = term4 AND Attribute=term3
  • Attribute 2
    • term4 (applied) - count here is 0, because I cannot add more
      documents to final result
    • term5 - count would be the number of matching documents with
      filter (Attribute1=term1 OR Attribute1=term2) AND Attribute2=term5

Can anyone advice how would search request to ES look like?

Thanks, Jaro.

--

In the end I found the solution which is based on facet_filter

curl -XGET 'http://localhost:9200/test_index/test_type/_search?pretty=true' -d
'{

"query":{
    "filtered":{
        "query" : {
            "query_string" : {
                "query" : "Lorem"
            }
        },
        "filter": {
            "and":[
                {
                    "or":[{
                        "term":{"size":"L"}
                    }]
                },{
                    "or":[{
                        "term":{"yesno":"yes"}
                    }]
                }
            ]
        }
    }
},
"facets":{
    "_facet_size":{
        "terms":{ 
            "field":"size",
            "order":"reverse_count",
            "all_terms":true             
        },                 
        "facet_filter": {
            "and":[
                {
                    "query" : {
                    "query_string" : {
                        "query" : "Lorem"
                    }
                }
                },
                {
                    "or":[{
                        "term": {"yesno": "yes"}
                    }]
                }
            ]
        },
        "global": true
    },
    "_facet_yesno":{
        "terms":{ 
            "field":"yesno",
            "order":"reverse_count",
            "all_terms":true             
        },                 
        "facet_filter": {
            "and":[
                {
                    "query" : {
                    "query_string" : {
                        "query" : "Lorem"
                    }
                }
                },
                {
                    "or":[{
                        "term": {"size": "L"}
                    }]
                }
            ]
        },
        "global": true
    }
}

}'

From the documentation page
(http://www.elasticsearch.org/guide/reference/api/search/facets/index.html) wasnt
quite obvious what query I need to sent to ES.

Could anyone advice if my solution is correct or is there any possible
improvement?

Thanks, Jaro.

On Sunday, December 2, 2012 12:41:06 AM UTC+2, Jaromír Müller wrote:

Hi everyone,

I need to use facet navigation for filtering search result. By adding each
term I can build query filter like* (Attribute1=term1 OR
Attribute1=term2) AND (Attribute2=term4)*. So when I have such query,
then in facet navigation I'd like to have all available terms with counts
showing increase with new possible increase in final result set, in short
user would see how many new documents will be added to search result set.

Example:

  • Applied filter (Attribute1=term1 OR Attribute1=term2) AND
    (Attribute2=term4)

Facet navigation

  • Attribute 1
  • term1 (applied) - count here is 0, because I cannot add more
    documents to final result
    • term2 (applied) - count here is 0, because I cannot add more
      documents to final result
    • term3 - count would be the number of matching documents with
      filter Attribute2 = term4 AND Attribute=term3
  • Attribute 2
    • term4 (applied) - count here is 0, because I cannot add more
      documents to final result
    • term5 - count would be the number of matching documents with
      filter (Attribute1=term1 OR Attribute1=term2) AND Attribute2=term5

Can anyone advice how would search request to ES look like?

Thanks, Jaro.

--

I cannot think of any other approach besides using filter facets. Facets
are typically used to narrow searches instead of expanding them. In your
case you are trying to expand your search and that means you have to run
all these extra searches for each "facet". You could also use multisearch
to do the same thing, but your solution will probably use caches better.

On Sunday, December 2, 2012 2:53:06 PM UTC-5, Jaromír Müller wrote:

In the end I found the solution which is based on facet_filter

curl -XGET '
http://localhost:9200/test_index/test_type/_search?pretty=true' -d '{

"query":{
    "filtered":{
        "query" : {
            "query_string" : {
                "query" : "Lorem"
            }
        },
        "filter": {
            "and":[
                {
                    "or":[{
                        "term":{"size":"L"}
                    }]
                },{
                    "or":[{
                        "term":{"yesno":"yes"}
                    }]
                }
            ]
        }
    }
},
"facets":{
    "_facet_size":{
        "terms":{ 
            "field":"size",
            "order":"reverse_count",
            "all_terms":true             
        },                 
        "facet_filter": {
            "and":[
                {
                    "query" : {
                    "query_string" : {
                        "query" : "Lorem"
                    }
                }
                },
                {
                    "or":[{
                        "term": {"yesno": "yes"}
                    }]
                }
            ]
        },
        "global": true
    },
    "_facet_yesno":{
        "terms":{ 
            "field":"yesno",
            "order":"reverse_count",
            "all_terms":true             
        },                 
        "facet_filter": {
            "and":[
                {
                    "query" : {
                    "query_string" : {
                        "query" : "Lorem"
                    }
                }
                },
                {
                    "or":[{
                        "term": {"size": "L"}
                    }]
                }
            ]
        },
        "global": true
    }
}

}'

From the documentation page (
http://www.elasticsearch.org/guide/reference/api/search/facets/index.html) wasnt
quite obvious what query I need to sent to ES.

Could anyone advice if my solution is correct or is there any possible
improvement?

Thanks, Jaro.

On Sunday, December 2, 2012 12:41:06 AM UTC+2, Jaromír Müller wrote:

Hi everyone,

I need to use facet navigation for filtering search result. By adding
each term I can build query filter like* (Attribute1=term1 OR
Attribute1=term2) AND (Attribute2=term4)*. So when I have such query,
then in facet navigation I'd like to have all available terms with counts
showing increase with new possible increase in final result set, in short
user would see how many new documents will be added to search result set.

Example:

  • Applied filter (Attribute1=term1 OR Attribute1=term2) AND
    (Attribute2=term4)

Facet navigation

  • Attribute 1
  • term1 (applied) - count here is 0, because I cannot add more
    documents to final result
    • term2 (applied) - count here is 0, because I cannot add more
      documents to final result
    • term3 - count would be the number of matching documents with
      filter Attribute2 = term4 AND Attribute=term3
  • Attribute 2
    • term4 (applied) - count here is 0, because I cannot add more
      documents to final result
    • term5 - count would be the number of matching documents with
      filter (*Attribute1=term1 OR Attribute1=term2) AND Attribute2=term5

Can anyone advice how would search request to ES look like?

Thanks, Jaro.

--