如何使用NGram过滤器为自动完成功能获取多值字段的Elasticsearch术语聚合?

问题描述 投票:0回答:1

我正在做我的自动完成项目,这是Elasticsearch的新功能。我已经使用Edge NGram过滤器进行自动补全。我试图获得自动完成的唯一结果,所以我对所有字段都使用了术语汇总。对于具有1个值的字段,我得到了很好的结果,但是对于具有多个值的字段,我得到了很好的结果..如果查询与该字段中的至少一个值匹配..它为我提供了该字段的所有值(是否查询是否与其他值匹配)。

我在服装索引下的设置和映射是:

PUT /garments
{
  "settings" : 
  {
    "number_of_replicas": 3,
    "number_of_shards": 2,
    "analysis": 
    {
      "analyzer": 
      {
        "autocomplete": 
        {
          "tokenizer": "autocomplete",
          "filter": 
          [
            "lowercase"
          ]
        },
        "autocomplete_search": 
        {
          "tokenizer": "lowercase"
        }
      },
      "tokenizer": 
      {
        "autocomplete": 
        {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 10,
          "token_chars": 
          [
            "letter"
          ]
        }
      }

    }
  },
  "mappings":
  {
    "properties":
    {
      "color": 
      {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "autocomplete_search",
        "fields": 
        {
          "keyword": 
          { 
            "type": "keyword"
          }
        }

      }
........
........
........
   }
}

(请注意,我正在使用文本类型)假设我在文档中有一个颜色字段,该字段具有多个值,例如:[“” blue“,” black“,” orange“,” marble“,” jet black“]我的搜索查询是:

GET /garments/_search
{
  "size": 0, 
  "query": 
  {
      "query_string": {
        "query": "bl"
      }
  },
  "aggs":
  {
    "Term_aggregation": 
    {
      "terms": 
      {
        "field": "color.keyword", 
        "size": 100
      }
    }
  }
}

这为我提供了所有输出,即:“蓝色”,“黑色”,“橙色”,“大理石”,“黑色”。但我只想将蓝色,黑色,黑色作为结果(查询为“ bl”)。后来我用过

"include": " .*bl.*" 

按照我的说法,过滤器集总成。给我蓝色,黑色,大理石,深黑色作为结果。这包括过滤器区分大小写...请帮助!

elasticsearch autocomplete kibana n-gram elasticsearch-aggregation
1个回答
0
投票

如果要对关键字字段进行不区分大小写的匹配,则可以将normalizer与小写过滤器一起使用

关键字字段的规范化属性类似于分析器除了保证分析链产生单个令牌。

{
  "settings": {
    "analysis": {
      "normalizer": {
        "lowercase_normalizer": {
          "type": "custom",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "color": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "autocomplete_search",
        "fields": {
          "keyword": {
            "type": "keyword",
            "normalizer": "lowercase_normalizer"
          }
        }
      }
    }
  }
}

“ include”:“。bl。”将起作用,即使实际值具有大写字母也是如此

编辑1

根据您的评论,如果您不想在术语中使用包含。您需要使用nested type索引颜色,以便将每种颜色都视为单独的对象

映射:

PUT index64
{
  "settings": {
    "number_of_replicas": 3,
    "number_of_shards": 2,
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "autocomplete",
          "filter": [
            "lowercase"
          ]
        },
        "autocomplete_search": {
          "tokenizer": "lowercase"
        }
      },
      "tokenizer": {
        "autocomplete": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 10,
          "token_chars": [
            "letter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "color": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text",
            "analyzer": "autocomplete",
            "search_analyzer": "autocomplete_search",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
}

查询:

POST index64/_doc
{
  "color": [
    {
      "name": "blue"
    },
    {
      "name": "black"
    },
    {
      "name": "orange"
    },
    {
      "name": "marble"
    },
    {
      "name": "jet black"
    }
  ]
}

结果:

GET index64/_search
{
  "size": 0, 
  "aggs": {
    "color": {
      "nested": {
        "path": "color"
      },
      "aggs": {
        "select_color": {
          "filter": {
            "match":{
              "color.name":"bl"
            }
          },
          "aggs": {
            "distinct_colors": {
              "terms": {
                "field": "color.name.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

结果

"aggregations" : {
    "color" : {
      "doc_count" : 5,
      "select_color" : {
        "doc_count" : 3,
        "distinct_colors" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "black",
              "doc_count" : 1
            },
            {
              "key" : "blue",
              "doc_count" : 1
            },
            {
              "key" : "jet black",
              "doc_count" : 1
            }
          ]
        }
      }
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.