Couchbase全文搜索(FTS)-如何返回存储桶数据?

时间:2019-06-06 21:00:20

标签: full-text-search couchbase n1ql

我有一个带有大约200,000个键的存储桶,正在使用全文搜索进行查询。数据返回的结果准确,但是我需要一种方法来返回结果的存储桶值数据。

我尝试使用继承的类型映射为要提取的对象建立索引,但fields: ["*"]类型映射之外的default似乎没有返回任何内容。文档使Type Mapping看起来应该可行,但我似乎缺少了一些东西。我唯一想到的解决方案是存储结果ID,并使用USE KEYS [""]参数对SQL查询运行它们。

我正在运行Couchbase 5.1。

桶对象

{
    "myData": {
        "foo": "bar"
    },
    "otherData": {
        "foo": "bar"
    }
}

响应正文

{
    "status": {
        "total": 6,
        "failed": 0,
        "successful": 6
    },
    "request": {
        "query": {
            "query": "ammonia"
        },
        "size": 3,
        "from": 0,
        "highlight": null,
        "fields": [
            "*"
        ],
        "facets": null,
        "explain": false,
        "sort": [
            "-_score"
        ],
        "includeLocations": false
    },
    "hits": [
        {
            "index": "x_lookup_4a3ce884b7959a52_aa574717",
            "id": "49648042171",
            "score": 2.3192631344475236,
            "sort": [
                "_score"
            ]
        },
        {
            "index": "x_lookup_4a3ce884b7959a52_aa574717",
            "id": "49648042174",
            "score": 2.3192631344475236,
            "sort": [
                "_score"
            ]
        },
        {
            "index": "x_lookup_4a3ce884b7959a52_aa574717",
            "id": "52735091636",
            "score": 2.2918152674612653,
            "sort": [
                "_score"
            ]
        }
    ],
    "total_hits": 256,
    "max_score": 2.3192631344475236,
    "took": 699827,
    "facets": {}
}

1 个答案:

答案 0 :(得分:1)

要返回存储桶数据作为搜索结果的一部分,需要存储索引内容。处理完之后,您就可以使用字段:[“ *”] 来获取匹配中索引存储的内容。

如果您正在使用自定义类型映射,并且在其中索引了选定的子字段,则可以为要作为结果发出的每个子字段设置“ store”:true

"types": {
    "medicine": {
        "dynamic": true,
        "enabled": true,
        "properties": {
            "content": {
                "enabled": true,
                "dynamic": false,
                "fields": [{
                    "name": "content",
                    "type": "text",
                    "store": true,
                    "index": true,
                    "include_term_vectors": true,
                    "include_in_all": true,
                    "docvalues": true
                }]
            }
        }
    }
}

如果仅使用默认的动态映射,则需要设置“ store_dynamic”:true ,这是一个简单的焦烧索引定义..

{
    "name": "sample",
    "type": "fulltext-index",
    "params": {
        "doc_config": {
            "docid_prefix_delim": "",
            "docid_regexp": "",
            "mode": "type_field",
            "type_field": "type"
        },
        "mapping": {
            "default_analyzer": "standard",
            "default_datetime_parser": "dateTimeOptional",
            "default_field": "_all",
            "default_mapping": {
                "dynamic": true,
                "enabled": true
            },
            "default_type": "_default",
            "docvalues_dynamic": true,
            "index_dynamic": true,
            "store_dynamic": true,
            "type_field": "_type"
        },
        "store": {
            "indexType": "scorch",
            "kvStoreName": ""
        }
    },
    "sourceType": "couchbase",
    "sourceName": "bucket_name",
    "sourceUUID": "",
    "sourceParams": {},
    "planParams": {
        "maxPartitionsPerPIndex": 171
    },
    "uuid": ""
}

如果要从沙发基础UI创建索引,则会在“高级”部分中找到“ store_dynamic”选项。

现在,您可以尝试使用上述两种方法中定义的索引来进行精确查询。

请注意,存储内容会增加索引的磁盘占用量。