neděle 17. února 2008

How to highlight a phrase on results from Lucene.Net

You came here probably because of Google search or mentioning this page on Stack Overflow. However this down is from 2008 and I am pretty sure it is not working anymore, last time I checked, there were no Lucene.Net.Highlight in Lucene.Net 2.4, sorry...

If you are Outlook, Remember The Milk or Producteev user interested in synchronization, you may have a look at my Outlook synchronization pages.



----------------------------------------------------------------------------------

Adding Lucene.Net based search to HawkWiki was really easy. The results page was showing only the links and I wanted to see also a part of the resulting page. I decided to use the highlighting feature in Lucene.Net.Highlight. I have not found Lucene.Net.Highlight.dll compiled, so I have compiled version 2.0.0 and you can download it. The code to highlight part of the page was also very simple. First instantiate the highlighter based on you search query:


Lucene.Net.Highlight.QueryScorer scorer = new Lucene.Net.Highlight.QueryScorer( query, reader, ContentsField );
Highlighter highlighter = new Highlighter( scorer );


If you are going through search hits, open the content and let highlighter to highlight the content:


StreamReader sr = File.OpenText(path);
string content = sr.ReadToEnd();
sr.Close();

string fragment = highlighter.GetBestFragment( analyzer, ContentsField, content );
result[ i ] = new Result();
string relPath = path.Replace( rootDir, "" );
result[ i ].Path = relPath;
result[ i ].Title = relPath.Remove( 0, 1 ).Replace( ".wiki", "" );
result[ i ].Fragment = fragment;


Simple, isn't it.

pondělí 11. února 2008

Wiki

UPDATE!
If you are interested in source code of wiki.podhola.net, please have a look at HawkWikiEx.

I have started a wiki on wiki.podhola.net. Why? You can find it on the start page there. The other reason was to have a wiki implementation working on Mono. I have started with HawkWiki and changed it a little bit. You can downloadit if you want.

sobota 9. února 2008

Ubuntu 7.10: server install from network on software RAID1

Okay, so I am writing the steps I took to get Ubuntu 7.10 Server on my computer. I had to use the installation from the network as I am not able to boot from CD (have a look here why). As I was unsuccessful last time I started from scratch.

(The first part is taken from this tutorial)
1. let's say you have Windows XP
2. create folder C:\boot
3. download files linux and initrd.gz to c:\boot
4. Download Grub4Dos from project page on sf.net
5. unzip grub4dos-0.4.3.zip
6. Copy c:\boot\grub4dos-0.4.3\grldr to C:\
7. create folder C:\boot\grub
8. Copy c:\boot\grub4dos-0.4.3\menu.lst to C:\boot\grub
9. Open Command Line: Start - Run - cmd
10. run attrib -a -r -s -h c:\boot.ini in the command line
11. run notepad c:\boot.ini in the command line
12. add C:\grldr="Start GRUB" at the end of boot.ini and save it
12. return to the command line and run notepad c:\boot\grub\menu.lst
13. to the end of the file add
title Ubuntu Installer (hd0,0)
kernel (hd0,0)/boot/linux vga=normal ramdisk_size=14972 root=/dev/rd/0 rw --
initrd (hd0,0)/boot/initrd.gz
and save it
14. remove CD and floppy and restart
15. when booting choose Start GRUB option from Windows loader (press F8 to get there)
16. after a while you should see GRUB4DOS window. Select the last option Ubuntu Installer (hd0,0)
17. Ubuntu installer should start after a while (Choose language)
18. Configure everything and choose also your network mirror
19. The installer should stop at "Partition disks"
20. We need to create 2 same layouts on both disk - the easiest thing how to do this is to create partitions RAID partitions from the end on both disk (8GB let say), do not forget to set the type to Linux raid autodetect
21. We also need to have a partition with root filesystem so let's create it on the second disk from the beginning the same size as Windows partition
22. Once installed, you can reboot and have your system running
23. run sudo fdisk -l to make sure the partitions are the same size and the correct type Linux raid autodetect
24. download the software raid tools sudo apt-get install mdadm
25. create the RAID by sudo mdadm --create /dev/md0 -l 1 -n 2 /dev/sda2 /dev/sdb1
26. format the new disk by sudo mkfs -t ext3 /dev/md0
27. mount the new disk sudo mount /dev/md0 /mnt/
28. we have to copy all the data from current root (e.g. /dev/sdb2) to the new disk: su
tar cl --exclude=/media/sda1 -–exclude=/proc -–exclude=/mnt -–exclude=/sys / | tar xvC /mnt
exit
. You should ommit --exclude /media/sda1 if windows partition is not mounted. You should not ommit the other excludes. You might check the status cat /proc/mdstat
29. we have to change fstab of the newly to-be-booted system by sudo nano /mnt/etc/fstab. Change the beginning of the line with / from something like UUID=03... to /dev/md0.
30. create ommited folders sudo mkdir /mnt/proc /mnt/sys; sudo chmod 555 /mnt/proc
31. it the RAID partition on the same disk as the original bootable partition is not set as bootable, set it sudo fdisk /dev/sdb and then a 1 and w.
32. we have to change booting with GRUB so sudo nano /boot/grub/menu.lst. Find the default entry, copy the lines before the default entry and change root (e.g. from root (hd1,1) to the new RAID partition on the same disk root (hd1,0)). Also change kernel line from root=UUID=xycxcxy... to root=/dev/md0.
33. copy changed file to mnt as well sudo cp /boot/grub/menu.lst /mnt/boot/grub/menu.lst
34. change /etc/mdadm/mdadm.conf by

