nextapps-de / flexsearch

Next-Generation full text search library for Browser and Node.js
Apache License 2.0
12.53k stars 491 forks source link

bool search gives errors #247

Closed ishanuda closed 2 years ago

ishanuda commented 3 years ago

Hi,

I have configured the search index and trying to search the index using bool conditions (AND/OR). However, the search gives me an error saying ' Cannot read property 'search' of undefined'

Example: https://codesandbox.io/s/flexsearch-bool-search-pdz3h?file=/src/index.js

Thank you very much.

ts-thomas commented 3 years ago

Thanks for the example. The query configuration from your example is not supported. This for example will work:

var result = index.search([{
  field: "title",
  query: "some query"
},{
  field: "title",
  query: "some other query"
}]);

The boolean flag needs to be set in the root of the configuration payload and can just switch between "or" / "and" boolean model for all fields in this query. What I can suggest is split your query into two or more multiple queries or apply your boolean logic on top of the result-set. Theoretically when using a combination of 4 boolean flags, it could have a lot of different combinations (dependent on how the bool needs to be applied like inner/outer). I came up with an idea which is already on my list for upcoming features, where you can call helper methods to combine any complex and/or/xor pattern easily.

ishanuda commented 3 years ago

Hi, Thank you for the answer.

  1. Can I use an array for the field? ex:
var result = index.search([{
  field: ["title", "subTitle", "content"]
  query: "some query"
},{
  field: ["title", "subTitle", "content"]
  query: "some other query"
}]);
  1. I did not understand where to configure the boolean flag in the root and use switching the flag.

  2. If you can please modify the example or direct me with an example existing with this kind of case that would be really helpful.

Thank you very much.

ts-thomas commented 2 years ago

The boolean flag is currently just applied across fields. That means you can specify if a term should included in every of the field or just at least in one of the fields.

var result = index.search("some query", {
  bool: "and",
  field: ["title", "subTitle", "content"]
});

The example above just return results from which every field matches the query. When you like to apply logical operator within same field but different terms, then you will need to apply your custom bool logic manually when getting back the result. Also consider you can make multiple search on specific fields against different queries you can process and merge when getting back (search over all fields VS. multiple searches over single field has the same computing complexity).