npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

elasticsearch-filter-append

v2.0.0

Published

append additional filters to filters, queries, and filtered queries

Downloads

9

Readme

Elasticsearch Filter Append

Append additional filters to filters, queries, and filtered queries.

Why?

This library exists primarily for security reasons. Imagine you're accepting a user generated filter but would like to tack on security trimming to input. A user could submit an arbitrary filter that opens up the flood gates and allows them to see EVERYTHING. You could ensure the user only views their information by appending an extra security filter to their input; trimming their results on the fly.

methods

constructor(queryClause)

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  filter: {
    term: {
      name: 'Atlanta, GA'
    }
  }
}

var builder = new FilterBuilder(queryClause)

append(filter1, filter2, filter3....filterN|Array)

append supports variable arguments or an Array as input:

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  filter: {
    term: {
      name: 'Atlanta, GA'
    }
  }
}

var filter1 = {
  term: {
    name: 'Wimberly'
  }
}

var filter2 = {
  term: {
    name: 'Antwan'
  }
}

var builder = new FilterBuilder(queryClause)

builder.append(filter1, filter2)

OR

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  filter: {
    term: {
      name: 'Atlanta, GA'
    }
  }
}

var filter1 = {
  term: {
    name: 'Wimberly'
  }
}

var filter2 = {
  term: {
    name: 'Antwan'
  }
}

var filters = [filter1, filter2]

var builder = new FilterBuilder(queryClause)

builder.append(filters)

The set of filters provided will be converted to an instance of a should filter. That means at least one of the condtitions provided must evaluate to true. You can apply as many groups of conditions as you like. For example, to represent

(cond1 OR cond2) AND (cond3)

You could call append as follows:

var secureQueryClause = builder.append(filter1, filter2).append(filter3).toQueryClause()

Yes, did I mention that append also supports chaining? :wink:

toQueryClause()

toQueryClause is very straightforward. It unwraps the accumulated filter. It should be called subsequent to calls to append. append will take the latest state of the query clause into account while chaining. For example, you could go from a query, to a filtered query with a should filter, to a filtered query with a must filter. Once you have a query clause, you can forward the final payload off to elasticsearch:

var elasticsearch = require('elasticsearch')
var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  filter: {
    term: {
      name: 'Atlanta, GA'
    }
  }
}

var filter = {
  term: {
    name: 'Wimberly'
  }
}

var builder = new FilterBuilder(queryClause).toQueryClause()

var secureQueryClause = builder.append(filter)

elasticsearch.search({
  body: secureQueryClause,
  type: 'my type',
  index: 'my ixndex'
})

bool must filter - array

sample

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  filter: {
    bool: {
      must: [
        {
          bool: {
            should: [
              {
                term: {
                  name: 'Professor Pythonesque'
                }
              },
              {
                term: {
                  name: 'Abraham Lost In San Francisco'
                }
              }
            ]
          }
        }
      ]
    }
  }
}

var filter = {
  term: {
    name: 'Antwan Atkins'
  }
}

var builder = new FilterBuilder(queryClause)

builder.append(filter)

Δδ

builder.toQueryClause()

{
  filter: {
    bool: {
      must: [
        {
          bool: {
            should: [
              {
                term: {
                  name: 'Professor Pythonesque'
                }
              },
              {
                term: {
                  name: 'Abraham Lost In San Francisco'
                }
              }
            ]
          }
        },
        {
          bool: {
            should: [
              {
                term: {
                  name: 'Antwan Atkins'
                }
              }
            ]
          }
        }
      ]
    }
  }
}

bool must filter - object

sample

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  filter: {
    bool: {
      must: {
        term: {
          name: 'Antwan Atkins'
        }
      }
    }
  }
}

var filter = {
  term: {
    name: 'Antwan Atkins'
  }
}

var builder = new FilterBuilder(queryClause)

builder.append(filter)

Δδ

