Constant_score vs. filtered; and a follow up about custom_filters_score


(weitzenfeld) #1

Question 1
Is there a preferred way to write a query that consists solely of filters -
that is, return documents iff they pass a set of filters?

From thishttp://elasticsearch-users.115913.n3.nabble.com/Filters-vs-Queries-td3219558.htmlthread, I've gathered that there are two options:

  • Constant Score:

    { query: {
    constant_score: {
    filter: {...},
    filter: {...},
    filter: {...}
    }
    }}

  • Filtered:

    { query: {
    filtered: {
    filter: {...},
    filter: {...},
    filter: {...}
    }
    }}

Am I right that these will produce identical results? Is there a reason to
prefer one over the other?

Question 2

I'd like to write a query that first uses filters to limit to a subset of
documents (as in question 1), then uses function scoring with filters to
score each of the the documents in the subset.

A year ago, I used custom_filters_score, like so:

{
"query":{
"custom_filters_score":{
"query":{
"filtered":{
"query":{
"match_all":{

              }
           },
           "filter":{
              "and":[ 
  •               <limiting filters>*
                ]
             }
          }
       },
       "filters":[ 
    
  •          <scoring filters>*
       ],
       "params":{
    
       },
       "score_mode":"total"
    }
    
    }
    }

Now, with function score query, I'm thinking:

{
"query":{
"function_score":{
"functions":[
* *
],
"query":{
"filtered":{
"query":{
"match_all":{

              }
           },
           "filter":{
              "and":[
                 * <limiting filters>*
              ]
           }
        }
     },
     "score_mode":"first"
  }

}
}

Is this the best way to achieve what I want?

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/b8c0ac6d-b496-4b6e-b5f5-4b510b4ac634%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(weitzenfeld) #2

I'll answer Question 1 myself:

I read the entire thread I linked to and kimchy says:

"A filtered query with a match_all query is automatically converted
internally to a constant_score one, so guess which one is better? :)"

On Thursday, January 9, 2014 3:04:57 PM UTC-5, weitz...@howaboutwe.com
wrote:

Question 1
Is there a preferred way to write a query that consists solely of filters

  • that is, return documents iff they pass a set of filters?

From thishttp://elasticsearch-users.115913.n3.nabble.com/Filters-vs-Queries-td3219558.htmlthread, I've gathered that there are two options:

  • Constant Score:

    { query: {
    constant_score: {
    filter: {...},
    filter: {...},
    filter: {...}
    }
    }}

  • Filtered:

    { query: {
    filtered: {
    filter: {...},
    filter: {...},
    filter: {...}
    }
    }}

Am I right that these will produce identical results? Is there a reason
to prefer one over the other?

Question 2

I'd like to write a query that first uses filters to limit to a subset of
documents (as in question 1), then uses function scoring with filters to
score each of the the documents in the subset.

A year ago, I used custom_filters_score, like so:

{
"query":{
"custom_filters_score":{
"query":{
"filtered":{
"query":{
"match_all":{

              }
           },
           "filter":{
              "and":[ 
  •               <limiting filters>*
                ]
             }
          }
       },
       "filters":[ 
    
  •          <scoring filters>*
       ],
       "params":{
    
       },
       "score_mode":"total"
    }
    
    }
    }

Now, with function score query, I'm thinking:

{
"query":{
"function_score":{
"functions":[
* *
],
"query":{
"filtered":{
"query":{
"match_all":{

              }
           },
           "filter":{
              "and":[
                 * <limiting filters>*
              ]
           }
        }
     },
     "score_mode":"first"
  }

}
}

Is this the best way to achieve what I want?

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/81e3eb91-b488-4890-9852-4f6b1a9ee966%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #3