将术语与同义词组合 - ElasticSearch

Combining terms with synonyms - ElasticSearch

我是 Elasticsearch 的新手,有一个同义词分析器,看起来像-

{
    "settings": {  
        "index": {  
            "analysis": {  
                "filter": {  
                    "graph_synonyms": {  
                        "type": "synonym_graph",
                        "synonyms": [ 
                            "gowns, dresses",
                            "backpacks, bags", 
                            "coats, jackets"
                        ] 
                    }
                },
                "analyzer": {  
                    "search_time_analyzer": { 
                        "tokenizer": "standard", 
                        "filter": [ 
                            "lowercase",
                            "graph_synonyms" 
                        ] 
                    } 
                }
            }
        }
    }
}

映射看起来像-

{
    "properties": {
        "category": {  
            "type": "text",
            "search_analyzer": "search_time_analyzer",
            "fields": {
                "no_synonyms": {
                    "type": "text"
                }
            }
          }
    }
}

如果我搜索礼服,它会为我提供礼服和连衣裙的正确结果。

但问题是如果我搜索红色礼服,(系统没有任何红色礼服)预期的行为是搜索红色礼服和 return 这些结果。但相反,它 return 是礼服和连衣裙的结果,与颜色无关。

我想配置系统,使其考虑术语及其各自的同义词(如果有),然后 return 结果。

作为参考,这是我的搜索查询的样子-

"query": 
{
    "bool": 
    {
        should: 
        [
            {
                "multi_match":
                {
                    "boost": 300,
                    "query": term,
                    "type": "cross_fields",
                    "operator": "or",
                    "fields": ["bu.keyword^10", "bu^10", "category.keyword^8", "category^8", "category.no_synonyms^8", "brand.keyword^7", "brand^7", "colors.keyword^2", "colors^2", "size.keyword", "size", "hash.keyword^2", "hash^2", "name"]
                }
            }
        ]
    }
} 

示例文档:

_source: {
  productId: '12345',
  name: 'RUFFLE FLORAL TRIM COTTON MAXI DRESS',
  brand: [ 'self-portrait' ],
  mainImage: 'http://test.jpg',
  description: 'Self-portrait presents this maxi dress, crafted from cotton, to offer your off-duty ensembles an elegant update. Trimmed with ruffled broderie details, this piece is an effortless showcase of modern femininity.',
  status: 'active',
  bu: [ 'womenswear' ],
  category: [ 'dresses', 'gowns' ],
  tier1: [],
  tier2: [],
  colors: [ 'WHITE' ],
  size: [ '4', '6', '8', '10' ],
  hash: [
    'ballgown',   'cotton',
    'effortless', 'elegant',
    'floral',     'jar',
    'maxi',       'modern',
    'off-duty',   'ruffle',
    'ruffled',    '1',
    '2',          'crafted'
  ],
  styleCode: '211274856'
}

我怎样才能达到预期的输出?任何帮助,将不胜感激。谢谢

您可以像下面这样配置索引时间分析器而不是搜索时间分析器:

{
    "properties": {
        "category": {  
            "type": "text",
            "analyzer": "search_time_analyzer",
            "fields": {
                "no_synonyms": {
                    "type": "text"
                }
            }
          }
    }
}

完成索引映射更改后,重新索引数据并尝试以下查询: 请注意,我已将 operator 更改为 and 并将 analyzer 更改为 standard:

{
  "query": {
    "multi_match": {
      "boost": 300,
      "query": "gowns red",
      "analyzer": "standard", 
      "type": "cross_fields",
      "operator": "and",
      "fields": [
        "category",
        "colors"
      ]
    }
  }
}

为什么您当前的查询不起作用:

不安全: 您当前的索引映射索引数据使用 standard 分析器,因此它不会使用同义词值索引您的任何类别。

正在搜索: 您当前的查询具有运算符 or,因此如果您搜索 red gowns,那么它将创建类似 red OR gowns OR dresses 的查询,并且无论颜色如何,它都会为您提供结果。此外,如果您在现有配置中将 operator 更改为 and,那么它将 return 为零结果,因为它将创建类似 red AND gowns AND dresses.

的查询

解决方案: 按照我的建议完成更改后,它也会为 category 字段编制索引同义词,并且它将与 and 运算符一起使用。因此,如果您尝试查询 gowns red,那么它将创建类似 gowns AND red 的查询。它将匹配,因为 category 字段具有两个值 gownsdresses 由于索引时应用的​​同义词。