2017-07-18 32 views
0

我正在使用Elastic 5.4並希望跨索引查詢包含多種類型的文檔(類型a和類型b)。以下是在索引的示例文件:在Elastic中的_all字段中搜索並返回高亮顯示的結果

文件:

{ 
    "_index": "test", 
    "_type": "a", 
    "_id": "1", 
    "_source": { 
    "id": "1", 
    "name": "john-usa-soccer", 
    "class": "5", 
    "lastseen": "2017-07-05", 
    "a_atts": { 
     "lastname": "tover", 
     "hobby": "soccer", 
     "country": "usa" 
    } 
    } 
} 

{ 
    "_index": "test", 
    "_type": "b", 
    "_id": "2", 
    "_source": { 
    "id": "2", 
    "name": "john-usa", 
    "class": "5", 
    "lastseen": "2017-07-05", 
    "b_atts": { 
     "lastname": "kaml", 
     "hobby": "baseball", 
     "country": "usa" 
    } 
    } 
} 

映射:

{ 
    "settings": { 
    "analysis": { 
     "analyzer": { 
     "my_ngram_analyzer": { 
      "tokenizer": "my_ngram_tokenizer" 
     } 
     }, 
     "tokenizer": { 
     "my_ngram_tokenizer": { 
      "type": "ngram", 
      "min_gram": "3", 
      "max_gram": "3", 
      "token_chars": [ 
      "letter", 
      "digit" 
      ] 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "a": { 
     "dynamic_templates": [ 
     { 
      "strings": { 
      "match": "*", 
      "match_mapping_type": "string", 
      "mapping": { 
       "type": "text", 
       "analyzer": "my_ngram_analyzer", 
       "fields": { 
       "keyword": { 
        "type": "keyword", 
        "ignore_above": 256 
       }, 
       "suggest": { 
        "type": "completion", 
        "analyzer": "simple" 
       }, 
       "analyzer1": { 
        "type": "text", 
        "analyzer": "simple" 
       }, 
       "analyzer2": { 
        "type": "text", 
        "analyzer": "standard" 
       } 
       } 
      } 
      } 
     } 
     ] 
    }, 
    "b": { 
     "dynamic_templates": [ 
     { 
      "strings": { 
      "match": "*", 
      "match_mapping_type": "string", 
      "mapping": { 
       "type": "text", 
       "analyzer": "my_ngram_analyzer", 
       "fields": { 
       "keyword": { 
        "type": "keyword", 
        "ignore_above": 256 
       }, 
       "suggest": { 
        "type": "completion", 
        "analyzer": "simple" 
       }, 
       "analyzer1": { 
        "type": "text", 
        "analyzer": "simple" 
       }, 
       "analyzer2": { 
        "type": "text", 
        "analyzer": "standard" 
       } 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

我的查詢搜索包含在任何的 '約翰' 的所有文件任何類型的字段並突出顯示找到匹配的字段。該查詢按照Elastic documentation構建。我的模式映射將ngram_analyzer配置爲分析器,而不是默認分析器,以查找模式中字符串類型的所有字段。

查詢:http://localhost:9200/student/_search

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { "match": { "_all": "john"} } 
     ] 
    } 
    }, 
    "highlight": { 
    "fields": { 
     "name": { 
     "require_field_match": false 
     }, 
     "a_atts.lastname":{ 
     "require_field_match": false 
     }, 
     "a_atts.hobby":{ 
     "require_field_match": false 
     }, 
     "a_atts.country":{ 
     "require_field_match": false 
     } 
    } 
    } 
} 

響應:

{ 
    "took": 79, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 2, 
    "max_score": 0.17669111, 
    "hits": [ 
     { 
     "_index": "student", 
     "_type": "a", 
     "_id": "AV1WjBeYEZrDBYsdGMtY", 
     "_score": 0.17669111, 
     "_source": { 
      "name": "john-usa-soccer", 
      "class": "5", 
      "lastseen": "2017-07-05", 
      "a_atts": { 
      "lastname": "tover", 
      "hobby": "soccer", 
      "country": "usa" 
      } 
     } 
     }, 
     { 
     "_index": "student", 
     "_type": "b", 
     "_id": "AV1WjHFxEZrDBYsdGMtZ", 
     "_score": 0.17669111, 
     "_source": { 
      "name": "john-usa", 
      "class": "5", 
      "lastseen": "2017-07-05", 
      "b_atts": { 
      "lastname": "kaml", 
      "hobby": "baseball", 
      "country": "usa" 
      } 
     } 
     } 
    ] 
    } 
} 

但是,在執行上面的查詢對索引,返回與他們_source內容匹配的文件,但不突出領域。它缺少以下:

"highlight": { 
     "name": [ 
     "<em>john</em>-usa-soccer" 
     ] 
    } 

我怎樣才能在結果中返回的亮點?

+0

你可以展示你的查詢和模式映射你的反應 – user3775217

+0

@ user3775217我已經更新了映射和查詢響應查詢 – FindingTheOne

回答

0

我按照this鏈接提供的答案獲得了熒光筆工作。

"highlight": { 
    "fields": { 
     "*": {} 
    }, 
    "require_field_match": false 
} 
相關問題