古い質問ですが、私は次のように返信しています
私は最近、この質問に関連する同様の質問をしました(数日経っても返信やコメントはありません)。
Solr は何年にもわたって大幅に変更されており、このトピックに関する既存のドキュメント (存在する場合) は混乱を招くだけでなく、間違っている場合もあります。
長くなりますが、この返信は、例とドキュメントを使用して質問に対する解決策を提供します。
手短に言えば、削除された私の StackOverflow の質問は、「Apache Solr を使用して HTML からカスタム (例: <my_id></my_id) タグ付きテキストを抽出する」というものでした。そのタスクに付随するのは、カスタム HTML 要素: 属性を含む HTML ページをインデックス化する方法でした。
簡単に言うと、「標準」の HTML 要素 (a; div; h1; h2; li; meta; p; title; ... https://www.w3.org/TR/2005 ) のインデックスを作成するのは比較的簡単ですが、 /WD-xhtml2-20050527/elements.html )、Solr で適切にフォーマットされた XML ファイルと更新関数を厳密に使用せずにカスタム タグセットを含めることは困難です (例: https://lucene.apache.org/solr/を参照)。 guide/6_6/uploading-data-with-index-handlers.html#uploading-data-with-index-handlers )、またはcaptureAttr
Apache Tika でのパラメーターの使用、Solr ネイティブExtractingRequestHandler
(以下で説明) またはその他のツールアパッチ・ナットとして。
のような標準の HTML 要素<title>Solr HTML Indexing Tests</title>
は、簡単に索引付けできます。ただし、次のような非標準の要素<my_id>bt-ic8eew2u</my_id>
は無視されます。
などの XML ベースのソリューションを適用することもできますが<field name="my_id">bt-ic8eew2u</field>
、私は簡単な HTML ベースのソリューション、つまり HTML メタデータ アプローチを好みます。
環境: Arch Linux (x86_64) コマンドライン; Apache Solr 8.7.0; FireFox 83.0 の Solr 管理 UI (http://localhost:8983/solr/#/gettingstarted/query)
テストファイル (solr_test9.html):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-us">
<head>
<meta charset="UTF-8" />
<title>Solr HTML Indexing Tests</title>
<meta name="date_created" content="2019-11-01" />
<meta name="source_url" content="/mnt/Vancouver/programming/datasci/solr/test/solr_test9.html" />
<!-- <my_id>bt-ic8eew2u</my_id> -->
<meta name="doc_id" content="bt-ic8eeW2U" />
<meta name="date_pub" content="2020-11-16" />
</head>
<body>
<h1>Apples</h1>
<p>I like apples.</p>
<h2>Bananas</h2>
<p>I also like bananas.</p>
<p><div id="div1">This text is located in div element 1.</div></p>
<p><div id="div2">This text is located in div element 2.</div></p>
<br/>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<br/>
<p>Suspendisse efficitur pulvinar elementum.</p>
<p>My website is <a href="https://buriedtruth.com/">BuriedTruth.com</a>.</p>
<h1>Nova Scotia</h1>
<p>Nova Scotia is a province on the east coast of Canada.</p>
<h2>Capital of Nova Scotia</h2>
<p>Halifax is the capital of N.S.</p>
<p>Halifax is also N.S.'s largest city.</p>
<h1>British Columbia</h1>
<h2>Capital of British Columbia</h2>
<p>Victoria is the capital of B.C.</p>
<p>Vancouver is the largest city in B.C., however.</p>
<p>Non-terminated sentence (missing period)</p>
<meta name="date_current" content="2020-11-17" />
<!-- Comments like these are not indexed. -->
<p>Current date: 2020-11-17</p>
</body>
</html>
solrconfig.xml
これが私のsolrconfig.xml
ファイルへの関連する追加です。
<!-- SOLR CELL PLUGINS: -->
<lib dir="${solr.install.dir:../../..}/contrib/extraction/lib" regex=".*\.jar" />
<lib dir="${solr.install.dir:../../..}/dist/" regex="solr-cell-\d.*\.jar" />
<!-- https://lucene.472066.n3.nabble.com/Prons-an-Cons-of-Startup-Lazy-a-Handler-td4059111.html -->
<requestHandler name="/update/extract"
class="org.apache.solr.handler.extraction.ExtractingRequestHandler">
<lst name="defaults">
<str name="lowernames">true</str>
<str name="uprefix">ignored_</str>
<str name="capture">div</str>
<str name="fmap.div">div</str>
<str name="capture">h1</str>
<str name="fmap.h1">h1</str>
<str name="capture">h2</str>
<str name="fmap.h2">h2_t</str>
<str name="capture">p</str>
<!-- <str name="fmap.p">p_t</str> -->
<str name="fmap.p">p</str>
<!-- COMMENT: note that the entries above refer to standard -->
<!-- HTML elements. As long as you have <meta/> (metadata) -->
<!-- entries ("doc-id", "date_pub" ...) in your schema then -->
<!-- Solr will automatically pick them up when indexing ... -->
<!-- (hence no need to include those, here!). -->
</lst>
</requestHandler>
<!-- https://doc.lucidworks.com/fusion-server/5.2/reference/solr-reference-guide/7.7.2/update-request-processors.html -->
<!-- The update.autoCreateFields property can be turned to false to disable schemaless mode -->
<updateRequestProcessorChain name="add-unknown-fields-to-the-schema" default="${update.autoCreateFields:true}"
processor="uuid,remove-blank,field-name-mutating,parse-boolean,parse-long,parse-double,parse-date,add-schema-fields">
<processor class="solr.LogUpdateProcessorFactory"/>
<processor class="solr.DistributedUpdateProcessorFactory"/>
<!-- ======================================== -->
<!-- https://lucene.apache.org/solr/7_4_0/solr-core/org/apache/solr/update/processor/RegexReplaceProcessorFactory.html -->
<processor class="solr.RegexReplaceProcessorFactory">
<str name="fieldName">content</str>
<str name="fieldName">title</str>
<str name="fieldName">p</str>
<!-- Case-sensitive (and only one pattern:replacement allowed, so use as many copies): -->
<!-- of this processor as needed: -->
<str name="pattern">\s+</str>
<str name="replacement"> </str>
<bool name="literalReplacement">true</bool>
</processor>
<!-- Solr bug? URLs parse as "rect https..." Managed-schema (Admin UI): defined p as text_general -->
<!-- but did not parse. Looking at content | title: text_general copied to string, so added -->
<!-- copyfield of p (text_general) as p_str ... regex below now works! -->
<!-- https://stackoverflow.com/questions/22178700/solr-extractingrequesthandler-extracting-rect-in-links/64882751#64882751 -->
<processor class="solr.RegexReplaceProcessorFactory">
<str name="fieldName">content</str>
<str name="fieldName">title</str>
<str name="fieldName">p</str>
<!-- Case-sensitive (and only one pattern:replacement allowed, so use as many copies): -->
<!-- of this processor as needed: -->
<str name="pattern">rect http</str>
<str name="replacement">http</str>
<bool name="literalReplacement">true</bool>
</processor>
<!-- ======================================== -->
<!-- This needs to be last (may need to clear documents and re-index to see changes, e.g. Solr Admin UI): -->
<processor class="solr.RunUpdateProcessorFactory"/>
</updateRequestProcessorChain>
管理スキーマ (schema.xml):
管理 UI を介して Solr スキーマを編集しました。基本的に、インデックスを作成する HTML メタデータが何であれ、同様の名前のフィールド (適切なタイプ: text_general
| string
| pdate
| ... など) を追加します。
たとえば、「doc-id」と「date_pub」のメタデータを取得するために、次の (それぞれの) スキーマ エントリを作成しました。
<field name="doc_id" type="string" uninvertible="true" indexed="true" stored="true"/>
<field name="date_pub" type="pdate" uninvertible="true" indexed="true" stored="true"/>
索引付け
HTMLテストファイルをインデックス化する方法は次のとおりです。
[victoria@victoria solr-8.7.0]$ date; pwd; ls -l; echo; ls -l server/solr/gettingstarted/conf/
Tue Nov 17 02:18:12 PM PST 2020
/mnt/Vancouver/apps/solr/solr-8.7.0
total 1792
drwxr-xr-x 3 victoria victoria 4096 Nov 17 13:26 bin
-rw-r--r-- 1 victoria victoria 946955 Oct 28 02:40 CHANGES.txt
drwxr-xr-x 12 victoria victoria 4096 Oct 29 07:09 contrib
drwxr-xr-x 4 victoria victoria 4096 Nov 15 12:33 dist
drwxr-xr-x 3 victoria victoria 4096 Nov 15 12:33 docs
drwxr-xr-x 6 victoria victoria 4096 Oct 28 02:40 example
drwxr-xr-x 2 victoria victoria 36864 Oct 28 02:40 licenses
-rw-r--r-- 1 victoria victoria 12646 Oct 28 02:21 LICENSE.txt
-rw-r--r-- 1 victoria victoria 766662 Oct 28 02:40 LUCENE_CHANGES.txt
-rw-r--r-- 1 victoria victoria 27540 Oct 28 02:21 NOTICE.txt
-rw-r--r-- 1 victoria victoria 7490 Oct 28 02:40 README.txt
drwxr-xr-x 11 victoria victoria 4096 Nov 15 12:40 server
total 208
drwxr-xr-x 2 victoria victoria 4096 Oct 28 02:21 lang
-rw-r--r-- 1 victoria victoria 33888 Nov 17 13:20 managed-schema
-rw-r--r-- 1 victoria victoria 873 Oct 28 02:21 protwords.txt
-rw-r--r-- 1 victoria victoria 33788 Nov 17 11:36 schema.xml.2020-11-17.13:01
-rw-r--r-- 1 victoria victoria 59248 Nov 17 13:16 solrconfig.xml
-rw-r--r-- 1 victoria victoria 59151 Nov 17 12:59 solrconfig.xml.2020-11-17.13:01
-rw-r--r-- 1 victoria victoria 781 Oct 28 02:21 stopwords.txt
-rw-r--r-- 1 victoria victoria 1124 Oct 28 02:21 synonyms.txt
[victoria@victoria solr-8.7.0]$ solr restart; sleep 1; post -c gettingstarted /mnt/Vancouver/programming/datasci/solr/test/solr_test9.html
Sending stop command to Solr running on port 8983 ... waiting up to 180 seconds to allow Jetty process 3511453 to stop gracefully.
Waiting up to 180 seconds to see Solr running on port 8983 [|]
Started Solr server on port 8983 (pid=3572520). Happy searching!
/usr/lib/jvm/java-8-openjdk/jre//bin/java -classpath /mnt/Vancouver/apps/solr/solr-8.7.0/dist/solr-core-8.7.0.jar -Dauto=yes -Dc=gettingstarted -Ddata=files org.apache.solr.util.SimplePostTool /mnt/Vancouver/programming/datasci/solr/test/solr_test9.html
SimplePostTool version 5.0.0
Posting files to [base] url http://localhost:8983/solr/gettingstarted/update...
Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log
POSTing file solr_test9.html (text/html) to [base]/extract
1 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/gettingstarted/update...
Time spent: 0:00:00.755
[victoria@victoria solr-8.7.0]$
...そしてここに結果があります(Solr管理UI: http://localhost:8983/solr/#/gettingstarted/query)
http://localhost:8983/solr/gettingstarted/select?q=*%3A*
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"*:*",
"_":"1605651674401"}},
"response":{"numFound":1,"start":0,"numFoundExact":true,"docs":[
{
"id":"/mnt/Vancouver/programming/datasci/solr/test/solr_test9.html",
"stream_size":[1428],
"x_parsed_by":["org.apache.tika.parser.DefaultParser",
"org.apache.tika.parser.html.HtmlParser"],
"stream_content_type":["text/html"],
"date_created":"2019-11-01T00:00:00Z",
"date_current":["2020-11-17"],
"resourcename":["/mnt/Vancouver/programming/datasci/solr/test/solr_test9.html"],
"title":["Solr HTML Indexing Tests"],
"date_pub":"2020-11-16T00:00:00Z",
"doc_id":"bt-ic8eeW2U",
"source_url":"/mnt/Vancouver/programming/datasci/solr/test/solr_test9.html",
"dc_title":["Solr HTML Indexing Tests"],
"content_encoding":["UTF-8"],
"content_type":["application/xhtml+xml; charset=UTF-8"],
"content":[" en-us stream_size 1428 X-Parsed-By org.apache.tika.parser.DefaultParser X-Parsed-By org.apache.tika.parser.html.HtmlParser stream_content_type text/html date_created 2019-11-01 resourceName /mnt/Vancouver/programming/datasci/solr/test/solr_test9.html date_pub 2020-11-16 doc_id bt-ic8eeW2U source_url /mnt/Vancouver/programming/datasci/solr/test/solr_test9.html dc:title Solr HTML Indexing Tests Content-Encoding UTF-8 Content-Language en-us Content-Type application/xhtml+xml; charset=UTF-8 Solr HTML Indexing Tests Lorem ipsum dolor sit amet, consectetur adipiscing elit. "],
"div":[" div1 This text is located in div element 1. div2 This text is located in div element 2."],
"p":[" I like apples. I also like bananas. Suspendisse efficitur pulvinar elementum. My website is https://buriedtruth.com/ BuriedTruth.com . Nova Scotia is a province on the east coast of Canada. Halifax is the capital of N.S. Halifax is also N.S.'s largest city. Victoria is the capital of B.C. Vancouver is the largest city in B.C., however. Non-terminated sentence (missing period) Current date: 2020-11-17"],
"h1":[" Apples Nova Scotia British Columbia"],
"h2_t":" Bananas Capital of Nova Scotia Capital of British Columbia",
"_version_":1683647678197530624}]
}}
更新 -- managed-schema
>>schema.xml
特徴:
元の質問とは関係ありませんが、次の内容は私の回答 (上記) に関連しています。具体的には、Solr からmanaged-schema
クラシック (ユーザー管理) への切り替えに関連する特殊性schema.xml
です。完全なソリューションを提供するためにここに含まれています。
まず、追加
<schemaFactory class="ClassicIndexSchemaFactory"/>
あなたのsolrconfig.xml
ファイルに。
次に、これを編集します: -->
<updateRequestProcessorChain
name="add-unknown-fields-to-the-schema"
default="${update.autoCreateFields:true}"
processor="uuid,remove-blank,field-name-mutating,parse-boolean,
parse-long,parse-double,parse-date,add-schema-fields">
...これに:
<updateRequestProcessorChain
processor="uuid,remove-blank,field-name-mutating,parse-boolean,
parse-long,parse-double,parse-date">
つまり、削除します
name="add-unknown-fields-to-the-schema"
default="${update.autoCreateFields:true}"
add-schema-fields
名前を に変更managed-schema
しschema.xml
、Solr を再起動するか、コアをリロードして変更を有効にします。
私の例 (上記) をさらに拡張するために、 <updateRequestProcessorChain />
私が提供した HTML コード (上記) のサンプルと出力を次に示します。
solrconfig.xml (一部):
<updateRequestProcessorChain
processor="uuid,remove-blank,field-name-mutating,parse-boolean,parse-long,parse-double,parse-date">
<processor class="solr.LogUpdateProcessorFactory"/>
<processor class="solr.DistributedUpdateProcessorFactory"/>
<processor class="solr.RegexReplaceProcessorFactory">
<str name="fieldName">content</str>
<str name="fieldName">title</str>
<str name="fieldName">p</str>
<!-- Case-sensitive (and only one pattern:replacement allowed, so use as many copies): -->
<!-- of this processor as needed: -->
<str name="pattern">\s+</str>
<str name="replacement"> </str>
<bool name="literalReplacement">true</bool>
</processor>
<processor class="solr.RegexReplaceProcessorFactory">
<str name="fieldName">content</str>
<str name="fieldName">title</str>
<str name="fieldName">p</str>
<!-- Case-sensitive (and only one pattern:replacement allowed, so use as many copies): -->
<!-- of this processor as needed: -->
<str name="pattern">rect http</str>
<str name="replacement">http</str>
<bool name="literalReplacement">true</bool>
</processor>
<processor class="solr.RegexReplaceProcessorFactory">
<str name="fieldName">content</str>
<str name="fieldName">title</str>
<str name="pattern">[sS]olr</str>
<str name="replacement">APPLE</str>
<bool name="literalReplacement">true</bool>
</processor>
<processor class="solr.RegexReplaceProcessorFactory">
<str name="fieldName">content</str>
<str name="fieldName">title</str>
<str name="pattern">HTML</str>
<str name="replacement">BANANA</str>
<bool name="literalReplacement">true</bool>
</processor>
<processor class="solr.RunUpdateProcessorFactory"/>
</updateRequestProcessorChain>
出力
{
"responseHeader":{
"status":0,
"QTime":32,
"params":{
"q":"*:*",
"_":"1605767164812"}},
"response":{"numFound":1,"start":0,"numFoundExact":true,"docs":[
{
"id":"/mnt/Vancouver/programming/datasci/solr/test/solr_test9.html",
"stream_size":[1628],
"x_parsed_by":["org.apache.tika.parser.DefaultParser",
"org.apache.tika.parser.html.HtmlParser"],
"stream_content_type":["text/html"],
"date_created":"2020-11-11T21:36:38Z",
"date_current":["2020-11-17"],
"resourcename":["/mnt/Vancouver/programming/datasci/solr/test/solr_test9.html"],
"title":["APPLE BANANA Indexing Tests"],
"date_pub":"2020-11-16T21:37:18Z",
"doc_id":"bt-ic8eeW2U",
"source_url":"/mnt/Vancouver/programming/datasci/solr/test/solr_test9.html",
"dc_title":["Solr HTML Indexing Tests"],
"content_encoding":["UTF-8"],
"content_type":["application/xhtml+xml; charset=UTF-8"],
"content":[" en-us stream_size 1628 X-Parsed-By org.apache.tika.parser.DefaultParser X-Parsed-By org.apache.tika.parser.html.HtmlParser stream_content_type text/html date_created 2020-11-11T21:36:38Z resourceName /mnt/Vancouver/programming/datasci/APPLE/test/APPLE_test9.html date_pub 2020-11-16T21:37:18Z doc_id bt-ic8eeW2U source_url /mnt/Vancouver/programming/datasci/APPLE/test/APPLE_test9.html dc:title APPLE BANANA Indexing Tests Content-Encoding UTF-8 Content-Language en-us Content-Type application/xhtml+xml; charset=UTF-8 APPLE BANANA Indexing Tests Lorem ipsum dolor sit amet, consectetur adipiscing elit. "],
"div":[" div1 This text is located in div element 1. div2 This text is located in div element 2. apple This text is located in the \"apple\" (class) div element. banana This text is located in the \"banana\" (class) div element."],
"p":[" I like apples. I also like bananas. Suspendisse efficitur pulvinar elementum. My website is https://buriedtruth.com/ BuriedTruth.com . Nova Scotia is a province on the east coast of Canada. Halifax is the capital of N.S. Halifax is also N.S.'s largest city. Victoria is the capital of B.C. Vancouver is the largest city in B.C., however. Non-terminated sentence (missing period) Current date: 2020-11-17"],
"h1":[" Apples Nova Scotia British Columbia"],
"h2_t":" Bananas Capital of Nova Scotia Capital of British Columbia",
"_version_":1683814668971278336}]
}}