question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question - is it possible to order index by word occurrences (hits)

See original GitHub issue

I’m trying to do a simple blog search that sorts search results by keyword occurrences. The default FlexSearch behavior seems to try to retain the original order of the content as it was added to the Index… which makes sense for a book. Is this possible with flexsearch@0.6.x or flexsearch@0.7.x?

Here is a simple example, where each page just a ‘id’ and the ‘body’. When I search for a keyword, ‘cat’, I want the posts with the most occurrences of ‘cat’ first.

import FlexSearch from "flexsearch"
type IndexableDocument = Record<string, unknown>

const index = FlexSearch.create<IndexableDocument>({})

const document = [
  { id: `threecat`, body: `cat meows. cat stretch. cat sleep.` },
  { id: `onecat`, body: `cat is still asleep` },
  { id: `catcatdogdog`, body: `dog barks at cat. cat runs from dog.` },
  { id: `onedog`, body: `dog sleeps` },
  { id: `threedog`, body: `dog woofs. dog chases tail. dog sleeps` },
]
document.forEach(({ id, body }) => {
  index.add(id as number, body)
})

index.search("cat") // goal: [ 'threecat', 'catcatdogdog', 'onecat' ]
index.search("dog") // goal: [ 'threedog', 'catcatdogdog', 'onedog' ]

// actual flexsearch@0.6.x output, respects order of original content
// cat: ["threecat","onecat","catcatdogdog"]
// dog: ["catcatdogdog","onedog","threedog"]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ts-thomascommented, Jun 30, 2021

Good news, it is now supported by the option “boost”: https://github.com/nextapps-de/flexsearch#index-options

Example:

const index = new Index({ 
    boost: function(words, term, index){
        let factor = 0;
        for(let i = 0; i < words.length; i++){ 
            if(words[i] === term) factor++; 
        }
        return factor || 1;
    }
});
0reactions
mmm8955405commented, Apr 8, 2022

term ,Appear other words!!!!…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get search word Hits ( number of occurences) per document ...
Lucene uses a field-based, rather than document-based, index. In order to get term counts per document: Iterate over documents using IndexReader.document() ...
Read more >
sql server - Counting occurrence of words in table is slow
UPDATE dbo.words WITH (TABLOCK) SET hits = ( SELECT COUNT_BIG(*) FROM dbo. ... I'm asking these questions because I can get a similar...
Read more >
Return Multiple Match Values in Excel - Xelplus - Leila Gharani
The VLOOKUP and INDEX & MATCH formulas are great for looking up a value in a large data table and returning a result...
Read more >
The Anatomy of a Large-Scale Hypertextual Web Search Engine
The hits record the word, position in document, an approximation of font size, and capitalization. The indexer distributes these hits into a set...
Read more >
Probability of an Event: Simple Steps in Plain English
The question asks about the probability of a cat owner having fewer than three cats. In order words, you want to find how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found