日光浴室的基本全文搜索

时间:2014-12-18 15:11:40

标签: php search solr full-text-search solarium

只需将Solr与Solarium结合使用,即可在网站上搜索搜索表单。 我想搜索像#34; word1 word2 word3"在所有列中,并按最高搜索分数排序结果。

我成功导入了solr数据库中的所有数据,日光浴中的简单选择示例输出如下:

NumFound: 21421

有一些详细的结果。

一旦我开始将搜索词添加到搜索中,我就得到0结果。当我指定搜索列时,像" body:word1"搜索确实有效并返回结果。我在搜索代码中做错了什么或者我的配置错了吗?

搜索代码:

// 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>

3 个答案:

答案 0 :(得分:2)

您应指定搜索字段。 如果您想搜索所有字段,那么您应该这样做:

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

或者,您也可以使用dismax / edismax查询类型:

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

$query->setQuery('searchTerm');

答案 1 :(得分:0)

您可以添加此行

  

$ query-> setQuery(“ searchFeild:searchTerm”);

请注意此行

  

$ query-> setQuery(“ *:searchTerm”);

答案 2 :(得分:0)

您可以如下动态地进行操作:-

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