builder.toQueryClause()

{
  filter: {
    bool: {
      must: [
        {
          term: {
            name: 'Antwan Atkins'
          }
        },
        {
          bool: {
            should: [
              {
                term: {
                  name: 'Antwan Atkins'
                }
              }
            ]
          }
        }
      ]
    }
  }
}

bool should filter

sample

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  filter: {
    bool: {
      should: [
        {
          bool: {
            must: {
              term: {
                name: 'Antwan Atkins'
              }
            }
          }
        },
        {
          bool: {
            must: {
              term: {
                name: 'J.Cole World'
              }
            }
          }
        }
      ]
    }
  }
}

var filter = {
  term: {
    name: 'Antwan Atkins'
  }
}

var builder = new FilterBuilder(queryClause)

builder.append(filter)

Δδ

builder.toQueryClause()

{
  filter: {
    bool: {
      must: [
        {
          bool: {
            should: [
              {
                bool: {
                  must: {
                    term: {
                      name: 'Antwan Atkins'
                    }
                  }
                }
              },
              {
                bool: {
                  must: {
                    term: {
                      name: 'J.Cole World'
                    }
                  }
                }
              }
            ]
          }
        },
        {
          bool: {
            should: [
              {
                term: {
                  name: 'Antwan Atkins'
                }
              }
            ]
          }
        }
      ]
    }
  }
}

filtered query - bool must filter - array

