lucene - Elasticsearch-搜索 Assets 可用性

标签 lucene elasticsearch

我想知道是否有一种方法可以使用 flex 搜索进行查询,该查询仅返回在特定日期范围内可用的对象?数据的结构如何?

我需要的是查询一个提供开始日期和结束日期的数据库,并查找在开始日期和结束日期之间的整个持续时间内该时段内可用的所有对象?

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

并且搜索在01/01/2012-15/01/2012之间可用的对象应返回该对象,
但是搜索01/03/2012-01/04/2012不应返回。

最佳答案

可以通过将可用性范围存储为Nested Objects,然后使用Nested QueryFilter来检查开始日期和结束日期是否都落在所需的日期范围内来完成此操作。您可以使用Bool Query和两个包含Date Range Queries的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"
                                }
                            }
                        }

                    ]
                }
            }
        }
    }
}'

关于lucene - Elasticsearch-搜索 Assets 可用性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12728221/

相关文章:

elasticsearch - 如何使ElasticSearch拒绝不存在的 parent 的 child 索引尝试?

elasticsearch - 在 Elasticsearch 中标记时从文本中删除符号

elasticsearch - 嵌套文档上的ElasticSearch查询过滤器

java - Solr 我怎样才能先拥有原始术语而不是词干版本?

java - 在最短的时间内合并 60GB 和 10GB 的 solr 索引的最佳方法?

c# - C# 中的位置类型(solr)

java - 查询 Lucene 索引文件

lucene - 仅根据查询获取分面结果

python - 序列化Haystack SearchQuerySet

elasticsearch - Elasticsearch Reindex API部分更新