su
mdadm --examine --scan > /tmp/mdadm.conf
cat /tmp/mdadm.conf >> /etc/mdadm/mdadm.conf
cp /etc/mdadm/mdadm.conf /mnt/etc/mdadm/mdadm.conf
exit

35. change /usr/share/initramfs-tools/init by sudo nano /usr/share/initramfs-tools/init and add line /sbin/mdadm --assemble --scan before mountroot
36. build init by sudo update-initramfs -k all -u. copy sudo cp /boot/initrd.img-2.6.22-14-generic /mnt/boot/initrd.img-2.6.22-14-generic.
37. install grub to the boot sector sudo grub-install /dev/sda and sudo grub-install /dev/sdb.
38. reboot. You should be up and running. Check by mount - the / partition should be /dev/md0, if you check cat /proc/mdstat you should see md0 : active.

Hope you find this helpful and enjoy it. If something is going wrong, you can always boot the second option,mount your RAID on /mnt and go through the files. You still need the original linux partition as it contains boot files.

RAID1 as root filesystem on Ubuntu 7.10

OK, so it was not that easy but following the very nice quide and finding some information how to solve my problems on Ubuntu here, here, here and here, I had RAID up and running for a while...


The most important part was changing /usr/share/initramfs-tools/init and adding mdadm --assemble --scan there. You have to run sudo update-initramfs -k all -u then and also sudo grub-install /dev/md0.
Also do not forget to add something like ARRAY /dev/md0 level=raid1 num-devices=2 UUID=cbc59996:6e0056a8:325ecf68:79913751 at the end of your /etc/mdadm/mdadm.conf.

The strange thing is, that I am still stuck at the boot now, but I hope I will be able to get over it.

pondělí 4. února 2008

.Net Twitter Badge

I needed something very simple to show my twits on my homepage and written in DotNet (C#). I have downloaded the code from RSS.NET, compiled it into .NET 1.1 DLL and created a very simple code that you can embed into your .ASPX page. The library is here and it should be put in /bin folder in your website folder. The code could look somehow like this:


<%
string url = "http://twitter.com/statuses/user_timeline/8607272.rss";
Rss.RssFeed feed = Rss.RssFeed.Read(url);
Rss.RssChannel channel = (Rss.RssChannel)feed.Channels[0];
foreach (Rss.RssItem item in channel.Items)
{
Response.Write( item.Description + "&nbsp;" );
Response.Write( "<small><a href='" +item.Link + "'>" + item.PubDate + "</a></small><br><br>" );
}
%>


Make sure your page has .ASPX extension and starts with famous


<%@ Page Language="C#" Debug="false" %>