当前位置: 首页 > 面试题库 >

Elasticsearch对数组字段的自动完成搜索

闾丘文昌
2023-03-14
问题内容

我正在对具有字符串类型数组的文档字段进行自动完成建议。我的文件如下所示;

{

    "title": "Product1",
    "sales": "6",
    "rating": "0.0",
    "cost": "45.00",
    "tags": [
        "blog",
        "magazine",
        "responsive",
        "two columns",
        "wordpress"
    ],
    "category": "wordpress",
    "description": "Product1 Description",
    "createDate": "2013-12-19"
}

{

    "title": "Product1",
    "sales": "6",
    "rating": "0.0",
    "cost": "45.00",
    "tags": [
        "blog",
        "paypal",
        "responsive",
        "skrill",
        "wordland"
    ],
    "category": "wordpress",
    "description": "Product1 Description",
    "createDate": "2013-12-19"
}

我正在 标签 字段上执行自动完成搜索。我的查询就像;

query: {
                    query_string: {
                        query: "word*",
                        fields: ["tags"]
                    }
                },
                facets: {
                    tags: {
                        terms: {
                            field: "tags"
                        }
                    }
                }

当用户键入“ word”时,我要显示“ wordland”和“ wordpress”。但是,我无法做到这一点。

您能帮上忙吗?

谢谢


问题答案:

您是否尝试过完成建议?解决问题的一种方法如下:

1)创建索引:

curl -XPUT "http://localhost:9200/test_index/"

2)使用完成建议者类型创建映射:

curl -XPUT "http://localhost:9200/test_index/product/_mapping" -d'
{
   "product": {
      "properties": {
         "category": {
            "type": "string"
         },
         "cost": {
            "type": "string"
         },
         "createDate": {
            "type": "date",
            "format": "dateOptionalTime"
         },
         "description": {
            "type": "string"
         },
         "rating": {
            "type": "string"
         },
         "sales": {
            "type": "string"
         },
         "tags": {
            "type": "string"
         },
         "title": {
            "type": "string"
         },
         "suggest": {
            "type": "completion",
            "index_analyzer": "simple",
            "search_analyzer": "simple",
            "payloads": false
         }
      }
   }
}'

3)添加文件:

curl -XPUT "http://localhost:9200/test_index/product/1" -d'
{
   "title": "Product1",
   "sales": "6",
   "rating": "0.0",
   "cost": "45.00",
   "tags": [
      "blog",
      "magazine",
      "responsive",
      "two columns",
      "wordpress"
   ],
   "suggest": {
      "input": [
         "blog",
         "magazine",
         "responsive",
         "two columns",
         "wordpress"
      ]
   },
   "category": "wordpress",
   "description": "Product1 Description",
   "createDate": "2013-12-19"
}'

curl -XPUT "http://localhost:9200/test_index/product/2" -d'
{

    "title": "Product2",
    "sales": "6",
    "rating": "0.0",
    "cost": "45.00",
    "tags": [
        "blog",
        "paypal",
        "responsive",
        "skrill",
        "wordland"
    ],
   "suggest": {
      "input": [
         "blog",
        "paypal",
        "responsive",
        "skrill",
        "wordland"
      ]
   },
    "category": "wordpress",
    "description": "Product1 Description",
    "createDate": "2013-12-19"
}'

4)然后使用_suggest端点进行查询:

curl -XPOST "http://localhost:9200/test_index/_suggest" -d'
{
    "product_suggest":{
        "text":"word",
        "completion": {
            "field" : "suggest"
        }
    }
}'

您将获得预期的结果:

{
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "product_suggest": [
      {
         "text": "word",
         "offset": 0,
         "length": 4,
         "options": [
            {
               "text": "wordland",
               "score": 1
            },
            {
               "text": "wordpress",
               "score": 1
            }
         ]
      }
   ]
}

当然,可以稍微改进此解决方案,特别是通过修剪一些重复的数据,但这应该为您指明正确的方向。



 类似资料:
  • 问题内容: 我对ES还是相当陌生,并正在将其用于我的新项目。首先,我为客户提供了一个简单的映射,其中包含名字和姓氏以及付款信息对象列表。如果我在SQL中执行此操作,那将类似于客户表和具有1:许多关系的付款信息表。 这是我要执行的操作的一个简单示例:https : //gist.github.com/anonymous/6109593 我希望根据payInfos嵌套数组中的任何匹配项找到任何客户,即

  • 我想从多个领域得到建议。我找不到这样的例子,所以也许这不是最好的主意,但我对你的意见很感兴趣。 要求是: GET查询适用于文本“fyodor”和“dostoevsky”,此示例仅适用于“fyodor” 启用筛选建议 我有什么想法可以实现这些?

  • 问题内容: Elasticsearch版本:7.1.1 嗨,我做了很多尝试,但是在索引中找不到任何解决方案,我有一个包含字符串的字段。 因此,例如,我有两个文档,它们在locations数组中包含不同的值。 文件1: 文件2: 用户请求搜索术语 克洛彭堡, 而我只想返回那些包含术语 克洛彭堡 而不是 Landkreis Cloppenburg的 文档。结果应仅包含 Document-1 。但是我的

  • 本文向大家介绍自动完成的搜索框javascript实现,包括了自动完成的搜索框javascript实现的使用技巧和注意事项,需要的朋友参考一下 在很多需要搜索的网站, 都会有一个自动完成的搜索框. 方便用户查找他们想要的搜索词. 帮助用户快速找到自己想要的结果. 这种方式是比较友好的. 所以是比较提倡使用的. 我们这次就来实现这一效果. 我们通过两篇文章来进行讲解. 首先我们来完成界面的设计布局.

  • 我确实在ElasticSearch中的字段中有一个数组数据,其中有一个关键字类型。我想用我想搜索的独占值搜索这个数组,即排除不包括在我的搜索关键字中的数组值。请看下面的细节。 谢了! 我有以下弹性搜索索引映射: 使用以下示例数据: 我的搜索是这样的: 我用过MatchQueryBuilder、TermQueryBuilder、TermsQueryBuilder都没用。根据ElasticSearch

  • 问题内容: 我有一个这样的映射: 我需要按对象的大小进行搜索。我已经试过了: 这是行不通的。给出错误 嵌套:ElasticSearchIllegalArgumentException [在类型为[post]的映射中未找到[提及]的字段]; 我还尝试过用替换脚本部分。这也是错误的 嵌套:ArrayIndexOutOfBoundsException [10]; 如何查询对象大小为2的记录? 问题答案: