1

特定の日付範囲で使用可能なオブジェクトのみを返すクエリを作成するためにElasticSearchを使用する方法があるかどうか疑問に思っていましたか?データを構造化する方法は?

必要なのは、開始日と終了日を指定してデータベースにクエリを実行し、開始日から終了日までの全期間でその期間に使用可能なすべてのオブジェクトを見つけることです。

{object-available:
{
{start:'01/01/2012', end:'03/02/2012'},
{start:'05/05/2012', end:'31/12/2012'}
}

また、2012年1月1日から2012年1月15日までの間に使用可能なオブジェクトを検索すると、このオブジェクトが返されますが、2012年3月1日から2012年4月1日までを検索しても返されません。

4

1 に答える 1

2

これは、可用性範囲をネストされたオブジェクトとして保存し、ネストされたクエリまたはフィルターを使用して、開始日と終了日の両方が目的の日付範囲に含まれることを確認することで実行できます。このチェックは、日付範囲クエリを含む2つのmust句を指定したブールクエリを使用して実行できます。例えば:

# Delete old version to make sure new settings are applied
curl -XDELETE "localhost:9200/dates-test/"
echo
# Create a new index with proper mapping 
# See http://www.elasticsearch.org/guide/reference/index-modules/analysis/pathhierarchy-tokenizer.html
curl -XPUT "localhost:9200/dates-test" -d '{
    "mappings": {
        "doc": {
            "properties": {
                "name": {"type": "string"},
                "object-available": {
                    "type": "nested",
                    "properties" : {
                        "end" : {
                            "type" : "date"
                        },
                        "start" : {
                            "type" : "date"
                        }
                    }
                }
            }
        }
    }
}'
echo
# Put some test data
curl -XPUT "localhost:9200/dates-test/doc/1" -d '{
    "name": "Record 1",
    "object-available":[
        {"start":"2012-01-01", "end":"2012-02-03"},
        {"start":"2012-05-05", "end":"2012-12-31"}
    ]
}
'
curl -XPUT "localhost:9200/dates-test/doc/2" -d '{
    "name": "Record 2",
    "object-available":[
        {"start":"2012-02-01", "end":"2012-04-20"},
        {"start":"2012-04-25", "end":"2012-11-30"}
    ]
}
'
curl -XPOST "localhost:9200/dates-test/_refresh"
echo
echo Test for the range 2011-12-01 - 2012-02-05. Should find only 1st record 
curl -XPOST "localhost:9200/dates-test/doc/_search?pretty=true" -d '{
    "query": {
        "nested": {
            "path": "object-available",
            "query": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "start": {
                                    "from": "2011-12-01",
                                    "to": "2012-02-05"
                                }
                            }
                        },
                        {
                            "range": {
                                "end": {
                                    "from": "2011-12-01",
                                    "to": "2012-02-05"
                                }
                            }
                        }

                    ]
                }
            }
        }
    }
}'
echo
echo Test for the range 2012-01-20 - 2012-12-01. Should find only 2nd record 
curl -XPOST "localhost:9200/dates-test/doc/_search?pretty=true" -d '{
    "query": {
        "nested": {
            "path": "object-available",
            "query": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "start": {
                                    "from": "2012-01-20",
                                    "to": "2012-12-01"
                                }
                            }
                        },
                        {
                            "range": {
                                "end": {
                                    "from": "2012-01-20",
                                    "to": "2012-12-01"
                                }
                            }
                        }

                    ]
                }
            }
        }
    }
}'
echo
echo Test for the range 2012-04-01 - 2013-01-01. Should find both record 
curl -XPOST "localhost:9200/dates-test/doc/_search?pretty=true" -d '{
    "query": {
        "nested": {
            "path": "object-available",
            "query": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "start": {
                                    "from": "2012-04-01",
                                    "to": "2013-01-01"
                                }
                            }
                        },
                        {
                            "range": {
                                "end": {
                                    "from": "2012-04-01",
                                    "to": "2013-01-01"
                                }
                            }
                        }

                    ]
                }
            }
        }
    }
}'
于 2012-10-05T04:12:54.660 に答える