elasticsearch无法按单独的术语搜索

qacovj5a  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(134)

我有一个文档,就像下面一样。

{
  "_index" : "trigger",
  "_type" : "_doc",
  "_id" : "urn:trigger:(urn:analyticsplatform:uc4):acxiom_au_attr.done",
  "_version" : 45,
  "_seq_no" : 420508,
  "_primary_term" : 20,
  "found" : true,
  "_source" : {
    "_boostScoreFactor" : 1.0,
    "type" : "trigger",
    "title" : "acxiom_au_attr.done",
  }
}

下面是标题字段的Map。

"title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }

下面是index的设置:

{
  "trigger" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "total_shards_per_node" : "2"
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "trigger",
        "creation_date" : "1668526304683",
        "unassigned" : {
          "node_left" : {
            "delayed_timeout" : "15m"
          }
        },
        "analysis" : {
          "filter" : {
            "discovery_stemmer" : {
              "type" : "stemmer",
              "language" : "porter2"
            },
            "discovery_stemmer_possessive" : {
              "type" : "stemmer",
              "language" : "minimal_english"
            },
            "discovery_synonym_abbr" : {
              "type" : "synonym_graph",
              "synonyms" : [
                "BN => browse node"
              ]
            }
          },
          "char_filter" : {
            "discovery_charfilter" : {
              "type" : "mapping",
              "mappings" : [
                ".=>|",
                "_=>|"
              ]
            }
          },
          "normalizer" : {
            "lowercase" : {
              "filter" : [
                "lowercase"
              ],
              "type" : "custom",
              "char_filter" : [ ]
            }
          },
          "analyzer" : {
            "discovery_index_analyzer" : {
              "filter" : [
                "lowercase",
                "discovery_synonym_abbr"
              ],
              "char_filter" : [
                "discovery_charfilter"
              ],
              "type" : "custom",
              "tokenizer" : "standard"
            }
          }
        },
        "number_of_replicas" : "2",
        "uuid" : "oMzEq9L_Sum8A5ikwLyCdQ",
        "version" : {
          "created" : "135238227"
        }
      }
    }
  }
}

它使用了一个分析器,将分离的标题,它将被分离成多个术语

{
  "tokens" : [
    {
      "token" : "acxiom",
      "start_offset" : 0,
      "end_offset" : 6,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "au",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "attr",
      "start_offset" : 10,
      "end_offset" : 14,
      "type" : "<ALPHANUM>",
      "position" : 2
    }
    {
      "token" : "done",
      "start_offset" : 19,
      "end_offset" : 23,
      "type" : "<ALPHANUM>",
      "position" : 4
    }
  ]
}

但我不能用这个词来搜索。

GET trigger/_search
{
  "query": {
    "match": {
      "title": "acxiom"
    }
  }
}

如果我使用整个标题,它可以工作。

GET trigger/_search
{
  "query": {
    "match": {
      "title": "acxiom_au_attr.done"
    }
  }
}

我应该怎么做才能让它支持按词搜索?

c8ib6hqw

c8ib6hqw1#

因为我可以检查Map,没有指定分析器。默认情况下,Elasticsearch使用Standard analyzer
这意味着,如果我插入数据与默认设置(与标准分析仪),它将创建以下条款:

GET /_analyze?pretty
{
  "analyzer" : "standard",
  "text" : "acxiom_au_attr.done"
}

回复

{
  "tokens": [
    {
      "token": "acxiom_au_attr.done",
      "start_offset": 0,
      "end_offset": 19,
      "type": "<ALPHANUM>",
      "position": 0
    }
  ]
}

它只创建了一个术语,即acxiom_au_attr.done。这就是为什么它与整个值匹配,而不是与部分值匹配。
您可以使用simple analyzer,这将创建如下条款:

GET /_analyze?pretty
{
  "analyzer" : "simple",
  "text" : "acxiom_au_attr.done"
}

回复

{
  "tokens": [
    {
      "token": "acxiom",
      "start_offset": 0,
      "end_offset": 6,
      "type": "word",
      "position": 0
    },
    {
      "token": "au",
      "start_offset": 7,
      "end_offset": 9,
      "type": "word",
      "position": 1
    },
    {
      "token": "attr",
      "start_offset": 10,
      "end_offset": 14,
      "type": "word",
      "position": 2
    },
    {
      "token": "done",
      "start_offset": 15,
      "end_offset": 19,
      "type": "word",
      "position": 3
    }
  ]
}

创建Map

PUT /test-index2?pretty
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "simple"
      }
    }
  }
}

插入文件

POST test-index2/_doc
{
  "title":"acxiom_au_attr.done"
}

搜索关键词

GET test-index2/_search
{
  "query": {
    "match": {
      "title": "acxiom"
    }
  }
}

相关问题