elasticsearch标记了方面术语

时间:2013-12-03 13:54:26

标签: elasticsearch faceted-search facet

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

请参阅以下示例:

我的映射如下所示:

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"
}

如果我执行以下查询,我会得到一个命中但是两个关于facet的术语:

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”作为价值来获得单个方面的术语。

1 个答案:

答案 0 :(得分:2)

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

这里最有可能出现其他问题,所以你应该检查一下你是否真的有假定的映射。

例如,您的字词过滤器({"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
            }
        }
    }
}
'