首页 文章

如何配置SOLR服务器以进行拼写检查功能

提问于
浏览
2

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

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

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

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

Steps I've done until now:

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

Step 2: 添加到Envoierment变量(您有solr服务器的地方):

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

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

或导入项目变更和战争 .

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

和libs

Questions:

1.如何通过我的数据库 Build 数据库连接并搜索内容以查看是否有任何可匹配的单词?
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 config: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

我真的需要你的帮助 . 退休 .

2 回答

  • 2

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

    您需要将数据从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以配置拼写检查,并提供建议 .

  • 6

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

相关问题