Add support for post filter in Search
Search requests can now accept a post filter applied after all hits were
returned from the database. For example:
    esquery.Search().
        Query(...).
        Aggs(...).
        PostFilter(esquery.Range(field).Gt(0)).
        Run(...)
			
			
This commit is contained in:
		
							parent
							
								
									7fa767fc28
								
							
						
					
					
						commit
						d7b8bc87a2
					
				
							
								
								
									
										22
									
								
								search.go
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								search.go
									
									
									
									
									
								
							@ -15,12 +15,13 @@ import (
 | 
				
			|||||||
// Not all features of the search API are currently supported, but a request can
 | 
					// Not all features of the search API are currently supported, but a request can
 | 
				
			||||||
// currently include a query, aggregations, and more.
 | 
					// currently include a query, aggregations, and more.
 | 
				
			||||||
type SearchRequest struct {
 | 
					type SearchRequest struct {
 | 
				
			||||||
	query   Mappable
 | 
						query      Mappable
 | 
				
			||||||
	aggs    []Aggregation
 | 
						aggs       []Aggregation
 | 
				
			||||||
	from    *uint64
 | 
						postFilter Mappable
 | 
				
			||||||
	size    *uint64
 | 
						from       *uint64
 | 
				
			||||||
	explain *bool
 | 
						size       *uint64
 | 
				
			||||||
	timeout *time.Duration
 | 
						explain    *bool
 | 
				
			||||||
 | 
						timeout    *time.Duration
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Search creates a new SearchRequest object, to be filled via method chaining.
 | 
					// Search creates a new SearchRequest object, to be filled via method chaining.
 | 
				
			||||||
@ -40,6 +41,12 @@ func (req *SearchRequest) Aggs(aggs ...Aggregation) *SearchRequest {
 | 
				
			|||||||
	return req
 | 
						return req
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// PostFilter sets a post_filter for the request.
 | 
				
			||||||
 | 
					func (req *SearchRequest) PostFilter(filter Mappable) *SearchRequest {
 | 
				
			||||||
 | 
						req.postFilter = filter
 | 
				
			||||||
 | 
						return req
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// From sets a document offset to start from.
 | 
					// From sets a document offset to start from.
 | 
				
			||||||
func (req *SearchRequest) From(offset uint64) *SearchRequest {
 | 
					func (req *SearchRequest) From(offset uint64) *SearchRequest {
 | 
				
			||||||
	req.from = &offset
 | 
						req.from = &offset
 | 
				
			||||||
@ -81,6 +88,9 @@ func (req *SearchRequest) Map() map[string]interface{} {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		m["aggs"] = aggs
 | 
							m["aggs"] = aggs
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if req.postFilter != nil {
 | 
				
			||||||
 | 
							m["post_filter"] = req.postFilter.Map()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if req.size != nil {
 | 
						if req.size != nil {
 | 
				
			||||||
		m["size"] = *req.size
 | 
							m["size"] = *req.size
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -42,6 +42,7 @@ func TestSearchMaps(t *testing.T) {
 | 
				
			|||||||
					StringStats("tag_stats", "tags").
 | 
										StringStats("tag_stats", "tags").
 | 
				
			||||||
						ShowDistribution(true),
 | 
											ShowDistribution(true),
 | 
				
			||||||
				).
 | 
									).
 | 
				
			||||||
 | 
									PostFilter(Range("score").Gt(0)).
 | 
				
			||||||
				Size(30).
 | 
									Size(30).
 | 
				
			||||||
				From(5).
 | 
									From(5).
 | 
				
			||||||
				Explain(true).
 | 
									Explain(true).
 | 
				
			||||||
@ -87,6 +88,13 @@ func TestSearchMaps(t *testing.T) {
 | 
				
			|||||||
						},
 | 
											},
 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
 | 
									"post_filter": map[string]interface{}{
 | 
				
			||||||
 | 
										"range": map[string]interface{}{
 | 
				
			||||||
 | 
											"score": map[string]interface{}{
 | 
				
			||||||
 | 
												"gt": 0,
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
				"size":    30,
 | 
									"size":    30,
 | 
				
			||||||
				"from":    5,
 | 
									"from":    5,
 | 
				
			||||||
				"explain": true,
 | 
									"explain": true,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user