如何使弹性搜索映射找到复数和单数?

时间:2014-07-14 16:32:36

标签: elasticsearch stemming

我使用的是elasticsearch版本1.2.1

该属性的存储值为shoes,该字段的分析器是滚雪球,尽管在我搜索shoes时ES没有找到它。当我搜索shoe时,它会找到文档......

这是我的疑问:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "or": [
          {
            "term": {
              "category": "shoes"
            }
          },
          {
            "term": {
              "sub_category1": "shoes"
            }
          },
          {
            "term": {
              "sub_category2": "shoes"
            }
          },
          {
            "term": {
              "brand": "shoes"
            }
          },
          {
            "term": {
              "shop": "shoes"
            }
          }
        ]
      }
    }
  },
  "aggregations": {
    "category": {
      "terms": {
        "field": "category"
      }
    },
    "sub_category1": {
      "terms": {
        "field": "sub_category1"
      },
      "aggregations": {
        "discount": {
          "avg": {
            "field": "discount_percentage"
          }
        }
      }
    }
  }
}

这是我的映射:

"mappings": {
      "item": {
        "properties": {
          "brand": {
            "type": "string",
            "analyzer": "snowball"
          },
          "category": {
            "type": "string",
            "analyzer": "snowball"
          },
          "color": {
            "type": "string"
          },
          "created_at": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "discount_percentage": {
            "type": "long"
          },
          "domain_name": {
            "type": "string"
          },
          "id": {
            "type": "long"
          },
          "image": {
            "type": "string"
          },
          "item_name": {
            "type": "string"
          },
          "link": {
            "type": "string"
          },
          "need_indexing": {
            "type": "boolean"
          },
          "price": {
            "type": "string"
          },
          "price_range": {
            "type": "string"
          },
          "product_key": {
            "type": "string"
          },
          "raw_size": {
            "type": "string"
          },
          "regular_price": {
            "type": "string"
          },
          "sale_price": {
            "type": "string"
          },
          "scrape_run": {
            "type": "string"
          },
          "shop": {
            "type": "string",
            "analyzer": "snowball"
          },
          "size": {
            "type": "string"
          },
          "source_url": {
            "type": "string"
          },
          "sub_category1": {
            "type": "string",
            "analyzer": "snowball"
          },
          "sub_category2": {
            "type": "string",
            "analyzer": "snowball"
          },
          "updated_at": {
            "type": "date",
            "format": "dateOptionalTime"
          }
        }
      }
    }
  }

1 个答案:

答案 0 :(得分:0)

问题是你使用Snowball进行索引,将“鞋子”限制为“鞋子”,然后使用术语过滤器运行match_all查询,查找未分析的术语:

  

期限过滤

     

过滤包含字词(未分析)的字段的文档。   与术语查询类似,不同之处在于它充当过滤器。可以放置   在接受过滤器的查询中

这就是“鞋子”匹配的原因 - 您正在搜索索引中的原始术语。

一般来说,当您设置复杂的索引和查询时间分析时,您希望使某些事情匹配 - 所以如果您正在进行中(例如使用Snowball),您希望确保在搜索时出现问题

根据您的情况,我尝试使用查询过滤器而不是术语过滤器:

  

查询过滤器

     

包装任何用作过滤器的查询。可以放在查询中   接受过滤器。

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-filter.html