【问题标题】:How to config SOLR server for spell check functionality如何为拼写检查功能配置 SOLR 服务器
【发布时间】:2013-09-04 10:49:18
【问题描述】:

我想使用 MySql 数据库实现 Solr 提供的拼写检查功能,但我不明白如何。
这是我想做的基本流程。

我有一个简单的 inputText(在 JSF 中),如果我输入单词 shwo,对 OutputLabel 的响应应该是 show

首先我使用以下工具和框架:

JBoss 应用服务器 6.1.
日食
JPA
JSF(Primefaces)

到目前为止我已经完成的步骤:

第 1 步: 从以下位置下载 Solr 服务器:http://lucene.apache.org/solr/downloads.html 提取内容。

第 2 步: 添加到环境变量(你有 solr 服务器的地方):

solr.solr.home=D:\JBOSS\solr-4.4.0\solr-4.4.0\example\solr

第 3 步:

打开 solr war 并在 solr.war\WEB-INF\web.xml 添加 env-entry - (简单的方法)

<env-entry>
    <env-entry-name>solr/home</env-entry-name>
    <env-entry-value>D:\JBOSS\solr-4.4.0\solr-4.4.0\example\solr</env-entry-value>
    <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

或导入项目变更和构建战争。

第 4 步: 浏览器:localhost:8080/solr/ 然后出现 solr 控制台。 到目前为止一切正常。

我发现了一些有用的代码(我的意见)返回:

[collection1] webapp=/solr path=/spell params={spellcheck=on&q=whatever&wt=javabin&qt=/spell&version=2&spellcheck.build=true} hits=0 status=0 QTime=16

这是给出上述结果的代码:

SolrServer solr;
try {
  solr = new CommonsHttpSolrServer("http://localhost:8080/solr");
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("qt", "/spell");
  params.set("q", "whatever");
  params.set("spellcheck", "on");
  params.set("spellcheck.build", "true");

  QueryResponse response = solr.query(params);
  SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse();
  if (!spellCheckResponse.isCorrectlySpelled()) {
    for (Suggestion suggestion : spellCheckResponse.getSuggestions()) {
      System.out.println("original token: " + suggestion.getToken() + " - alternatives: " + suggestion.getAlternatives());
    }
  }
} catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

我也在data-config.xml中添加了

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
  <dataSource type="JdbcDataSource" name="altadict"
    driver="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://localhost:3306/myproject"
    user="root"
    password=""
  />

  <document name="myproject">
    <entity name="myproject" query="SELECT * FROM words">
      <field column="Id" name="Id" />
      <field column="Cuvint" name="Cuvint" />
      <field column="TradDiac" name="TradDiac" />
      <field column="Explicatie" name="Explicatie" />
      <field column="TipCuvint" name="TipCuvint" />
      <field column="ItalicParant" name="ItalicParant" />
    </entity>
  </document>
</dataConfig>

schema.xml

<field name="Id" type="tlong" indexed="true" stored="true" required="true"/>
<field name="Cuvint" type="string" indexed="true" stored="true" required="true"/>
<field name="TradDiac" type="string" indexed="true" stored="true" required="true"/>
<field name="Explicatie" type="string" indexed="true" stored="true"/>
<field name="TipCuvint" type="string" indexed="true" stored="true" required="true"/>
<field name="ItalicParant" type="string" indexed="true" stored="true"/>

solrconfig.xml

<!-- altadict Request Handler -->

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
  <lst name="defaults">
    <str name="config">data-config.xml</str>
  </lst>
</requestHandler>

<requestHandler name="/spell" class="solr.SearchHandler" startup="lazy">
  <lst name="defaults">
    <str name="df">Cuvint</str> 
    <str name="spellcheck.dictionary">default</str> 
    <str name="spellcheck">on</str>
    <str name="spellcheck.extendedResults">true</str> 
    <str name="spellcheck.count">10</str>
    <str name="spellcheck.maxResultsForSuggest">5</str> 
    <str name="spellcheck.collate">true</str>
    <str name="spellcheck.collateExtendedResults">true</str> 
    <str name="spellcheck.maxCollationTries">10</str>
    <str name="spellcheck.maxCollations">5</str> 
  </lst>
  <arr name="last-components">
    <str>spellcheck</str>
  </arr>
</requestHandler>

