olivernn / lunr.js

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

How to search strings that are made with JSON.stringify? #184

Closed beshur closed 9 years ago

beshur commented 9 years ago

Hi.

I am making a web log viewer using lunr.js and I have the string like this:

07.10.2015 03:41:45 [DEBUG] send: connId: b959a0 msg: {"error":null,"id":"9046d19aaec1ef2fa6546a05d89f4e99%%144420370560637","result":{}}
07.10.2015 03:41:47 [DEBUG] on_message: connId: e9ab70 msg: {"jsonrpc":"2.0","method":"Ping","id":"771874b2-b4d3-4f74-96f3-d6640077b26d","params":{}}
07.10.2015 03:41:47 [DEBUG] send: connId: e9ab70 msg: {"error":null,"id":"771874b2-b4d3-4f74-96f3-d6640077b26d","result":"Pong"}

At the moment, when I just add them to index as a string, the search does not return anything that is inside JSON {}. What is a good way to search it too? I see two options here:

  1. Split JSON from string, parse it and add it into a separate field of the index.
  2. Redefine tokenizer (which is very dark topic for me).

Any comments on this?

beshur commented 9 years ago

Went along with the option 1.

Something like this:

// el - is a main string to search

var add = {
    id: Date.now() + Math.ceil(Math.random()*1000),
    body: el
};

// split the json piece (which is msg: {})
var json = (/msg: ({.*})/).exec(el);

if (json.length > 1) {
    // replacing quotes (") to enable search
    add.json = json[1].replace(/"/g, " ");
}

this.logIndex.add(add);