Skip to content

Instantly share code, notes, and snippets.

@simon-lang
Last active April 21, 2021 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simon-lang/1b7e9431ef8afe3965f58270d4641d09 to your computer and use it in GitHub Desktop.
Save simon-lang/1b7e9431ef8afe3965f58270d4641d09 to your computer and use it in GitHub Desktop.

Author Search

Step 1:

Use this aggregation:

{
  "aggregations": {
    "result": {
      "terms": {
        "field": "author.display_name.keyword",
        "size": 100,
        "include": "(R.* J.*)|(J.* R.*)"
      }
    }
  },
  "size": 0,
  "from": 0
}

For the following three queries:

q=author.display_name:(richard~2 jefferson~2)

q=author.display_name:("richard jefferson")

q=((author.display_name:(richard*) AND author.display_name:(*jefferson))) OR ((author.display_name:(richard*) AND author.display_name:(jefferson*)))

Step 2:

Combine and deduplicate the results of all 3 aggregations

Step 3:

Modify the results of aggregation to include a new field, noMiddleNames which is just the first and last word for each.

So "George R R Martin" just becomes "George Martin"

Step 4:

Run list through this local Fuse fuzzy search:

{
    shouldSort: true,
    tokenize: true,
    matchAllTokens: true,
    findAllMatches: true,
    location: 0,
    distance: 100,
    maxPatternLength: 32,
    keys: [
        {
            name: 'key',
            weight: 1,
        },
        {
            name: 'noMiddleNames',
            weight: 0.5,
        },
    ],
}

https://fusejs.io/demo.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment