lucaong / minisearch

Tiny and powerful JavaScript full-text search engine for browser and Node
https://lucaong.github.io/minisearch/
MIT License
4.9k stars 137 forks source link

Is it possible to search multiple separate objects? #48

Closed Travis42 closed 4 years ago

Travis42 commented 4 years ago

In all of the examples I've encountered, minisearch does its thing against an object that contains different books, song titles, etc.

What I have been doing is using minisearch to find sections within a large document. That works. Format is: [ { id: 1, name: "Section Title", link_name: "an id tag to search in the HTML doc", description: "full text of that section to search against" }, ... ]

What I want to do next is search over multiple documents, ideally each composed of their own separate object. This would be a lot nicer than combining all my document objects into one giant object.

Is that possible? Thanks in advance.

lucaong commented 4 years ago

Hi @Travis42 , if I understand correctly, you want to be able to search within sections of a single large document, but also between several of those documents. Is that correct?

If so, you could use the storeFields option to store, for each section, the ID (or title) of the larger document that the section is part of. This way, the search results (each result being a section) will include the ID of the document they are part of.

Example:

const sections = [
  { id: 1, title: 'Section One, doc One', text: '...', documentId: 1, ... },
  { id: 2, title: 'Section Two, doc One', text: '...', documentId: 1, ... },
  { id: 3, title: 'Section One, doc Two', text: '...', documentId: 2, ... }
]

const miniSearch = new MiniSearch({
  fields: ['title', 'text'],
  storeFields: ['documentId']
})

miniSearch.search('two')
//=> [{ id: 2, documentId: 1, ... }, { id: 3, documentId: 2, ... }]
lucaong commented 4 years ago

Closing this issue as I assume the question is answered, but feel free to comment on it if I misunderstood your question, and I will reopen it.

Travis42 commented 4 years ago

You did, thanks. Its taken me longer to get back to this part of the code than I anticipated. :)