can I prioritize more exact matches when using ngram filter in search results?

前端 未结 3 2207
一整个雨季
一整个雨季 2021-02-20 15:14

When using the ngram filter with elasticsearch so that when I search for something like \"test\" I return a document \"latest\", \"tests\" and \"test\". Is there a way to make i

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 16:13

    You can copy the field content to fields via the mapping. Example:

      "fullName": {
        "type": "string",
        "search_analyzer": "str_search_analyzer",
        "index_analyzer": "str_index_analyzer",
        "fields": {
            "fullWord": { "type": "string" },
            "raw": { 
                "type":  "string",
                "index": "not_analyzed"
            }
        }
      }
    

    Note that str_index_analyzer uses nGram here. Then you can build your search to also search against these fields. Example:

    {
        "query": {
          "bool": {
            "should": [{
              "multi_match": {
                "fields": [
                  "firstName.fullWord",
                  ...
                "query": query,
                "fuzziness": "0"
              }
            }],
            "must": [{
              "multi_match": {
                "fields": [
                  "firstName",...],
                "query": query,
                "fuzziness": "AUTO"
              }
            }]
          }
        }
      };
    }
    

提交回复
热议问题