elasticsearch - 在Elastic的_all字段中搜索并返回突出显示的结果

标签 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构造的。对于架构中所有类型为string的字段,我的架构映射均将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"
      ]
    }

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

最佳答案

通过遵循this链接中提供的答案,我可以使用荧光笔。

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

关于elasticsearch - 在Elastic的_all字段中搜索并返回突出显示的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45158433/

相关文章:

elasticsearch - Elasticsearch :hour_minute_second映射返回空数据

search - Elasticsearch :URI搜索不准确

mysql - 有没有比为每个排列创建索引更好的索引多列的方法?

java抽象方法错误

elasticsearch - 如何尽可能隔离 Elasticsearch 索引和搜索路径

elasticsearch - 使用logstash的csv文件输入处理在2/3天后停止工作

elasticsearch - ElasticSearch:删除分片

mysql - 使用不使用索引的查询变量进行选择

arrays - 将数组与矩阵中的每一行进行比较(列数可能不同)

java - 为多个字段构建 Lucene 查询