将基于单词的solr搜索转换为edge n gram

时间:2014-07-03 06:55:13

标签: solr solr-schema

我的Solr根据单词给出结果,但在输入字符时不显示结果。 示例:它将“This”的结果显示为“This is a book”,但没有“Th”的结果。

有人可以帮助我使用我的配置进行边缘搜索

这是我的架构:

<?xml version="1.0" encoding="UTF-8"?>
<schema>
   <types>
      <!-- field type used for autocomplete feature in 
              conjunction with a suggester component -->
<fieldType name="long" class="solr.TrieLongField" precisionStep="0"     positionIncrementGap="0" />
<fieldType name="edgytext" class="solr.TextField" positionIncrementGap="100">
 <analyzer type="index">
   <tokenizer class="solr.KeywordTokenizerFactory"/>
   <filter class="solr.LowerCaseFilterFactory"/>
   <filter class="solr.EdgeNGramFilterFactory" minGramSize="1" maxGramSize="25" />
 </analyzer>
 <analyzer type="query">
   <tokenizer class="solr.KeywordTokenizerFactory"/>
   <filter class="solr.LowerCaseFilterFactory"/>
 </analyzer>
</fieldType>
      <fieldType name="text" class="solr.TextField" positionIncrementGap="100">
         <analyzer>
            <tokenizer class="solr.WhitespaceTokenizerFactory" />
            <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1"     generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0"     splitOnCaseChange="1" />
            <filter class="solr.LowerCaseFilterFactory" />
         </analyzer>
      </fieldType>
      <fieldType name="text_auto" class="solr.TextField">
         <analyzer>
            <tokenizer class="solr.KeywordTokenizerFactory" />
            <filter class="solr.LowerCaseFilterFactory" />
         </analyzer>
      </fieldType>
   </types>
   <fields>
<field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/>
      <field name="id" type="text" indexed="true" stored="true" multiValued="false"     required="true" />
      <field name="name" type="text" indexed="true" stored="true" multiValued="false" />
<field name="author" type="text" indexed="true" stored="true" multiValued="false" />
      <field name="description" type="text" indexed="true" stored="true" multiValued="false" />
      <field name="name_auto" type="edgytext" indexed="true" stored="true" multiValued="true" omitNorms="true"
  omitTermFreqAndPositions="true"/>
<field name="weight" type="long" indexed="true" stored="true" multiValued="false" />
<copyField source="id" dest="name_auto" />
<copyField source="author" dest="name_auto" />
      <copyField source="name" dest="name_auto" />
   </fields>
   <uniqueKey>id</uniqueKey>
    </schema>

这是我的solrconfig文件:     ...

 <searchComponent name="suggest" class="solr.SpellCheckComponent">
<lst name="spellchecker">
  <str name="name">suggest</str>
  <str name="classname">org.apache.solr.spelling.suggest.Suggester</str>
  <str name="lookupImpl">org.apache.solr.spelling.suggest.tst.TSTLookup</str>
  <str name="field">name</str>
  <str name="sourceLocation">dict.txt</str>
 </lst>
</searchComponent>

...
<requestHandler name="/select" class="solr.SearchHandler">
    <!-- default values for query parameters can be specified, these
         will be overridden by parameters in the request
      -->
     <lst name="defaults">
<str name="echoParams">NONE</str>
<str name="defType">edismax</str>
<str name="rows">10</str>    
<str name="fl">id,author,name,description,weight,name_auto</str>
<str name="qf">name_auto</str>
<str name="sort">weight desc</str>
<str name="wt">json</str>
<str name="q.op">AND</str>
<str name="spellcheck">true</str>
<str name="spellcheck.count">1</str>
<str name="spellcheck.onlyMorePopular">true</str>
<str name="spellcheck.accuracy">0.7</str>
<str name="spellcheck.collate">true</str>
</lst>
<arr name="last-components">
<str>suggest</str>
</arr>
    </requestHandler>

我是Solr的新手。任何帮助都将被指定。 :)

2 个答案:

答案 0 :(得分:1)

我终于得到了n克的工作。这是我的架构现在的样子。

<?xml version="1.0" encoding="UTF-8"?>
<schema>
   <types>
      <!-- field type used for autocomplete feature in 
              conjunction with a suggester component -->

<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0" />

