summaryrefslogtreecommitdiff
path: root/content/blog/web-search-homepage.md
blob: a3a1cc384e9388ba02ef0c2c4ee4bada597f202d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
+++
title = "Web search for my homepage"
date = "2017-04-12T15:49:11+01:00"
categories = [ "Strus", "Search", "Information Retrieval", "Web" ]
thumbnail = "/images/blog/web-search-homepage/search.png"
+++

## Intro
I wanted to add a search function to my web page.
As the website is built with Hugo as a set of static
HTML pages onto a read-only web server, standard
approaches didn't work like a LIKE-query in Mysql
as many CMS are implementing search.

The big logo gives it away, it's done with the
strusWebService from the [Strus](http://project-strus.net/)
project. 

The basic idea is that the author of the web pages
can build a search index locally with the markdown version
of his content and then push it to a web service dedicated
to search only. Again, the files making up the search index 
can be set to read-only after an update, leaving the system
open to only DOSA or DDOSA (but which public system isn't).

## Installing strus for content indexing

So, I installed the 'strusutilities' package for ArchLinux
on my local machine from the
[Open Build Service](https://software.opensuse.org/download.html?project=home:PatrickFrey&package=strusutilities)
with:

```
pacman -S strusutilities
```

This gives me the command line tools to build a search index.

## Generating the documents readable by strus

The command line tools consist of tools to analyze the document,
apply some basic parsing and normalization of search terms.

The tools take XML, JSON or TSV (tab-separated-values) currently.
My Hugo documents have their metadata in
[TOML](https://en.wikipedia.org/wiki/TOML) and the content in
[Markdown](https://de.wikipedia.org/wiki/Markdown):

```
+++
title = "Web search for my homepage"
date = "2017-04-12T15:49:11+01:00"
categories = [ "Strus", "Search", "Information Retrieval" ]
thumbnail = "/images/blog/web-search-homepage/strus.jpg"
+++

I wanted to add a search function to my web page.
As the website is built with Hugo as a set of static
...
```

So I first have to convert the blog entries to a big XML
file using:

 * [remarshal](https://github.com/dbohdan/remarshal): converts
   the metadata in TOML/YAML to JSON
 * [pandoc](http://pandoc.org/): convert markdown to
   tons of formats

I choose to convert to a [DocBook](http://docbook.org/whatis) style
of XML and put all the posts into one big file called `posts.xml`.
The metadata is embedded as a JSON value into the XML file in a tag `<meta>`.

The final XML file looks like:

```
<posts>
  <post>
    <slug>/blog/web-search-homepage</slug>
    <filename>../content/blog/.../xx.md</filename>
    <meta>
      {
       "categories" : ["Strus","Search",
                       "Information Retrieval"],
       "date"       : "2017-04-12T15:49:11+01:00",
       "thumbnail"  : "/images/blog/.../strus.jpg",
       "title"      : "Web search for my homepage"
      }
    </meta>
    <body>
      <para>
        I wanted to add a search function to my
        web page. As the website is built with
        Hugo as a set of static
...
```

The cool thing about the way Strus is analyzing the document
is that you can choose where to split the document into logical
documents (in our case the blog entries and web pages) and
you can switch the format from XML to JSON in embedded content.

I packaged this whole ugly conversion step into a script like that:

```
./create_xml.sh > posts.xml
```

## Configuring the document analysis and indexing process

Now we define the configuration for the text analysis. Basically,
we tell the system where to split the document into retrievable
items, which features we want to be able to search for and what
attributes and text we want to show in the rank list.

The file `document.ana` contains a configuration which describes
how Strus should analyze and index the documents:

```
[Document]
  post = /posts/post;
```

which means split on the XPath expression `/posts/post`.

```
[Content]
  "encoding=UTF-8; content=JSON;" /posts/post/meta();
```

This says that our metadata tag contains a different format JSON.
Later we can select the individual fields inside the XML tag `meta`
by specifying a path to the JSON key:

```
[Attribute]
  ...
  title = orig content /posts/post/meta()/title();
  categories = orig content /posts/post/meta()/categories();
  thumbnail = orig content /posts/post/meta()/thumbnail();
   ...
```

The other attributes we select from the XML tags directly:

```
[Attribute]
	docid = orig content /posts/post/slug();
```

Now for the things we want to be able to search for. For now
simply stemmed words in English are fine:

```
[SearchIndex]
	word = lc:convdia(en):stem(en):lc regex("([A-Za-z']+)") /posts/post/meta()/title();
	word = lc:convdia(en):stem(en):lc regex("([A-Za-z']+)") /posts/post/body//para();
	sentence = empty punctuation("en") /posts/post/body//para();
```

The sentence marker is used to pick the best sentences later
when presenting the hit in the rank list.

```	
[ForwardIndex]
	title = orig split /posts/post/meta()/title();
	text = orig split /posts/post/body//para();
```

The forward index stores the document almost verbatim as a sequence
of title and text tokens. So when we get a hit in a search result
we can present a selection of them (usually a sentence containing
the matches) in the rank list.

Finally, we need to count the number of words per document,
this is needed for the retrieval function:

```
[Aggregator]
	doclen = count( word );
```   

With that we can now index our collection and build a search index:

```
strusCreate -s 'path=storage/wwwandreasbaumanncc; metadata=doclen UINT16, publish_date UINT16'
strusInsert -c 1000 -f 1 -t 1 -s "path=storage/wwwandreasbaumanncc" document.ana posts.xml
```

The `storage` directory contains the complete search index which we
can copy to the server running the `strusWebService`.

## Installing the strusWebService for querying

On a publicly available server I installed the 'strusWebService' package from the
[Open Build Service](https://software.opensuse.org/download.html?project=home:PatrickFrey&package=struswebservice)
with:

```
pacman -S struswebservice
```

The search index I copied into `/srv/strusWebService/storage`.

And I started the services:

```
systemctl enable strusWebService
systemctl start strusWebService
```

## Extending the Hugo theme adding a search text field

So, the static web page needs a search field now and
some Javascript code doing the search request to the
strusWebService (with JSON on HTTP).

This leads to the problem of CORS (Cross-Origin-Requests)
which have to be configured in the strusWebService (which
must allow requests originating from the server which 
delivered the static HTML page generated with Hugo).

Also I had to stem and lowercase the query terms with
a [Snowball Javascript library](https://github.com/fortnightlabs/snowball-js)
as the strusWebService doens't support query analysis right now.


## Workflow

So, when I change or add a page in to my website I have to run:

```
./create_xml.sh > posts.xml
rm -rf storage
mkdir storage
strusCreate -s 'path=storage/wwwandreasbaumanncc; metadata=doclen UINT16, publish_date UINT16'
strusInsert -c 1000 -f 1 -t 1 -s "path=storage/wwwandreasbaumanncc" document.ana posts.xml
scp storage/wwwandreasbaumann.cc struswebservice.home:/srv/strusWebService/storage
```

and of course stop and restart the strusWebService because I replaced
it's search index completely.

# Outlook and future steps

There are several things I want to improve from here.

First, the conversion to XML/JSON is quite hacky and slow.
Writting segmenters for strus for new file formats like
TOML or markdown is not that complicated.

A future web service should also support indexing, otherwise
we cannot properly analyze the query (using the Snowball JS stemmer
in the browser is a hack which doesn't work if the query analysis
is more complex). 

The upload to the remote webserver can be done more elegantly.
One idea is to use the backup/replication API of strus for this:
build an index locally and sync it to the strusWebService.

Update March 2018: currently the ARM version of strus is completely broken
and the strusWebService is being rewritten. So there will be an updated
blog entry when those things are fixed.

# Sources

As always you can find the sources of the project on my github (also hosted
on the same Raspberry Pi, of course) at:

http://git.andreasbaumann.cc/cgit/www-andreasbaumann-cc/

The strus specific scripts and configuration are in:

http://git.andreasbaumann.cc/cgit/www-andreasbaumann-cc/tree/strus