【问题标题】:elasticsearch tokenizes facet termselasticsearch 标记方面术语
【发布时间】:2013-12-03 13:54:26
【问题描述】:

我正在使用弹性搜索按类别页面上的某些条件过滤产品。 一种选择是按制造商过滤。用户可以选择制造商名称并获得过滤的结果集。我的问题是,虽然映射被定义为“not_analyzed”,但 elasticsearch 似乎对方面术语进行了标记。

请看下面的例子:

我的映射如下所示:

POST /products_test/product
{
    "mappings" : {
        "product":{
            "properties":{
                "manufacturer": {
                    "type" : "string",
                    "index" : "not_analyzed"
                }
            }
        }
    }
}

这是我的测试数据:

POST /products_test/product/1
{
    "id": 1,
    "manufacturer": "bra"
}

POST /products_test/product/2
{
    "id": 2,
    "manufacturer": "abracada bra"
}

如果我执行以下查询,我会得到一个命中,但该方面有两个术语:

POST /products_test/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
          "term": {
             "manufacturer": "abracada"
          }
         }
      }
   },
   "facets": {
      "f_manufacturer": {
         "terms": {
            "field": "manufacturer",
            "size": 30,
            "order": "term",
            "all_terms": false
         }
      }
   }
}

结果:

   "facets": {
      "f_manufacturer": {
         "_type": "terms",
         "missing": 0,
         "total": 2,
         "other": 0,
         "terms": [
            {
               "term": "abracada",
               "count": 1
            },
            {
               "term": "bra",
               "count": 1
            }
         ]
      }
   }

我的期望是获得一个以“abracada bra”为价值的单面术语。

【问题讨论】:

  • 那里似乎没有问题。但是,我建议您检查两次映射。你可能会错过一些东西。

标签: elasticsearch faceted-search facet


【解决方案1】:

您的映射和您期望的行为是正确的。

这里很可能还有其他问题,所以您应该检查您是否真的有假设的映射。

例如,您的术语过滤器 ({"term": {"manufacturer": "abracada"}}) 匹配“abracada bra”。

这是一个你可以玩的可运行示例:https://www.found.no/play/gist/cf4e908967e6512bc3b2

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Create indexes

curl -XPUT "$ELASTICSEARCH_ENDPOINT/products_test" -d '{
    "settings": {},
    "mappings": {
        "product": {
            "properties": {
                "manufacturer": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}'


# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"products_test","_type":"product","_id":1}}
{"manufacturer":"bra"}
{"index":{"_index":"products_test","_type":"product","_id":2}}
{"manufacturer":"abracada bra"}
{"index":{"_index":"products_test","_type":"product","_id":3}}
{"manufacturer":"abracada"}
'

# Do searches

# Note: The filter is for "abracada bra" and not "abracada"
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "filtered": {
            "filter": {
                "term": {
                    "manufacturer": "abracada bra"
                }
            }
        }
    },
    "facets": {
        "f_manufacturer": {
            "terms": {
                "all_terms": false,
                "field": "manufacturer",
                "order": "term",
                "size": 30
            }
        }
    }
}
'

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "filtered": {
            "filter": {
                "term": {
                    "manufacturer": "abracada"
                }
            }
        }
    },
    "facets": {
        "f_manufacturer": {
            "terms": {
                "all_terms": false,
                "field": "manufacturer",
                "order": "term",
                "size": 30
            }
        }
    }
}
'

【讨论】:

  • 你是对的。我在为文档创建映射时犯了一个错误,并且未设置“not_analyzed”部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 2020-05-28
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多