2015-04-01 54 views
4

你能帮我解决关于特定语言分析器的一些小问题吗?分析器是否阻止字段突出显示?

我需要通过查询字符串搜索文档并突出显示匹配的字符串。 这里是我的映射:

{ 
    "usr": { 
     "properties": { 
      "text0": { 
       "type": "string", 
       "analyzer": "english" 
      }, 
      "text1": { 
       "type": "string" 
      } 
     } 
    } 
} 

注意,对于“text0”中域“英语”分析仪设置,并为“text1”中的字段默认使用标准的分析。

在我的指数有现在一个文件:

hits": [{ 
    "_index": "tt", 
    "_type": "usr", 
    "_id": "AUxvIPAv84ayQMZV-3Ll", 
    "_score": 1, 
    "_source": { 
     "text0": "highlighted. need to be highlighted.", 
     "text1": "highlighted. need to be highlighted." 
    } 
}] 

考虑以下查询:

{ 
    "query": { 
     "query_string" : { 
      "query" : "highlighted" 
     } 
    }, 
    "highlight" : { 
     "fields" : { 
      "*" : {} 
     } 
    } 
} 

我所预料的文档中的每个领域加以强调,但高亮只出现在“text1”字段(其中没有分析器集合):

"hits": [{ 
    "_type": "usr", 
    "_source": { 
     "text0": "highlighted. need to be highlighted.", 
     "text1": "highlighted. need to be highlighted." 
    }, 
    "_score": 0.19178301, 
    "_index": "tt", 
    "highlight": { 
     "text1": [ 
      "<em>highlighted</em>. need to be <em>highlighted</em>." 
     ] 
    }, 
    "_id": "AUxvIPAv84ayQMZV-3Ll" 
}] 

让我们考虑以下NG查询(我的预期“突出”,因为分析仪匹配“亮点”):

{ 
    "query": { 
     "query_string" : { 
       "query" : "highlight" 
      } 
     }, 
    "highlight" : { 
      "fields" : { 
       "*" : {} 
      } 
     } 
} 

但有响应无HIST可言:(做过英语仪甚至在这里工作?)

"hits": { 
    "hits": [], 
    "total": 0, 
    "max_score": null 
} 

最后,考虑到一些卷曲的命令(请求和响应):

curl "http://localhost:9200/tt/_analyze?field=text0" -d "highlighted" 

{"tokens":[{ 
    "token":"highlight", 
    "start_offset":0, 
    "end_offset":11, 
    "type":"<ALPHANUM>", 
    "position":1 
}]} 

curl "http://localhost:9200/tt/_analyze?field=text1" -d "highlighted" 

{"tokens":[{ 
    "token":"highlighted", 
    "start_offset":0, 
    "end_offset":11, 
    "type":"<ALPHANUM>", 
    "position":1 
}]} 

我们看到,通过传递文本通过英语和标准的分析,其结果是不同的。 最后,问题是:分析器是否防止突出显示字段?如何在全文搜索时突出显示我的字段?

P.S.我使用windows 8.1在本地机器上使用elasticsearch v1.4.4。

回答

2

它与您的查询有关。您正在使用query_string查询,并且您没有指定该字段,因此它默认在_all字段上进行搜索。 这就是为什么你看到奇怪的结果。查询更改为multi_match查询在这两个领域的搜索:

{ 
    "query": { 
     "multi_match": { 
      "fields": [ 
       "text1", 
       "text0" 
      ], 
      "query": "highlighted" 
     } 
    }, 
    "highlight": { 
     "fields": { 
      "*": {} 
     } 
    } 
} 

现在突出了这两个领域的结果将在响应中返回。