elasticsearch 在Elastic中的_all字段中搜索,并返回突出显示的结果,elasticsearch,indexing,lucene,full-text-search,elasticsearch,Indexing,Lucene,Full Text Search" /> elasticsearch 在Elastic中的_all字段中搜索,并返回突出显示的结果,elasticsearch,indexing,lucene,full-text-search,elasticsearch,Indexing,Lucene,Full Text Search" />

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

elasticsearch 在Elastic中的_all字段中搜索,并返回突出显示的结果,elasticsearch,indexing,lucene,full-text-search,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": { "lastnam

我使用的是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"
                }
              }
            }
          }
        }
      ]
    }
  }
}
{
  "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"
          }
        }
      }
    ]
  }
}
映射:

{
  "_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"
                }
              }
            }
          }
        }
      ]
    }
  }
}
{
  "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"
          }
        }
      }
    ]
  }
}
我的查询是在任何类型的任何字段中搜索包含“john”的所有文档,并突出显示找到匹配项的字段。此查询是根据构造的。我的模式映射将ngram_analyzer配置为模式中所有字符串类型字段的analyzer,而不是默认analyzer

查询:

响应:

{
  "_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"
                }
              }
            }
          }
        }
      ]
    }
  }
}
{
  "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"
          }
        }
      }
    ]
  }
}
但是,对索引执行上述查询会返回与其源内容匹配的文档,但不会突出显示字段。它缺少以下内容:

    "highlight": {
      "name": [
        "<em>john</em>-usa-soccer"
      ]
    }
“突出显示”:{
“姓名”:[
“约翰美国足球”
]
}

如何返回结果中的突出显示?我按照链接中提供的答案让highlighter开始工作


我让highlighter按照链接中提供的答案工作


您能否显示查询和模式的响应mappings@user3775217我已经用映射和查询响应更新了查询。您可以显示来自查询和模式的响应吗mappings@user3775217我已经用映射和查询响应更新了查询