只有基本搜索才有效

时间:2016-12-23 13:52:46

标签: elasticsearch

我正在玩curl通过我的mac控制台查询elasticsearch db。但是我在执行更复杂的搜索方面遇到了麻烦。到目前为止,我可以像这样查询match_all:

curl -XGET 'localhost:9200/products/fashion/_search?pretty' -d'
{
   "query" : { "match_all" : {} }
}'

我收到以下数据:

{
    "took" : 7,
    "timed_out" : false,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
    },
    "hits" : {
        "total" : 915503,
        "max_score" : 1.0,
        "hits" : [
        {
            "_index" : "products",
            "_type" : "fashion",
            "_id" : "57d49ee494efcfdfe0f3abfe",
            "_score" : 1.0,
            "_source" : {
            "doc" : {
                "id" : "57d49ee494efcfdfe0f3abfe",
                "name" : "Shorts",
                "price" : {
                    "value" : 35
                }
            }
        }
    },
    ...........
}

我要求这样的映射没有问题:

curl -XGET 'localhost:9200/products/_mapping/fashion?pretty'

价格的结果是:

.......
"price" : {
    "properties" : {
        "currency" : {
            "type" : "text",
            "fields" : {
                "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                }
            }
        },
        "value" : {
            "type" : "long"
        }
    }
},
....

但我在“price.value”上使用过滤器进行查询的所有尝试都未点击。

curl -XGET 'localhost:9200/products/fashion/_search?pretty' -d'
{
    "query" : {
        "constant_score" : { 
            "filter" : {
                "term" : { 
                    "price.value" : 35
                }
            }
        }
    }
}'
{
    "took" : 26,
    "timed_out" : false,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
    },
    "hits" : {
        "total" : 0,
        "max_score" : null,
        "hits" : [ ]
    }
}

This query I took from elasticsearch guide

我没有想法和示例如何编写此查询以返回我在数据库中显然拥有的内容。您可能已经注意到我至少有一个price.value = 35

的文档

1 个答案:

答案 0 :(得分:2)

这是因为您的price字段位于另一个名为doc的字段中,因此您需要像这样查询doc.price.value

curl -XPOST 'localhost:9200/products/fashion/_search?pretty' -d'
{
    "query" : {
        "constant_score" : { 
            "filter" : {
                "term" : { 
                    "doc.price.value" : 35
                }
            }
        }
    }
}'