Apachesolr, Drupal, Php

Drupal 7 Apache solr add custom sort to solr query parameter

Drupal 7 Apache solr add custom field to solr search index was my last post where i described how to add custom field to solr search index. Here i am going to describe how to add custom sort to solr query parameter so that you get result sorted with that field.

To modify the query parameter there is a hook provided by Apache solr search integration module hook_apachesolr_query_prepare() this hook is similar to hook_apachesolr_query_alter() but it is called before this hook.

You can apply sort on the basis of any single value field in apache solr search index like score, create date, update data, sticky etc. Before applying sort you have to make that available as sort field by calling the method setAvailableSort of DrupalSolrQueryInterface and then calling setSolrSort method you can set ascending or descending sort on that field. As here i am going to apply sort based on score field.

function apache_solr_search_facet_block_apachesolr_query_prepare(&amp;$query){
  $query->setAvailableSort('score', array('title' => t('Most Relevant'), 'default' => 'asc'));
  $query->setSolrSort('score', 'desc');

You can also remove the existing sort field with removeAvailableSort method.

For drupal 6 these method were named as set_available_sort() and set_solr_sort(). See the following post  for more detail:


Apachesolr, Drupal, Php

Drupal 7 Apache solr add custom field to solr search index

Drupal 7 Apache solr search integration module is most useful to integrate Apache Solr with drupal. Apache solr search integration module will index all the content of your sites at solr which is full with advance search feature. You can exclude some of content types from being index under setting of this module. When you index your content at solr it will index most of the field of your content type but some of the custom field will not be indexed but Apache solr integration module provides number of hooks which you can use to add custom field to solr index, to alter the query before it sent to apache solr or alter the search result before being displayed.

For Solr Indexing:

You need to use hook_apachesolr_index_document_build. This hook is fairly new in the Solr module (since version 7.x-1.0-beta14) and it replaces the deprecated hook_apachesolr_update_indexhook. Here’s how to use it in your custom module:

function MY_MODULE_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
  $price = $entity->field_price;
  $document->addField('ss_product_price', $price / 100);

Here notice i added the field the name ss_product_price. This is to follow the apache solr naming convention for dynamic field. Here ss means string single field. The first ‘s’ means its string value field, this field will hold only string value, and second ‘s’ means this is single value field. When we will search for some terms then solr will expect to search for single string in this field. If we store text (containing more string) value to this field then search will not work as expected. If the field you are indexing can have more then one value then loop over each value and us sm_ prefix with the field name. If you adding text field then try ts_ and tm_ for text single and text multi value respectively. Drupal Apache solr integration module support to index dynamic fields. For detail read the Dynamic field section in schema.xml shipped with apache solr module.


This tells Solr to add the new field into the set of returned fields.

For Sorl Searching:

Now you’ve got the new field in Solr’s search index. By default this new field doesn’t get returned when you search from Solr and therefore you need to use the hook_apachesolr_query_alter hook.

function MY_MODULE_form_apachesolr_query_alter($query) {
  $query->addParam('fl', 'ss_product_price');

Here i added ‘fl’ which stands for field in addParam function to add parameter to query.

End result:

Using these two hooks you can add custom field to solr index and can query to solr to search in that custom field. These two hooks will only work for Drupal 7. For drupal 6 follow this post.


If adding custom field in solr index works but search within that field doesn’t work then there is chance that you indexed the field as String field and you are trying to search complete text within that field in this case search will not work. You need to index field as text field with ts_ prefix in custom field name and altering the query by adding ‘qf’ parameter in query like this:

  function MY_MODULE_form_apachesolr_query_alter($query) {
    $query->addParam('qf', 'ts_product_price');

Have a look at this post on drupal.org for more clarification.


Loading image through Ajax?

Loading image through ajax! Is it possible? I thought it is possible before i really need to implement it. During some project there was a requirement to load a gif image through ajax. So i just simply tried

$('#popup-container').click(function() {
    type: "GET",
    url: "img/popup_calculating.gif",
    dataType: "image/gif",
    success: function(img) {
      i = new Image();
      i.src = img;
    error: function(error, txtStatus) {

But rather then getting  a image what i get was error !! It was parsererror. It mean browser is making request asynchronously and getting the data but its not able to understand the returned data. Because the dataType supported by ajax are  xml, html, script and json.

So what should i do? I should think how browser load images when i do something like this

<img title="myimage" src="myimage.png" alt="" >

Browser send the request to url specified in src property of img tag and load image asynchronously with image/png specified in contentType header request but in case of ajax it only accept xml, html, script, and json datatype.
So what should i do!! Think?

When browser already loading images asynchronously then why don’t i use that functionality. Just create a img tag on the fly and put the src property to url. Yeah i got the solution. So here is simple jquery plugin to load image asynchronously.

$.fn.image = function(src, f) {
  return this.each(function() {
    var i = new Image();
    i.src = src;
    i.onload = f;

Simply put this code in script tag or in js file. And to load image in a container call the image() function like this

  alert("The image is loaded now");

That’s it!


Changes Are Good

“Chages Are Good !” Really they are damn good.

Changes are good

Changes is good you go first

My life is going to change and i accepted this change. I was doing freelancing since last 3 years and never thought i would join any full time job. I thought i will never fit with job. But the good & bad experiences i had in last year and changes i felt now i ended up with joining a full time job. I am joining a web development company srijan.in in Gurgaon Haryana. Really the changes are not accepted so easily. Our mind resist to changes but once we make our mind & heart to accept changes this change may lead to us where we really want to go.

Definitely this changes are going to good for my life because i have made my mind for this change and have positive approach for this change. Its going to be great to work with new people, learn and share ideas and knowledge. I accepted this change with lots of dream in my eyes hope this change will lead to me there. 😛


Piratepad Beta

Via Scoop.itrocking-css3

Piratepad Its Perfact tool for webdeveloper and team member of any businees those who want to work in collaborative environment. It provide real time live editing of to do list or any thing which you want to work as team and need realtime updation. It have cool feature like rich text, different color for each user text, image and link import, realtime chat and much more. Check it out.
Via piratepad.net


import command for screenshots in linux

Screenshots: Every computer guy need it either you are normal computer user or developer.
There are many tools for screenshots but what about screenshots in linux.
If you use gnome as desktop manager then there is utility available for this gnome-screenshots.
In terminal type


if you little bit nerdy and linux guy then definitely you like terminal. There is a tool to capture screenshot directly from command line. This is the import command. This command is part of a tool called ImageMagick. Before using this command you should have this tool installed on your system. This tool is for image processing and work as back end for several applications.
You can install this tool via command line.

yum install ImageMagick.

For more detail you can see this link

import filename

As you type this command your mouse pointer converts into cross “X” mark and now you can select any region or any window to capture. If you want to capture any particular window then just select that window, or any region by selecting it.

you can specify different file extensions like(jpeg, png, gif etc) in which format you want to save your screenshot. If you didn’t specify any extension then by default it saves in poscript format.


Recursively extract all compressed files in a dir

Today i need to upload some tar.gz files to ftp server then i want to extract them. one way was to extract each file individually. Then i thought to write a simple shell script to extract them because i have ssh access to that server. So this is the simple script to extract all the tar.gz compressed files in a directory.

for user in $( ls *.tar.gz )
tar zxvf $user

if you have zip file then change the *.tar.gz to *.zip and tar zxvf to unzip. If you have tar.bz2 file change tar zxvf to tar jxvf