<searchComponent name="spellcheck" class="solr.SpellCheckComponent">
  <str name="queryAnalyzerFieldType">string</str> <!-- Replace with Field Type of your schema -->
  <lst name="spellchecker">
    <str name="name">default</str>
    <str name="field">examplew</str> <!-- Replace with field name as per your scheme -->
    <str name="spellcheckIndexDir">./spellchecker</str>
    <str name="buildOnOptimize">true</str>
    <str name="buildOnCommit">true</str>
  </lst>

  <!-- a spellchecker that uses a different distance measure -->
  <lst name="spellchecker">
    <str name="name">jarowinkler</str> 
    <str name="field">spell</str>
    <str name="distanceMeasure">org.apache.lucene.search.spell.JaroWinklerDistance</str>
    <str name="spellcheckIndexDir">./spellchecker2</str>
  </lst>
</searchComponent>

和库

问题:

1.如何与我的数据库建立数据库连接并搜索内容以查看是否有任何可以匹配的单词?
2.如何进行配置(solr-config.xml,shema.xml...等)?
3.如何从我的视图(xhtml)发送一个字符串,以便 solr 服务器知道他在寻找什么?
4.如何从Cuvine数据库列中获取正确的单词,例如wodr 我希望solr返回word

我阅读了有关 solr 的所有信息,但仍不清楚:

链接:主页:
http://lucene.apache.org/solr/

主页教程: http://lucene.apache.org/solr/4_4_0/tutorial.html

Solr Wiki:
http://wiki.apache.org/solr/Solrj --- solrj 官方文档
http://wiki.apache.org/solr/SpellCheckComponent

Solr 配置: http://wiki.apache.org/solr/SolrConfigXml http://www.installationpage.com/solr/solr-configuration-tutorial-schema-solrconfig-xml/ http://wiki.apache.org/solr/SchemaXml

StackOverflow 证明: Solr Did you mean (Spell check component)

Solr 数据库集成:
http://www.slideshare.net/th0masr/integrating-the-solr-search-engine
http://www.cabotsolutions.com/2009/05/using-solr-lucene-for-full-text-search-with-mysql-db/

Solr 拼写检查:
http://docs.lucidworks.com/display/solr/Spell+Checking
http://searchhub.org/2010/08/31/getting-started-spell-checking-with-apache-lucene-and-solr/
http://techiesinsight.blogspot.ro/2012/06/using-solr-spellchecker-from-java.html
http://blog.websolr.com/post/2748574298/spellcheck-with-solr-spellcheckcomponent
How to use SpellingResult class in SolrJ

我真的需要你的帮助。问候。

【问题讨论】:

    标签: java configuration solr lucene


    【解决方案1】:

    1.我如何与我的数据库建立数据库连接并搜索内容以查看是否有任何可以匹配的单词?

    您需要将数据从 MySql 索引到 Solr。
    这可以通过构建一个应用程序来从 MySql 读取记录并将数据提供给 Solr 来完成。
    或者正如已经回答的那样,使用Data Import Handler (DIH) 这将使您能够 连接到 MySql 并加载数据并将其索引到 Solr。 此外,使您能够进行增量更新

    2.如何进行配置(solr-config.xml,shema.xml...等)?

    拼写检查的字段应标记为文本分析。
    由于您的字段被标记为字符串,因此没有标记化。
    Schema.xml

    <field name="Cuvint" type="text" indexed="true" stored="true" required="true"/>
    

    另外,对于 solrconfig.xml,替换您要考虑的拼写建议字段

    <str name="field">examplew</str> <!-- Replace with field name as per your scheme -->
    

    检查Example

    3.如何从我的视图(xhtml)中发送一个字符串,以便 solr 服务器知道他在寻找什么?

    通常,我们通过在 Solr 请求中结合搜索和拼写建议来实现此功能。
    当我们没有从 Solr 得到任何结果时,我们检查拼写检查建议是否可用并显示为 Did you mean 建议 此外,我们不等待拼写建议,而是向用户提供提前输入建议,以防止往返服务器。

    4.如何从Cuvine数据库列中获取正确的单词,例如wodr我希望solr返回单词。

    检查Example 以配置拼写检查,这应该会提供建议。

    【讨论】:

      【解决方案2】:

      使用DataImoprtHandler 将您的数据库导入 Solr,以便能够在 Solr 中搜索拼写。

      【讨论】:

      • 同意你的观点,但从我读过的内容来看,我需要从我的数据库中创建一个带有确切字段的 schema.xml,将其添加到 solr-config.xml。我需要一些示例假人:)。 +1 暗示
      • Solr 4.4-4.5 是无模式的,据我所知。阅读here,如何在不创建模式的情况下进行。但是,如果您只想对 1 个字段进行拼写,则可以轻松地为每个文档仅配置 1 个字段(使用示例中的一个,随 Solr 提供)。
      猜你喜欢
      • 2011-03-01
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多