简体   繁体   English

弹性搜索优先从匹配开始

[英]Elastic search give priority to starts with match

I am using elasticsearch 6.8 version for document indexing and I have this requirement when searching.我正在使用 elasticsearch 6.8 版本进行文档索引,搜索时我有这个要求。 I have created an index with the following settings.我使用以下设置创建了一个索引。

{
  "settings": {
"number_of_shards": 3,
"number_of_replicas": 2,
"analysis": {
  "analyzer": {
    "default": {
      "type": "custom",
      "tokenizer": "icu_tokenizer"
    }
  }
}
  },
      "mappings": {
"person": {
  "properties": {
    "firstName": {
      "type": "text"
    },
    "lastName": {
      "type": "text"
    },
    "technologies": {
      "type": "nested"
    }
  }
}
  }
}

For example, I am creating 3 documents with following technologies.例如,我正在使用以下技术创建 3 个文档。

  • 1st document- "technologies" :["my test technology"]第一个文档-“技术”:[“我的测试技术”]
  • 2nd document - "technologies" :["test technology"]第二个文件 - “技术”:[“测试技术”]
  • 3rd document "technologies" :["tech test"]第三个文件“技术”:[“技术测试”]

So when search by keyword - "test", it is returning all the documents, that is correct behavior, but I want the 2nd document get priority because it starts with test*.因此,当按关键字搜索 - “test”时,它返回所有文档,这是正确的行为,但我希望第二个文档获得优先级,因为它以 test* 开头。 So my requirement is to get starts with value the priority as below.所以我的要求是从价值优先级开始,如下所示。

Search results should be:搜索结果应该是:

  • 1st result - 2nd document - "technologies" :["test technology"] Others can be in any order第一个结果 - 第二个文档 - “技术”:[“测试技术”] 其他可以按任何顺序排列
  • 1st document- "technologies" :["my test technology"]第一个文档-“技术”:[“我的测试技术”]
  • 3rd document "technologies" :["tech test"]第三个文件“技术”:[“技术测试”]

Here is my java code for searching.这是我用于搜索的java代码。 I am using elasticsearch-rest-high-level-client library.我正在使用 elasticsearch-rest-high-level-client 库。

SearchRequest searchRequest = new SearchRequest("profile1"); SearchRequest searchRequest = new SearchRequest("profile1"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

    QueryBuilder queryBuilder = QueryBuilders
            .boolQuery()
            .must(QueryBuilders
                    .matchQuery("technologies.name", technology));

    searchSourceBuilder.query(QueryBuilders
            .nestedQuery("technologies",
                    queryBuilder,
                    ScoreMode.Avg));

    searchRequest.source(searchSourceBuilder);

    SearchResponse response =
            client.search(searchRequest, RequestOptions.DEFAULT);

Is there something I am missing searching documents?我在搜索文档时遗漏了什么吗? Please help me on this.请帮我解决这个问题。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM