Jeroen
Jeroen

Reputation: 2031

basic full text search with solarium

Just trying out Solr in combination with Solarium for a search form on a website. I would like to search for a combination of words like "word1 word2 word3" in all columns, and get results sorted by the highest search score.

I successfully imported all the data in the solr database and the simple select example from solarium outputs the following:

NumFound: 21421

With a few detailed results.

As soon as i start to add the search words to the search i get 0 results. When i specify the search column like "body:word1" the search does work and returns results. am i doing something wrong in de search code or do i have my configuration wrong ?

Search code:

// create a client instance
$client = new Solarium_Client($config);

// get a select query instance
$query = $client->createSelect();
$query->setFields(array('id','title','description','body'));
#$query->setQuery("searchTerm"); //this does not work 
#$query->setQuery("body:searchTerm"); //this does work 

// this executes the query and returns the result
$resultset = $client->select($query);

// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();

schema.xml

<?xml version="1.0" encoding="UTF-8" ?>



<schema name="example" version="1.5">

   <field name="_version_" type="long" indexed="true" stored="true"/>  
   <field name="_root_" type="string" indexed="true" stored="false"/>
   <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
   <field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/>   
   <field name="description" type="text_general" indexed="true" stored="true"/>
   <field name="body" type="text_general" indexed="true" stored="true"/> 
   <field name="content" type="text_general" indexed="false" stored="true" multiValued="true"/>
   <field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/>
   <uniqueKey>id</uniqueKey>

    <fieldType name="string" class="solr.StrField" sortMissingLast="true" />

    <!-- boolean type: "true" or "false" -->
    <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>

    <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" positionIncrementGap="0"/>

    <fieldType name="tint" class="solr.TrieIntField" precisionStep="8" positionIncrementGap="0"/>
    <fieldType name="tfloat" class="solr.TrieFloatField" precisionStep="8" positionIncrementGap="0"/>
    <fieldType name="tlong" class="solr.TrieLongField" precisionStep="8" positionIncrementGap="0"/>
    <fieldType name="tdouble" class="solr.TrieDoubleField" precisionStep="8" positionIncrementGap="0"/>

    <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
      <analyzer type="index">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
        <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
    </fieldType>

    <!-- lowercases the entire field value, keeping it as a single token.  -->
    <fieldType name="lowercase" class="solr.TextField" positionIncrementGap="100">
      <analyzer>
        <tokenizer class="solr.KeywordTokenizerFactory"/>
        <filter class="solr.LowerCaseFilterFactory" />
      </analyzer>
    </fieldType>
</schema>

Upvotes: 2

Views: 2870

Answers (3)

user3322296
user3322296

Reputation: 1

You can do it dynamically as below:-

$helper = $query->getHelper();
$query->setQuery($searchField .':'. $helper->escapePhrase($searchValue));

Upvotes: 0

user10899318
user10899318

Reputation:

You can add this line

$query->setQuery("searchFeild:searchTerm");

insteed of this line

$query->setQuery("*:searchTerm");

Upvotes: 0

notdang
notdang

Reputation: 500

You should specify the search fields. If you want to search through all the fields, then you should do this:

$query->setQuery("*:searchTerm");

Or, alternatively, you can use dismax/edismax query type:

$dismax = $query->getDisMax();
$dismax->setQueryFields('theFieldIwantToLookInto1 theFieldIwantToLookInto2');

$query->setQuery('searchTerm');

Upvotes: 2

Related Questions