<fieldType name="edgytext" class="solr.TextField" positionIncrementGap="100">

<analyzer type="index">
            <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-    ISOLatin1Accent.txt" />
                <tokenizer class="solr.StandardTokenizerFactory" />
                <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1"     generateNumberParts="1" catenateWords="0"
                    catenateNumbers="0" catenateAll="0" splitOnCaseChange="1" />
                <filter class="solr.LowerCaseFilterFactory" />
                <filter class="solr.EdgeNGramFilterFactory" maxGramSize="20"     minGramSize="1" side="front" />
                <filter class="solr.PatternReplaceFilterFactory" pattern="    ([^\w\d\*æøåÆØÅ ])" replacement="" replace="all" />
            </analyzer>
            <analyzer type="query">
                <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-    ISOLatin1Accent.txt" />
                <tokenizer class="solr.StandardTokenizerFactory" />
                <filter class="solr.WordDelimiterFilterFactory" generateWordParts="0"     generateNumberParts="0" catenateWords="0"
                    catenateNumbers="0" catenateAll="0" splitOnCaseChange="0" />
                <filter class="solr.LowerCaseFilterFactory" />
                <filter class="solr.PatternReplaceFilterFactory" pattern="    ([^\w\d\*æøåÆØÅ ])" replacement="" replace="all" />
                <filter class="solr.PatternReplaceFilterFactory" pattern="^(.{20})(.*)?"     replacement="$1" replace="all" />
            </analyzer>
</fieldType>

      <fieldType name="text" class="solr.TextField" positionIncrementGap="100">
         <analyzer>
            <tokenizer class="solr.WhitespaceTokenizerFactory" />
            <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1"     generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0"     splitOnCaseChange="1" />
            <filter class="solr.LowerCaseFilterFactory" />
         </analyzer>
        </fieldType>

        <fieldType name="text_auto" class="solr.TextField">
         <analyzer>
            <tokenizer class="solr.KeywordTokenizerFactory" />
            <filter class="solr.LowerCaseFilterFactory" />
         </analyzer>
      </fieldType>

   </types>
   <fields>
<field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/>
      <field name="id" type="text" indexed="true" stored="true" multiValued="false"     required="true" />
      <field name="name" type="edgytext" indexed="true" stored="true"     multiValued="false" />
<field name="author" type="text" indexed="true" stored="true" multiValued="false" />
      <field name="description" type="text" indexed="true" stored="true"     multiValued="false" />
      <field name="name_auto" type="edgytext" indexed="true" stored="true"     multiValued="true" omitNorms="true"
  omitTermFreqAndPositions="true"/>
<field name="weight" type="long" indexed="true" stored="true" multiValued="false" />
<copyField source="author" dest="name_auto" />
      <copyField source="name" dest="name_auto" />
   </fields>
   <uniqueKey>id</uniqueKey>
</schema>

solr配置与上面粘贴的相同。这适用于边缘n克。

请注意,每当您更改架构文件时,请务必删除旧数据并重新添加。

答案 1 :(得分:0)

那些喜欢使用Schema API的人-这对我有用!

curl -X POST -H 'Content-type:application/json' --data-binary '
{
"add-field-type": {
  "name": "edgeGramText",
  "class": "solr.TextField",
  "positionIncrementGap": "100",
  "indexAnalyzer": {
    "tokenizer": {
      "class": "solr.KeywordTokenizerFactory"
    },
    "filters": [
      {
        "class": "solr.LowerCaseFilterFactory"
      },
      {
        "class": "solr.NGramTokenizerFactory",
        "maxGramSize": "25",
        "minGramSize": "1"
      }
    ]
  },
  "queryAnalyzer": {
    "tokenizer": {
      "class": "solr.KeywordTokenizerFactory"
    },
    "filters": [
      {
        "class": "solr.LowerCaseFilterFactory"
      }
    ]
  }
},
"add-dynamic-field": {
  "name": "*_eg",
  "type": "edgeGramText",
  "indexed": true,
  "stored": true
}
}]' http://localhost:8983/solr/collectionName/schema

我也将其添加为动态字段,因此我不想更改我添加的所有edgeGram字段的架构。我只需要确保字段名称以后缀“ eg”结尾。

希望这对大多数使用SolrCloud的人很有帮助!