【问题标题】:different analyzers in different fields不同领域的不同分析仪
【发布时间】:2020-06-25 11:55:45
【问题描述】:

因为在下面的映射中,当我在三个字段中同时进行搜索时,当我将 URL 字段与标题和描述字段不同的分析器放置时,即使我有这三个字段之一,它也不会返回任何内容每个字段下面的单词

 {
      "settings": {
        "index": {
          "number_of_shards": "5",
          "number_of_replicas": "0",
          "analysis": {
            "filter": {
              "stemmer_plural_portugues": {
                "name": "minimal_portuguese",
                "stopwords" : ["http", "https", "ftp", "www"],
                "type": "stemmer"
              },
              
              
                "synonym_filter": {
                "type": "synonym",
                "lenient": true,
                "synonyms_path": "analysis/synonym.txt",
                "updateable" : true
    
              },
              
           
              "shingle_filter": {
                "type": "shingle",
                "min_shingle_size": 2,
                "max_shingle_size": 3
              }
    
            },
            
            "analyzer": {
              "analyzer_customizado": {
                "filter": [
                  "lowercase",
                  "stemmer_plural_portugues",
                  "asciifolding",
                  "synonym_filter",
                  "shingle_filter" ],
                "tokenizer": "standard"
              },
              
              
               "analyzer_url": {
                "filter": [
                  "lowercase",
                  "stemmer_plural_portugues",
                  "asciifolding" ],
                "tokenizer": "lowercase"
              }
              
              
            }
    
          }
        }
      },
      "mappings": {
          "properties": {
    
            "id": {
             "type": "long"
            },
             "data": {
              "type": "date"
            },
             "quebrado": {
              "type": "byte"
              
            },
             "pgrk": {
               "type":  "integer" 
            },
             "url_length": {
               "type":  "integer" 
            },
            "title": {
              "analyzer": "analyzer_customizado",
              "type": "text",
              "fields": {
                "keyword": {
                  "ignore_above": 256,
                  "type": "keyword"
                }
              }
            },
            "description": {
            "analyzer": "analyzer_customizado",
              "type": "text",
              "fields": {
                "keyword": {
                  "ignore_above": 256,
                  "type": "keyword"
                }
              }
            },
            "url": {
              "analyzer": "analyzer_url",
              "type": "text",
              "fields": {
                "keyword": {
                  "ignore_above": 256,
                  "type": "keyword"
                }
              }
            }
          }
        }
      }

在下面的查询中,每个字段都存在三个单词,但它仅在我搜索标题和描述中的单词时返回结果,如果我还搜索 URL 字段中的单词有不同的分析器不返回任何东西。

如果我只搜索标题和描述字段中的单词,您通常会找到,如果我只搜索 URL 字段中的单词也会找到它,但是如果我搜索存在于三个字段它不返回任何内容。

{
    "from": 0,
    "size": 10,
    
    "query": {
      
            
                "multi_match": {
                    "query": "carro moto aviao",
                     "type": "cross_fields",
                    "fields": [
                        "title",
                        "description",
                        "url"
                    ],
                    "operator": "and"
              }

    }
}

【问题讨论】:

  • 您还可以添加示例文档以便我们在您的数据上进行测试吗?

标签: elasticsearch


【解决方案1】:

问题是您将operator 用作and,这意味着carro moto aviao 必须出现所有三个单词,您可以将其更改为OR 并查看它是否返回结果。

添加一个带有映射、示例数据和带有or 参数的搜索查询的工作示例,并确认它有效。

示例文档

{
    "title": "carro",
    "description": "moto",
    "url": "aviao"
}

带有OR参数的搜索查询

{
    "from": 0,
    "size": 10,
    "query": {
        "multi_match": {
            "query": "carro moto aviao",
            "type": "cross_fields",
            "fields": [
                "title",
                "description",
                "url"
            ],
            "operator": "or"
        }
    }
}

搜索结果

 "hits": [
            {
                "_index": "jean",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.5753642,
                "_source": {
                    "title": "carro",
                    "description": "moto",
                    "url": "aviao"
                }
            }
        ]

注意:如果您的查询确认它不适用于and 参数。

【讨论】:

  • 我必须返回搜索了所有单词的文档,所以我使用 AND。由于这三个搜索词存在于记录中,所以应该返回。但是,由于 URL 字段具有不同的分析器,因此它不会返回,如果这三个字段具有相同的分析器,则它可以工作。
  • @Jean 您能否添加示例文档和预期文档以便我可以进一步提供帮助,顺便说一句,我使用了您的索引映射,您有机会尝试我的示例吗?
  • 我添加了示例
【解决方案2】:

@opster

插入文档

{
    "title": "carro",
    "description": "moto",
    "url": "aviao"
}

查询文档的三个字段中存在的三个单词,但它不返回任何内容。

{
    "from": 0,
    "size": 10,
    
    "query": {
      
            
                "multi_match": {
                    "query": "carro moto aviao",
                    "type": "cross_fields",
                    "fields": [
                        "title",
                        "description",
                        "url"
                    ],
                    "operator": "and"
              }

    }
}

如果我只搜索标题字段和描述(carro moto)中的单词,则文档返回。

如果我只搜索 URL 字段 (aviao) 中的单词会返回文档,但是如果搜索三个单词不会返回文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多