首页 > 解决方案 > ElasticSearch 7.6.3 Java HighLevel Rest Client:跨多个字段的自动建议 - 如何实现

问题描述

我们有一个包含以下字段的索引,并且需要通过在索引中的所有文本和关键字映射字段中搜索数据来向用户提供自动建议

{


"settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
     }
  },

  "mappings": {
    "properties": {
      "id": {
        "type": "text"
      },
      "title": {
        "type": "text"
      },
      "date": {
        "type": "date",
        "format": "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
      },
      "subject": {
        "type": "text"
      },
      "title_suggest": {
            "type": "completion",
            "analyzer": "simple",
            "preserve_separators": true,
            "preserve_position_increments": true,
            "max_input_length": 50
        },
        "subject_suggest": {
            "type": "completion",
            "analyzer": "simple",
            "preserve_separators": true,
            "preserve_position_increments": true,
            "max_input_length": 50

        }
      "fieldOr": {
        "type": "text"
      },
      "fieldsTa": {
        "type": "text"
      },
      "notes": {
        "type": "text"
      },
      "fileDocs": {
        "type": "nested",
        "properties": {
          "fileName": {
            "type": "text",
            "analyzer": "autocomplete", 
            "search_analyzer": "standard"
          },
          "fileContent": {
            "type": "text",
            "analyzer": "autocomplete", 
            "search_analyzer": "standard" 
          },
          "docType": {
            "type": "keyword"
          },
          "opinionId": {
            "type": "integer"
          }
        }
      },   
      "fileMeta": {
        "type": "nested",
        "properties": {
          "url": {
            "type": "text"
          },
          "name": {
            "type": "text"
          }
        }
      }
    }
  }
}

我已经尝试过 Completion Suggest,但它适用于 1 个字段。我在索引中创建了 2 个带有 *-suggest 的字段,并尝试使用 completionSuggest 创建建议

SuggestBuilders.completionSuggestion("my_index_suggest").text(input);

但它只支持 1 个字段。我将 ES 7.6.3 与 Java HighLevel Rest Client 一起使用,它适用于 1 个字段。我需要做哪些改变来支持跨多个领域。这可以通过 JSON 搜索实现吗?如果是,那么我可以使用 Xcontentbuilder 创建一个 json 并执行自动建议?

标签: elasticsearchautocompleteresthighlevelclient

解决方案


使用copy_to并将您所需的所有字段复制到一个字段并在其上执行您的建议。

copy_to 文档中的示例是,

PUT my_index
{
  "mappings": {
    "properties": {
      "first_name": {
        "type": "text",
        "copy_to": "full_name" 
      },
      "last_name": {
        "type": "text",
        "copy_to": "full_name" 
      },
      "full_name": {
        "type": "text"
      }
    }
  }
}

推荐阅读