簡體   English   中英

如何查詢彈性搜索自動完成字段,並且僅返回某些結果?

[英]How can I query an elastic search autocomplete field, and return only certain results?

給定以下映射:

{
      "tags": {
        "aliases": {},
        "mappings": {
          "tag": {
            "properties": {
              "_users": {
                "type": "string"
              },
              "child": {
                "type": "string"
              },
              "level": {
                "type": "double"
              },
              "name": {
                "type": "string",
                "fields": {
                  "suggest": {
                    "type": "completion",
                    "analyzer": "simple",
                    "payloads": true,
                    "preserve_separators": true,
                    "preserve_position_increments": true,
                    "max_input_length": 50
                  }
                }
              },
              "parent": {
                "type": "string"
              },
              "root": {
                "type": "boolean"
              }
            }
          }
        },
        "settings": {
          "index": {
            "creation_date": "1443688314495",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "version": {
              "created": "1070299"
            },
            "uuid": "iH0QRRm_QF6pXEZ5sJI4yA"
          }
        },
        "warmers": {}
      }
    }

同樣, _users是字符串數組。

如何查詢標記名稱建議,並且僅返回查詢的'id'在_users數組中的_users

這是一個示例標記:

{
  "_index": "tags",
  "_type": "tag",
  "_id": "560b25d8276a6504d808d703",
  "_score": 1.0,
  "_source": {
    "name": "elastic indexed?",
    "child": [],
    "_users": ["foobar", "someMongoObjectID", "hellothere"],
    "_id": "560b25d8276a6504d808d703"
  }
}

查看上下文建議器是否可以滿足您的需求。

彼得,謝謝你的主意。 這是基於此的解決方案。

映射:

 {
  "tags": {
    "aliases": {},
    "mappings": {
      "tag": {
        "properties": {
          "_users": {
            "type": "string"
          },
          "child": {
            "type": "string"
          },
          "level": {
            "type": "double"
          },
          "name": {
            "type": "string",
            "fields": {
              "suggest": {
                "type": "completion",
                "analyzer": "simple",
                "payloads": true,
                "preserve_separators": true,
                "preserve_position_increments": true,
                "max_input_length": 50,
                "context": {
                  "_users": {
                    "type": "category",
                    "path": "_users",
                    "default": 'public' // documents with no `_users` or where 
                                        //_users.length  === 0 should be passed 
                                        //'public' as context in the query.
                  }
                }
              }
            }
          },
          "parent": {
            "type": "string"
          },
          "root": {
            "type": "boolean"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1443757145827",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "version": {
          "created": "1070299"
        },
        "uuid": "Uzb7Rc3sQIafgYD73CSp3g"
      }
    },
    "warmers": {}
  }
}

查詢:

 /_suggest
{
    "tag-suggest": {
        "text": "foo",
        "completion": {
            "field": "name.suggest",
            "fuzzy": {},
            "context": {
                "_users": "someuserID" // or `public` for tags that are not
                                       // associated with at least one user
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM