flexsearch icon indicating copy to clipboard operation
flexsearch copied to clipboard

bool search gives errors

Open ishanuda opened this issue 2 years ago • 2 comments

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.

ishanuda avatar Jul 27 '21 11:07 ishanuda

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.

ts-thomas avatar Jul 30 '21 12:07 ts-thomas

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.

ishanuda avatar Jul 30 '21 13:07 ishanuda

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).

ts-thomas avatar Oct 03 '22 14:10 ts-thomas