Simple Photo Search with Solr and Tika
Join the DZone community and get the full member experience.Join For Free
recently we had a change to help with a non-commercial project which included search as its part. one of the assumptions, although not the key ones, was the photo search functionality, so that the user could find the pictures fast and accurately. because the search had to work with meta data of jpeg files, the idea was simple – use apache solr with apache tika.
assumptions were quite simple – the user should be able to find photos by their file name, author and other data available in exif, like aperture, shutter speed, focal length or iso value. another thing was that solr should take care of grabbing the meta data from jpeg files, so this was definitely something we wanted use solr cell for. as you can see, those assumptions were simple.
index structure was very simple and contained only most needed fields. the fields section of the schema.xml file looked as follows:
<field name="id" type="string" indexed="true" stored="true" required="true" /> <field name="name" type="text" indexed="true" stored="true" /> <field name="author" type="text" indexed="true" stored="true" /> <field name="iso" type="text" indexed="true" stored="true" multivalued="true" /> <field name="iso_string" type="text" indexed="true" stored="true" multivalued="true" /> <field name="aperture" type="double" indexed="true" stored="true" /> <field name="exposure" type="string" indexed="true" stored="true" /> <field name="exposure_time" type="double" indexed="true" stored="true" /> <field name="focal" type="string" indexed="true" stored="true" /> <field name="focal_35" type="string" indexed="true" stored="true" /> <dynamicfield name="ignored_*" type="string" indexed="false" stored="false" multivalued="true" />
the dynamic field was added to ignore the data we weren’t interested in. also the copyfield was introduced to copy the iso field value to iso_string field to enable faceting.
the following handler definition was added to solrconfig.xml file:
<requesthandler name="/update/extract" class="solr.extraction.extractingrequesthandler"> <lst name="defaults"> <str name="uprefix">ignored_</str> <str name="lowernames">true</str> <str name="captureattr">true</str> <str name="fmap.stream_name">name</str> <str name="fmap.artist">author</str> <str name="fmap.exif_isospeedratings">iso</str> <str name="fmap.exif_fnumber">aperture</str> <str name="fmap.exposure_time">exposure</str> <str name="fmap.exif_exposuretime">exposure_time</str> <str name="fmap.focal_length">focal</str> <str name="fmap.focal_length_35">focal_35</str> </lst> </requesthandler>
a few words about configuration. the uprefix parameter tells solr which prefix it should use for the fields that were not mentioned explicitly in the handler configuration. in the above case, the fields which were not mentioned will be prefixed with the ignored_ word. that means that they will be matched by the dynamic field and thus they won’t be indexed ( stored=”false” and indexed=”false” ). the lowernames parameter with the value of true will cause all the field names to be lowercased. the captureattr parameter tell solr, to catch file attributes. the next parameters in the above configuration is mapping definition between fields returned by tika and fields in the index. for example, fmap.exif_fnumber with the value of aperture says solr to place the value of tika exif_fnumber in the aperture index field.
additional, needed libraries
in order for the above configuration to work we need some additional libraries (similar to the ones described in language identification ). from the dist directory that is available in solr distribution we copy the apache-solr-cell-3.5.0.jar file to tikadir directory that should be created at the same level as the webapps directory in solr deployment (of course this is an example). next we add the following like to the solrconfig.xml file:
<lib dir="../tikalib/" />
the above tell solr to include all the libraries from the given directory. next we need to copy all the jar files from the contrib/extraction/ solr distribution directory to the created tikadir directory. additional solrconfig.xml changes are not needed.
the assumptions were, that there will be about 10.000 new photos a week that will need to be indexed. those photos will be stored in a shared file system location. a simple bash script was responsible for choosing the files that were needed to be indexed and during its work it run the following command for each file:
curl 'http://solrmaster:8983/solr/photos/update/extract?literal.id=9926&commit=true" -f "myfile=@wisla_2011_10_10.jpg"
the above command sends a file names wisla_2011_10_10.jpg to /extract handler and says to run commit command after its processing. in addition to that, the unique id of the file is set (the literal.id parameter).
i addition to some standard filtering by author or other attributes of the photo it was also desired for the search to work. yeah, just work we decided, that if we were the users of the application, we would like the fields like author or file name to be important. so, we decided to start with the following query:
as you can see, the query is simple. two fields in the index are more valuable then others – name of the photo and its author. the value of those fields were set up by adding query time boosts. the rest of the fields are without boost, so the default boost of 1 applies.
to sum up
the described deployment is really simple. the applications works as so the search the next steps that will have to be done is the jvm and solr tunning. one of the most important things would be looking at the users behavior and tune up searches to make search experience as good as possible. but let’s leave it for other solr.pl post.
Opinions expressed by DZone contributors are their own.
Azure Virtual Machines
Java Concurrency: Condition
A Complete Guide to Agile Software Development
Authorization: Get It Done Right, Get It Done Early