弹性搜索中索引任意服装值对的最佳方法

Ser*_*gel 4 elasticsearch elasticsearch-indices

我正在尝试对具有属性值对的弹性搜索索引文档。示例文件:

{
    id: 1,
    name: "metamorphosis",
    author: "franz kafka"
}

{
    id: 2,
    name: "techcorp laptop model x",
    type: "computer",
    memorygb: 4
}

{
    id: 3,
    name: "ss2014 formal shoe x",
    color: "black",
    size: 42,
    price: 124.99
}
Run Code Online (Sandbox Code Playgroud)

然后,我需要以下查询:

1. "author" EQUALS "franz kafka"
2. "type" EQUALS "computer" AND "memorygb" GREATER THAN 4
3. "color" EQUALS "black" OR ("size" EQUALS 42 AND price LESS THAN 200.00)
Run Code Online (Sandbox Code Playgroud)

存储这些文档以有效查询它们的最佳方法是什么?是否应该完全按照示例所示存储它们?或者我应该像这样存储它们:

{
    fields: [
        { "type": "computer" },
        { "memorygb": 4 }
    ]
}
Run Code Online (Sandbox Code Playgroud)

或类似:

{
    fields: [
        { "key": "type", "value": "computer" },
        { "key": "memorygb", "value": 4 }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我应该如何映射索引才能执行相等查询和范围查询?

smn*_*mnh 5

如果有人还在寻找答案,我写了一篇文章,介绍如何将任意数据索引到Elasticsearch中,然后按特定的字段和值进行搜索。所有这一切,都不会破坏索引映射。

帖子:http : //smnh.me/indexing-and-searching-arbitrary-json-data-using-elasticsearch/

简而言之,您将需要创建帖子中描述的特殊索引。然后,您将需要使用flattenData功能https://gist.github.com/smnh/30f96028511e1440b7b02ea559858af4来整理数据。然后,可以将扁平化的数据安全地索引到Elasticsearch索引中。

例如:

flattenData({
    id: 1,
    name: "metamorphosis",
    author: "franz kafka"
});
Run Code Online (Sandbox Code Playgroud)

将产生:

[
    {
        "key": "id",
        "type": "long",
        "key_type": "id.long",
        "value_long": 1
    },
    {
        "key": "name",
        "type": "string",
        "key_type": "name.string",
        "value_string": "metamorphosis"
    },
    {
        "key": "author",
        "type": "string",
        "key_type": "author.string",
        "value_string": "franz kafka"
    }
]
Run Code Online (Sandbox Code Playgroud)

flattenData({
    id: 2,
    name: "techcorp laptop model x",
    type: "computer",
    memorygb: 4
});
Run Code Online (Sandbox Code Playgroud)

将产生:

[
    {
        "key": "id",
        "type": "long",
        "key_type": "id.long",
        "value_long": 2
    },
    {
        "key": "name",
        "type": "string",
        "key_type": "name.string",
        "value_string": "techcorp laptop model x"
    },
    {
        "key": "type",
        "type": "string",
        "key_type": "type.string",
        "value_string": "computer"
    },
    {
        "key": "memorygb",
        "type": "long",
        "key_type": "memorygb.long",
        "value_long": 4
    }
]
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用构建Elasticsearch查询来查询数据。每个查询应同时指定键和值的类型。如果不确定该索引具有哪些键或类型,则可以运行聚合来查找,这也将在后文中讨论。

例如,要查找author == "franz kafka"需要执行以下查询的文档:

{
    "query": {
        "nested": {
            "path": "flatData",
            "query": {
                "bool": {
                    "must": [
                        {"term": {"flatData.key": "author"}},
                        {"match": {"flatData.value_string": "franz kafka"}}
                    ]
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

查找type == "computer" and memorygb > 4需要执行以下查询的文档:

{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "flatData",
                        "query": {
                            "bool": {
                                "must": [
                                    {"term": {"flatData.key": "type"}},
                                    {"match": {"flatData.value_string": "computer"}}
                                ]
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "flatData",
                        "query": {
                            "bool": {
                                "must": [
                                    {"term": {"flatData.key": "memorygb"}},
                                    {"range": {"flatData.value_long": {"gt": 4}}}
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,由于我们希望同一文档同时满足这两个条件,因此我们在使用外部bool查询以及包含must两个nested查询的子句。


小智 0

您需要从这里过滤查询:

您必须将范围查询与匹配查询一起使用