【问题标题】:Get only the matching values and corresponding fields from ElasticSearch仅从 ElasticSearch 中获取匹配的值和对应的字段
【发布时间】:2018-12-13 12:16:09
【问题描述】:

在elasticsearch中,假设我有像

这样的文档
{
  "name": "John",
  "department": "Biology",
  "address": "445 Mount Eden Road"
},
{
  "name": "Jane",
  "department": "Chemistry",
  "address": "32 Wilson Street"
},
{
  "name": "Laura",
  "department": "BioTechnology",
  "address": "21 Greens Road"
},
{
  "name": "Mark",
  "department": "Physics",
  "address": "Random UNESCO Bio-reserve"
}

有一个用例,如果我在搜索栏中键入“bio”,我应该从 elasticsearch 获取匹配的字段值以及字段名称。

对于这个例子,

输入:“简历”

预期输出

{
  "field": "department",
  "value": "Biology"
},
{
  "field": "department",
  "value": "BioTechnology"
},
{
  "field": "address",
  "value": "Random UNESCO Bio-reserve"
}

我应该使用什么类型的查询?我可以考虑使用 NGram Tokenizer,然后使用匹配查询。但是,我不确定如何只获取匹配的字段值(而不是整个文档)和相应的字段名称作为输出。

【问题讨论】:

    标签: elasticsearch n-gram


    【解决方案1】:

    在进一步阅读了Completion SuggestersContext Suggesters 之后,我可以通过以下方式解决这个问题:

    1) 为每个类型为“completion”且上下文映射类型为“category”的记录保留一个单独的“suggest”字段。我创建的映射如下所示:

    {
      "properties": {
        "suggest": {
          "type": "completion",
          "contexts": [
            {
              "name": "field_type",
              "type": "category",
              "path": "cat"
            }
          ]
        },
        "name": {
          "type": "text"
        },
        "department": {
          "type": "text"
        },
        "address": {
          "type": "text"
        }
      }
    }
    

    2) 然后我插入如下所示的记录(将搜索元数据添加到具有适当“上下文”的“建议”字段)。

    例如,要插入第一条记录,我执行以下操作:

    POST:本地主机:9200/test_index/test_type/1

    {
        "suggest": [
            {
                "input": ["john"],
                "contexts": {
                    "field_type": ["name"] 
                }
            },
            {
                "input": ["biology"],
                "contexts": {
                    "field_type": ["department"] 
                }
            },
            {
                "input": ["445 mount eden road"],
                "contexts": {
                    "field_type": ["address"] 
                }
            }
        ],
        "name": "john",
        "department": "biology",
        "address": "445 mount eden road"
    }
    

    3) 如果我们要搜索出现在句子中间的词条(因为搜索词“bio”出现在第 4 条记录的地址字段中间,我们可以按如下方式索引该条目:

    POST:本地主机:9200/test_index/test_type/4

    {
        "suggest": [
            {
                "input": ["mark"],
                "contexts": {
                    "field_type": ["name"] 
                }
            },
            {
                "input": ["physics"],
                "contexts": {
                    "field_type": ["department"] 
                }
            },
            {
                "input": ["random unesco bio-reserve", "bio-reserve"],
                "contexts": {
                    "field_type": ["address"] 
                }
            }
        ],
        "name": "mark",
        "department": "physics",
        "address": "random unesco bio-reserve"
    }
    

    4)然后像这样搜索关键字“bio”:

    localhost:9200/test_index/test_type/_search

    {
        "_source": false,
        "suggest": {
            "suggestion" : {
                "text" : "bio",
                "completion" : {
                    "field" : "suggest",
                    "size": 10,
                    "contexts": {
                        "field_type": [ "name", "department", "address" ]
                    }
                }
            }
        }
    }
    

    回应:

    {
        "hits": {
            "total": 0,
            "max_score": 0,
            "hits": []
        },
        "suggest": {
            "suggestion": [
                {
                    "text": "bio",
                    "offset": 0,
                    "length": 3,
                    "options": [
                        {
                            "text": "bio-reserve",
                            "_index": "test_index",
                            "_type": "test_type",
                            "_id": "4",
                            "_score": 1,
                            "contexts": {
                                "field_type": [
                                    "address"
                                ]
                            }
                        },
                        {
                            "text": "biology",
                            "_index": "test_index",
                            "_type": "test_type",
                            "_id": "1",
                            "_score": 1,
                            "contexts": {
                                "field_type": [
                                    "department"
                                ]
                            }
                        },
                        {
                            "text": "biotechnology",
                            "_index": "test_index",
                            "_type": "test_type",
                            "_id": "3",
                            "_score": 1,
                            "contexts": {
                                "field_type": [
                                    "department"
                                ]
                            }
                        }
                    ]
                }
            ]
        }
    }
    

    谁能推荐更好的方法?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 2021-05-15
      • 2020-04-24
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多