ndx icon indicating copy to clipboard operation
ndx copied to clipboard

ability to search by field

Open Georgehatouts opened this issue 8 years ago • 4 comments

Thank you for this repo, can I ask you if is possible to search by field, for example, search all the docks where field = address and the query is 'test' .

import { DocumentIndex } from "ndx";

const index = new DocumentIndex();
index.addField("title");
index.addField("content");

const documents = [
  {
    id: "doc1",
    title: "First Document",
    content: "Lorem ipsum dolor",
    address: "15",
  },
  {
    id: "doc2",
    title: "Second Document",
    content: "Lorem ipsum",
    address: "23",
  }
];

documents.forEach((doc) => {
  index.add(doc.id, doc);
});

index.search("First");
// => [{ docId: "doc1", score: ... }]

index.search("Lorem");
// => [{ docId: "doc2", score: ... }, { docId: "doc1", score: ... }]

Georgehatouts avatar Sep 04 '18 08:09 Georgehatouts

No, ndx doesn't support searching by field. It was designed to have a small memory footprint, so it stores as little information as possible.

localvoid avatar Sep 04 '18 08:09 localvoid

Thank you, any suggestion for javascript or typescript engine that has that feature. ?

Georgehatouts avatar Sep 04 '18 08:09 Georgehatouts

Just looked at lunr, elasticlunr and it seems that they support specifying which fields should be used for searching, but it is probably not enough for your use case if you want to create a query that uses different terms in different fields.

Don't know about any other full text search implementations, haven't looked at alternative implementations since I wrote this package two years ago, maybe nowadays there are more libraries.

localvoid avatar Sep 04 '18 08:09 localvoid

Thank you @localvoid

Georgehatouts avatar Sep 04 '18 09:09 Georgehatouts