在Elastic中搜索_all字段并使用突出显示返回结果

时间:2017-07-18 05:45:04

标签: elasticsearch indexing lucene full-text-search

我正在使用Elastic 5.4,并希望查询包含多种类型的索引。(输入a和b)。以下是索引中的示例文档:

文件:

{
  "_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"
                }
              }
            }
          }
        }
      ]
    }
  }
}

我的查询是在任何类型的任何字段中搜索包含“john”的所有文档,并突出显示找到匹配项的字段。此查询按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"
      ]
    }

如何在结果中返回突出显示?

1 个答案:

答案 0 :(得分:1)

我按照this链接中提供的答案让荧光笔工作。

"highlight": {
    "fields": {
      "*": {}
    },
    "require_field_match": false
 }