如果结果以单词开头,则提升

Rap*_*lié 4 elasticsearch

我使用Elasticsearch使用ngram过滤器进行自动完成搜索.如果它以search关键字开头,我需要提升结果.

我的查询很简单:

"query": {
   "match": {
      "query": "re",
      "operator": "and"
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我的结果:

  • 重新加入
  • Couture等重新润色
  • 重新开始

但我希望他们这样:

  • 重新加入
  • 重新开始
  • Couture等重新润色

如何从关键字开始提升结果?

万一它可以帮助,这是我的映射:

{
    "settings": {
        "analysis": {
            "analyzer": {
                "partialAnalyzer": {
                    "type": "custom",
                    "tokenizer": "ngram_tokenizer",
                    "filter": ["asciifolding", "lowercase"]
                },
                "searchAnalyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": ["asciifolding", "lowercase"]
                }
            },

            "tokenizer": {
                "ngram_tokenizer": {
                    "type": "edge_ngram",
                    "min_gram": "1",
                    "max_gram": "15",
                    "token_chars": [ "letter", "digit" ]
                }
            }
        }
    },

    "mappings": {
        "place": {
            "properties": {
                "name": {
                    "type": "string",
                    "index_analyzer": "partialAnalyzer",
                    "search_analyzer": "searchAnalyzer",
                    "term_vector": "with_positions_offsets"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问候,

And*_*fan 7

这个想法怎么样,不是100%肯定它,因为它取决于我认为的数据:

  • 在你的name领域创建一个应该用分析keyword仪分析的子领域(几乎保持不变)
  • 更改查询是一个boolshould小号
  • 一个should是你现在的查询
  • 另一个shouldmatchphrase_prefix在子场.

映射:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "partialAnalyzer": {
          "type": "custom",
          "tokenizer": "ngram_tokenizer",
          "filter": [
            "asciifolding",
            "lowercase"
          ]
        },
        "searchAnalyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "asciifolding",
            "lowercase"
          ]
        },
        "keyword_lowercase": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "asciifolding",
            "lowercase"
          ]
        }
      },
      "tokenizer": {
        "ngram_tokenizer": {
          "type": "edge_ngram",
          "min_gram": "1",
          "max_gram": "15",
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "place": {
      "properties": {
        "name": {
          "type": "string",
          "index_analyzer": "partialAnalyzer",
          "search_analyzer": "searchAnalyzer",
          "term_vector": "with_positions_offsets",
          "fields": {
            "as_is": {
              "type": "string",
              "analyzer": "keyword_lowercase"
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

查询:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": {
              "query": "re",
              "operator": "and"
            }
          }
        },
        {
          "match": {
            "name.as_is": {
              "query": "re",
              "type": "phrase_prefix"
            }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)