如何在Elasticsearch中自动将查询字符串拆分为多个或相关的查询?

时间:2017-01-26 15:40:48

标签: elasticsearch

我正在尝试使用Elasticsearch 2.3.3构建搜索查询,该查询能够匹配复合词。 因此我添加了trigram-tokenizer。 这是一个示例索引:

PUT /my_index
{
    "settings": {
    "analysis": {
      "analyzer": {
        "trigrams": {
          "tokenizer": "my_ngram_tokenizer"
          ,"filter": [
          "lowercase"
        ]
        }
      },
      "tokenizer": {
        "my_ngram_tokenizer": {
          "type": "nGram",
          "min_gram": "3",
          "max_gram": "3",
          "token_chars": [
            "letter",
            "digit",
            "symbol",
            "punctuation"
          ]
        }
      }
    }
},
    "mappings": {
        "my_type": {
            "properties": {
                "text": {
                    "type":     "string",
                    "analyzer": "trigrams" 
                }
            }
        }
    }
}

POST /my_index/my_type/_bulk
{ "index": { "_id": 1 }}
{ "text": "Aussprachewörterbuch" }
{ "index": { "_id": 2 }}
{ "text": "Militärgeschichte" }
{ "index": { "_id": 3 }}
{ "text": "Weißkopfseeadler" }
{ "index": { "_id": 4 }}
{ "text": "Weltgesundheitsorganisation" }
{ "index": { "_id": 5 }}
{ "text": "Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz" }

当您搜索gesundheit时,它会按预期匹配Weltgesundheitsorganisation。我希望它有一点用户友好性,但在多个搜索词之间有一个或一个关系。

我想要以下行为:搜索gesund adler匹配WeltgesundheitsorganisationWeißkopfseeadler

这是通过下面的第二个查询实现的,但不是第一个查询。 是否有更优雅的方式,而不是手动将查询拆分为多个should子句?

GET /my_index/my_type/_search
{
    "query": {
        "match": {
            "text": {
                "query":                "gesund adler",
                "minimum_should_match": "80%"
            }
        }
    }
}

GET /my_index/my_type/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "text": {
              "query": "gesund",
              "minimum_should_match": "80%"
            }
          }
        },
        {
          "match": {
            "text": {
              "query": "adler",
              "minimum_should_match": "80%"
            }
          }
        }
      ]
    }
  }
}

0 个答案:

没有答案