3

我正在尝试过滤一些文本并将超链接添加到匹配项。但是在添加超链接之前,我不知道如何突出显示多个单词。这是我对这个场景的快乐路径:

  1. 像这样添加映射:
curl -X PUT "localhost:9200/my-index" -H 'Content-Type: application/json' -d'
{
    "mappings": {
        "_doc": {
            "properties": {
                "message": {
                    "type": "text"
                },
                "query": {
                    "type": "percolator"
                }
            }
        }
    }
}
'
  1. 在过滤器中注册查询
curl -X PUT "localhost:9200/my-index/_doc/1?refresh" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "message" : "the bonsai three in a jungle"
        }
    }
}
'
  1. 用我的渗透查询突出显示的新文档
curl -X GET "localhost:9200/my-index/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "percolate" : {
            "field": "query",
            "document" : {
                "title" : "A new bonsai tree in the office and jungle" 
            }
        }
    },
    "highlight": {
      "fields": {
        "title": {}
      }
    }
}
'

输出:

{  
   "took":10,
   "timed_out":false,
   "_shards":{  
      "total":#,
      "successful":#,
      "skipped":0,
      "failed":0
   },
   "hits":{  
      "total":#,
      "max_score":1.7260926,
      "hits":[  
         {  
            "_index":"my-index",
            "_type":"_doc",
            "_id":"1",
            "_score":1.7260926,
            "_source":{  
               "query":{  
                  "match":{  
                     "title":"the bonsai tree in a jungle"
                  }
               }
            },
            "fields":{  
               "_percolator_document_slot":[  
                  0
               ]
            },
            "highlight":{  
               "title":[  
                  "<em>A</em> new <em>bonsai</em> <em>tree</em> <em>in</em> <em>the</em> office and <em>jungle</em>"
               ]
            }
         }
      ]
   }
}

如您所见,从字面上看,丛林中的盆景三不等于办公室和丛林中的新盆景树。但它匹配了我的渗透查询中的每个单词。为什么会这样?

  1. 然后我添加新的渗透查询
curl -X PUT "localhost:9200/my-index/_doc/2?refresh" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {    
            "title" : "the enojen tree in a kerojen"
        }                 
    }                                                                  
}            
' 

和新的搜索:

curl -X GET "localhost:9200/my-index/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "percolate" : {
            "field": "query",
            "document" : {
                "title" : "A new bonsai tree in the office and jungle. The enojen tree in a kerojen."
            }
        }
    },
    "highlight": {
      "fields": {
        "title": {}
      }
    }
}
'

输出:

{  
   "took":12,
   "timed_out":false,
   "_shards":{  
      "total":5,
      "successful":5,
      "skipped":0,
      "failed":0
   },
   "hits":{  
      "total":45,
      "max_score":2.1576157,
      "hits":[  
         {  
            "_index":"my-index",
            "_type":"_doc",
            "_id":"2",
            "_score":2.1576157,
            "_source":{  
               "query":{  
                  "match":{  
                     "title":"the enojen tree in a kerojen"
                  }
               }
            },
            "fields":{  
               "_percolator_document_slot":[  
                  0
               ]
            },
            "highlight":{  
               "title":[  
                  "<em>A</em> new bonsai <em>tree</em> <em>in</em> <em>the</em> office and jungle. <em>The</em> <em>enojen</em> <em>tree</em> <em>in</em> <em>a</em> <em>kerojen</em>."
               ]
            }
         },
         {  
            "_index":"my-index",
            "_type":"_doc",
            "_id":"1",
            "_score":2.1576157,
            "_source":{  
               "query":{  
                  "match":{  
                     "title":"the bonsai tree in a jungle"
                  }
               }
            },
            "fields":{  
               "_percolator_document_slot":[  
                  0
               ]
            },
            "highlight":{  
               "title":[  
                  "<em>A</em> new <em>bonsai</em> <em>tree</em> <em>in</em> <em>the</em> office and <em>jungle</em>. <em>The</em> enojen <em>tree</em> <em>in</em> <em>a</em> kerojen."
               ]
            }
         }
      ]
   }
}

正如您看到的两个不同的匹配,但我想在搜索中结合我的两个渗透查询。不可能吗?

  1. 这一步还不能达到)用 一些代码改变pre-tag和改变。post-tag <em></em><a href="$var"></a>$var

最后,

  1. 所有文档都将显示在带有超链接的 html 页面中。(希望:))

我想做的事有可能吗?如果没有,你能解释一下哪一部分不能做吗?如果可能的话,你能给我一些建议如何去做以及如何改进吗?

4

0 回答 0