sample

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  query: {
    filtered: {
      query: {
        query_string: {
          query: '*I spy an @kyspy \\? \\* &*',
          default_operator: 'AND',
          fields: [
            'name',
            'first_name',
            'parent.name'
          ]
        }
      },
      filter: {
        bool: {
          must: [
            {
              bool: {
                should: [
                  {
                    term: {
                      name: 'Professor Pythonesque'
                    }
                  },
                  {
                    term: {
                      name: 'Abraham Lost In San Francisco'
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

var filter = {
  term: {
    name: 'Antwan Goes To School'
  }
}

var builder = new FilterBuilder(queryClause)

builder.append(filter)

Δδ

builder.toQueryClause()

{
  query: {
    filtered: {
      query: {
        query_string: {
          query: '*I spy an @kyspy \\? \\* &*',
          default_operator: "AND",
          fields: [
            'name',
            'first_name',
            'parent.name'
          ]
        }
      },
      filter: {
        bool: {
          must: [
            {
              bool: {
                should: [
                  {
                    term: {
                      name: 'Professor Pythonesque'
                    }
                  },
                  {
                    term: {
                      name: 'Abraham Lost In San Francisco'
                    }
                  }
                ]
              }
            },
            {
              bool: {
                should: [
                  {
                    term: {
                      name: 'Antwan Goes To School'
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

filtered query - bool must filter - object

sample

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  query: {
    filtered: {
      query: {
        query_string: {
          query: '*I spy an @kyspy \\? \\* &*',
          default_operator: 'AND',
          fields: [
            'name',
            'first_name',
            'parent.name'
          ]
        }
      },
      filter: {
        bool: {
          must: {
            term: {
              name: 'Antwan Atkins'
            }
          }
        }
      }
    }
  }
}

var filter = {
  term: {
    name: 'Antwan Goes To School'
  }
}

var builder = new FilterBuilder(queryClause)

builder.append(filter)

Δδ

builder.toQueryClause()

{
  query: {
    filtered: {
      query: {
        query_string: {
          query: '*I spy an @kyspy \\? \\* &*',
          default_operator: "AND",
          fields: [
            'name',
            'first_name',
            'parent.name'
          ]
        }
      },
      filter: {
        bool: {
          must: [
            {
              term: {
                name: 'Antwan Atkins'
              }
            },
            {
              bool: {
                should: [
                  {
                    term: {
                      name: 'Antwan Goes To School'
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

filtered query - bool should filter

sample

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  query: {
    filtered: {
      query: {
        query_string: {
          query: '*I spy an @kyspy \\? \\* &*',
          default_operator: 'AND',
          fields: [
            'name',
            'first_name',
            'parent.name'
          ]
        }
      },
      filter: {
        bool: {
          should: [
            {
              bool: {
                must: {
                  term: {
                    name: 'Antwan Atkins'
                  }
                }
              }
            },
            {
              bool: {
                must: {
                  term: {
                    name: 'J.Cole World'
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

var filter = {
  term: {
    name: 'Antwan Goes To School'
  }
}

var builder = new FilterBuilder(queryClause)

builder.append(filter)

Δδ

builder.toQueryClause()

{
  query: {
    filtered: {
      query: {
        query_string: {
          query: '*I spy an @kyspy \\? \\* &*',
          default_operator: 'AND',
          fields: [
            'name',
            'first_name',
            'parent.name'
          ]
        }
      },
      filter: {
        bool: {
          must: [
            {
              bool: {
                should: [
                  {
                    bool: {
                      must: {
                        term: {
                          name: 'Antwan Atkins'
                        }
                      }
                    }
                  },
                  {
                    bool: {
                      must: {
                        term: {
                          name: 'J.Cole World'
                        }
                      }
                    }
                  }
                ]
              }
            },
            {
              bool: {
                should: [
                  {
                    term: {
                      name: 'Antwan Goes To School'
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

filtered query - simple filter

sample

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  query: {
    filtered: {
      query: {
        query_string: {
          query: '*I spy an @kyspy \\? \\* &*',
          default_operator: 'AND',
          fields: [
            'name',
            'first_name',
            'parent.name'
          ]
        }
      },
      filter: {
        term: {
          name: 'Antwan Atkins'
        }
      }
    }
  }
}

var filter = {
  term: {
    name: 'Antwan Goes To School'
  }
}

var builder = new FilterBuilder(queryClause)

builder.append(filter)

Δδ

builder.toQueryClause()

{
  query: {
    filtered: {
      query: {
        query_string: {
          query: '*I spy an @kyspy \\? \\* &*',
          default_operator: 'AND',
          fields: [
            'name',
            'first_name',
            'parent.name'
          ]
        }
      },
      filter: {
        bool: {
          must: [
            {
              term: {
                name: 'Antwan Atkins'
              }
            },
            {
              bool: {
                should: [
                  {
                    term: {
                      name: 'Antwan Goes To School'
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

query

sample

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  query: {
    query_string: {
      query: "*I spy an @kyspy \\? \\* &*",
      default_operator: "AND",
      fields: ['name', 'first_name', 'parent.name']
    }
  }
}

var filter = {
  term: {
    name: 'Antwan Atkins'
  }
}

var builder = new FilterBuilder(queryClause)

builder.append(filter)

Δδ

builder.toQueryClause()

{
  query: {
    filtered: {
      query: {
        query_string: {
          query: '*I spy an @kyspy \\? \\* &*',
          default_operator: 'AND',
          fields: ['name', 'first_name', 'parent.name']
        }
      },
      filter: {
        bool: {
          should: [
            {
              term: {
                name: 'Antwan Atkins'
              }
            }
          ]
        }
      }
    }
  }
}

simple filter

sample

var FilterBuilder = require('elasticsearch-filter-append')

var queryClause = {
  filter: {
    term: {
      name: 'Antwan Atkins'
    }
  }
}

var filter = {
  term: {
    name: 'Antwan Atkins'
  }
}

var builder = new FilterBuilder(queryClause)

builder.append(filter)

Δδ

builder.toQueryClause()

{
  filter: {
    bool: {
      must: [
        {
          term: {
            name: 'Antwan Atkins'
          }
        },
        {
          bool: {
            should: [
              {
                term: {
                  name: 'Antwan Atkins'
                }
              }
            ]
          }
        }
      ]
    }
  }
}