从原始文本到分析器,再到分词器,再到过滤器,再到原始文本:在 solr 中如何?

From original text to analyzers to tokenizers to filters to original text back: how in solr?

考虑对阿尔伯特·爱因斯坦维基百科页面第一句话的分析:

http://localhost:8983/solr/#/trans/analysis?analysis.fieldvalue=Albert%20Einstein%20(14%20March%201879%20%E2%80%93%2018%20April%201955)%20was%20a%20German-born%20theoretical%20physicist%5B5%5D%20who%20developed%20the%20theory%20of%20relativity,%20one%20of%20the%20two%20pillars%20of%20modern%20physics%20(alongside%20quantum%20mechanics)&analysis.fieldtype=text_en&verbose_output=0

及其输出:

问题:有什么办法可以从 solr 中以某种半结构化的方式得到它吗?最后,我感兴趣的是引用从原始文本到最后一行的确切标记的字符序列..

Solr 中的 Web 界面是一个精简的 HTML/Javascript 应用程序,它通过调用回 Solr 的 REST 界面来执行任何实际工作。如果您在要求 Web 界面执行分析时查看浏览器中的网络选项卡,您可以看到它正在向以下地址发出请求:

http://localhost:8080/solr/corename/analysis/field?wt=json&analysis.showmatch=true&analysis.fieldvalue=foo%20bar&analysis.query=foo%20bar&analysis.fieldtype=text_no

响应是一个 JSON 结构,用于构建 UI 你看到的:

{
  "responseHeader":{
    "status":0,
    "QTime":108
  },
  "analysis":{
    "field_types":{
      "text_no":{
        "index":[
          "org.apache.lucene.analysis.standard.StandardTokenizer",
          [
            {
              "text":"foo",
              "raw_bytes":"[66 6f 6f]",
              "match":true,
              "start":0,
              "end":3,
              "org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength":1,
              "type":"<ALPHANUM>",
              "position":1,
              "positionHistory":[
                1
              ]
            },
            {
              "text":"bar",
              "raw_bytes":"[62 61 72]",
              "match":true,
              "start":4,
              "end":7,
              "org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength":1,
              "type":"<ALPHANUM>",
              "position":2,
              "positionHistory":[
                2
              ]
            }
          ],
          // .....
        ],
        "query":[
          "org.apache.lucene.analysis.standard.StandardTokenizer",
          [
             // ....
          ]
        ]
      }
    },
    "field_names":{

    }
  }
}

然后您可以遍历 indexquery 键并选择您需要的条目 (last/first/etc.)

URL 和响应格式可能在 Solr 版本之间发生了变化,但我很确定它在最近的主要版本中是稳定的。

您要查找的内容也可以使用 term-vector-component 检索。鉴于您在 solrconfig.xml 中启用了该组件(该文件必须包含以下行:)

  <searchComponent name="tvComponent" class="solr.TermVectorComponent"/>

  <requestHandler name="/tvrh" class="solr.SearchHandler" startup="lazy">
    <lst name="defaults">
      <bool name="tv">true</bool>
    </lst>
    <arr name="last-components">
      <str>tvComponent</str>
    </arr>
  </requestHandler>

并且架构必须正确配置组件(此处的类型与德语文本匹配):

<field name="trans" 
  type="text_de" 
  indexed="true" 
  termOffsets="true" 
  stored="true" 
  termPositions="true" 
  termVectors="true" 
  multiValued="true"/>

您可以使用

检索相应的值
http://localhost:8983/solr/trans/tvrh?q=trans:tag&rows=1&indent=true&tv.all=true&wt=xml

典型输出为

<lst name="zweck">
  <int name="tf">1</int> <- term frequency
  <lst name="positions">
    <int name="position">7</int> <!-- 7th word                       -->
  </lst>
  <lst name="offsets">
    <int name="start">45</int>   <!-- 45th byte in the original text -->
    <int name="end">52</int>     <!-- 52 byte                        -->
  </lst>
  <int name="df">7</int>         <!-- 7 documents have the term      -->
  <double name="tf-idf">0.14285714285714285</double> <-- 1/7         -->
</lst>