【问题标题】:ElasticSearch Highlighting fails on match queries against index with Ngram Analyzer使用 Ngram Analyzer 对索引进行匹配查询时,ElasticSearch 突出显示失败
【发布时间】:2017-12-28 20:08:03
【问题描述】:

我创建了一个索引,并在索引中的所有字段上设置了 ngram 分析器并自定义了 _all。在索引了几个文档之后,我试图查询索引以获得类似功能的建议。

查询的输出确实返回了结果,但没有突出显示。

分析仪设置:

"analysis": {
  "analyzer": {
    "my_edgegram_analyzer": {
      "filter": [
        "lowercase"
      ],
      "tokenizer": "my_edge_tokenizer"
    }
  },
  "tokenizer": {
    "my_edge_tokenizer": {
      "token_chars": [
        "letter",
        "digit",
        "punctuation",
        "symbol"
      ],
      "min_gram": "3",
      "type": "ngram",
      "max_gram": "26"
    }
  }
}

映射:

{
  "st1": {
    "mappings": {
      "a": {
        "_all": {
          "enabled": false
        },
        "dynamic_templates": [
          {
            "catch_all": {
              "match": "imp*",
              "match_mapping_type": "string",
              "mapping": {
                "analyzer": "my_edgegram_analyzer",
                "copy_to": "catch_all",
                "norms": false,
                "type": "text"
              }
            }
          }
        ],
        "properties": {
          "catch_all": {
            "type": "text",
            "store": true,
            "analyzer": "my_edgegram_analyzer"
          },
          "imp_server_id": {
            "type": "text",
            "norms": false,
            "copy_to": [
              "catch_all"
            ],
            "analyzer": "my_edgegram_analyzer"
          },
          "imp_server_name": {
            "type": "text",
            "norms": false,
            "copy_to": [
              "catch_all"
            ],
            "analyzer": "my_edgegram_analyzer"
          }
        }
      },
      "b": {
        "_all": {
          "enabled": false
        },
        "dynamic_templates": [
          {
            "catch_all": {
              "match": "imp*",
              "match_mapping_type": "string",
              "mapping": {
                "analyzer": "my_edgegram_analyzer",
                "copy_to": "catch_all",
                "norms": false,
                "type": "text"
              }
            }
          }
        ],
        "properties": {
          "catch_all": {
            "type": "text",
            "store": true,
            "analyzer": "my_edgegram_analyzer"
          },
          "imp_server_id": {
            "type": "text",
            "norms": false,
            "copy_to": [
              "catch_all"
            ],
            "analyzer": "my_edgegram_analyzer"
          },
          "imp_server_name": {
            "type": "text",
            "norms": false,
            "copy_to": [
              "catch_all"
            ],
            "analyzer": "my_edgegram_analyzer"
          }
        }
      }
    }
  }
}

文件:

http://localhost:9200/st1/b/1

{"imp_server_name":"abc1-4-jam9.my.test.com","imp_server_id":"vrock2-us"}

http://localhost:9200/st1/a/1

{"imp_server_name":"abc2-5-ajm9.my.test.com","imp_server_id":"vrock2-us"}

查询:

{
    "query": {
        "match": {
           "catch_all": {
            "query":"test",
            "analyzer": "keyword"
           }
        }

    },
"highlight": {
        "pre_tags": ["<b>"],
        "post_tags": ["</b>"],
    "fields": {
      "*": {}
    },
    "require_field_match": false
 }
}

回应:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.16292635,
    "hits": [
      {
        "_index": "st1",
        "_type": "a",
        "_id": "1",
        "_score": 0.16292635,
        "_source": {
          "imp_server_name": "abc2-5-ajm9.my.test.com",
          "imp_server_id": "vrock2-us"
        },
        "highlight": {
          "imp_server_name": [
            "abc2-5-ajm9.my.test.com"
          ],
          "catch_all": [
            "abc2-5-ajm9.my.test.com"
          ]
        }
      },
      {
        "_index": "st1",
        "_type": "b",
        "_id": "1",
        "_score": 0.16292635,
        "_source": {
          "imp_server_name": "abc1-4-jam9.my.test.com",
          "imp_server_id": "vrock2-us"
        },
        "highlight": {
          "imp_server_name": [
            "abc1-4-jam9.my.test.com"
          ],
          "catch_all": [
            "abc1-4-jam9.my.test.com"
          ]
        }
      }
    ]
  }
}

如何在上述情况下使突出显示工作。以下是预期的输出:

预期输出:

"highlight": {
  "imp_server_name": [
    "abc2-5-ajm9.my.<b>test</b>.com"
  ],
  "catch_all": [
    "abc2-5-ajm9.my.<b>test</b>.com"
  ]
}

【问题讨论】:

  • 这里有人遇到过类似的问题吗?

标签: search elasticsearch lucene full-text-search n-gram


【解决方案1】:

我能够通过在架构中设置 term_vector 来获得结果。

 "term_vector": "with_positions_offsets"

【讨论】:

    猜你喜欢
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2019-05-18
    • 1970-01-01
    • 2012-11-03
    相关资源
    最近更新 更多