olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.89k stars 545 forks source link

Search breaks in react-native release build but not in debug build #340

Open aecorredor opened 6 years ago

aecorredor commented 6 years ago

I've been going through the issues like crazy to find something similar to what is happening to me. The closest one I've found is #279 and the possible solutions haven't worked. My app breaks when searching in the release build for some terms, but not when it's on debug mode. I've also gone through the react-native issues and have tried to fix the minify/uglify bugs but haven't been able to solve the problem I'm having. I get TypeError: undefined is not an object (evaluating 'p._index') when I call search with 'maine maine v v' for example. The weird thing is that in debug mode it works as expected, but in the release build it crashes.

Here's my code for building the index, and my code for searching:

export default {
  createSearchIndex: function (items) {
    return lunr(function() {
      this.ref('id');
      this.field('title');
      this.field('description');

      items
        .forEach(item => {
          // Adds description index field to the outer object so videos are also
          // searchable by description as per requirements.
          const data = item && item.data || {};
          const description = data.description && data.description.toLowerCase() || '';

          const searchableItem = {
            ...item,
            description,
          }

          this.add(searchableItem);
        });
    });
  },
  search: (searchText, searchIndex, videoDocs) => {
    const query = searchText
      .toLowerCase()
      .split(/\s+/)
      // Add asterisk to partially match terms: ALA* -> ALASKA, ALAMO, etc...
      // this query will give a high positive boost to exact matches, and
      // a lower one to partially matched terms.
      .map(term => term && `${term}^100 ${term}*^10` || '')
      .join(' ')
      .trim();

    const searchResults = query ?
      searchIndex.search(query).map(result => videoDocs[result.ref]) :
      [];

    return searchResults;
  }
};
hoelzro commented 6 years ago

@aecorredor I know next to nothing about React Native, so bear with me, but how are you passing the pre-built search index to search? Are you keeping it around in a global variable, or are you serializing it to some storage and then deserializing it before you search?

aecorredor commented 6 years ago

@hoelzro I build the index when the app launches and then keep it in my redux store so I can connect different components that might need search. Just an FYI there’s an issue open in RN for minification breaking some libraries; I tried the specified solution but had no luck. I don’t want to have to switch search libraries just for that, because it works on development and just breaks on release. So I guess it has to do with some part of the library being incorrectly minified for production.

hoelzro commented 6 years ago

@aecorredor From the look of that error message, I'm guessing that's the issue

olivernn commented 6 years ago

@aecorredor do you have a link to the issue on RN for magnification problems? Might be worth updating the RN team with Lunr as a reproduction to help them test any fixes they have.

aecorredor commented 6 years ago

@oliviernn https://github.com/facebook/react-native/issues/9711

I had to switch to fuse.js for now which I didn’t want to do but had no other choice. I had already spent too much time figuring what part of the build process was breaking it. Hopefully we can figure it out. Cheers.

olivernn commented 6 years ago

@aecorredor Thanks for the link. A brief look through it suggests that react native is using uglify for minification, this is what is used by Lunr for generating minified code so it certainly is a strange bug.

aecorredor commented 6 years ago

@olivernn no problem. And also, I kind of discarded that it was a bug on my code from the get go because on RN debug builds it works perfectly fine and it never crashes.

yaronlevi commented 6 years ago

Any updates on using lunr + React Native?

dave-irvine commented 6 years ago

@aecorredor @yaronlevi are you still having problems in recent RN versions? I'm on RN 0.55 and not getting a crash in a Release build when I call idx.search('test*');

aecorredor commented 6 years ago

@dave-irvine I ended up having to switch to fuse.js so I wouldn’t be able to tell you. Sorry mate.

lucaong commented 5 years ago

I am quite confident that this issue with React Native was fixed by this PR:

https://github.com/olivernn/lunr.js/pull/361

The PR originally targeted a bug in Safari, and React Native on iOS uses the same JS engine as Safari.