ElasticSearch 7.6.3 Java HighLevel Rest Client:跨多个字段自动建议 - 如何实施

ElasticSearch 7.6.3 Java HighLevel Rest Client : Auto Suggest across multiple fields - How to implement

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

{


"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 个字段。我在索引中创建了带有 *-suggest 的 2 个字段,并尝试使用 completionSuggest

创建 Suggest
SuggestBuilders.completionSuggestion("my_index_suggest").text(input);

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

使用 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"
      }
    }
  }
}

出于说明目的,我使用自己的索引映射,它只有两个字段 nameaddress,我将在这两个字段上使用 prefix 和您可以类似地包含更多字段。

索引映射

{
  "employee": {
    "mappings": {
      "properties": {
        "address": {
          "type": "text"
        },
        "name": {
          "type": "text"
        }
      }
    }
  }
}

使用 Rest 高级客户端搜索查询

 public SearchResponse autosuggestSearch() throws IOException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder qb = QueryBuilders.boolQuery();

        PrefixQueryBuilder namePQBuilder = QueryBuilders.prefixQuery("address", "usa");
        PrefixQueryBuilder addressPQBuilder = QueryBuilders.prefixQuery("address", "usa");
        qb.should(namePQBuilder);
        qb.should(addressPQBuilder); //Similarly add more fields prefix queries.
        sourceBuilder.query(qb);

        SearchRequest searchRequest = new SearchRequest("employee").source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("Search JSON query \n" + searchRequest.source().toString()); //Generated ES search JSON.
        return searchResponse;
    }

对于此示例生成的搜索 JSON

{
  "query": {
    "bool": {
      "should": [
        {
          "prefix": {
            "address": {
              "value": "usa",
              "boost": 1.0
            }
          }
        },
        {
          "prefix": {
            "address": {
              "value": "usa",
              "boost": 1.0
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  }
}
}