代码之家  ›  专栏  ›  技术社区  ›  Mohammad Banisaeid

Elasticsearch:在文本末尾查找术语

  •  0
  • Mohammad Banisaeid  · 技术社区  · 7 年前

    我正在Elasticsearch 5.x中为以下文档编制索引:

    {
        "id": 1
        "description": "The quick brown fox"
    }
    
    {
        "id": 2
        "description": "The fox runs fast"
    }
    
    

    如果我搜索这个词 "fox" description 字段以单词结尾 “狐狸” ? 在上面的例子中,我看到的是 id=1

    Query String .

    2 回复  |  直到 7 年前
        1
  •  1
  •   LeBigCat    7 年前

    好吧,正则表达式应该有用。注意:如果您需要花费大量时间查找End,那么使用analyzer重新编制索引将是最好的选择( https://discuss.elastic.co/t/elasticsearch-ends-with-word-in-phrases/60473 )

    "regexp":{
                "id": {
                    "value": "*fox",
                }
            }
    
        2
  •  0
  •   Adam T    7 年前

    确保索引映射包含 keyword field 为了描述。例如:

    PUT my_index
    {
      "mappings": {
        "_doc": {
          "properties": {
            "description": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "id": {
              "type": "long"
            }
          }
        }
      }
    }
    

    添加文档:

    POST my_index/_doc
    {
        "id": 1,
        "description": "The quick brown fox"
    }
    
    POST my_index/_doc
    {
        "id": 2,
        "description": "The fox runs fast"
    }
    

    并使用如下查询字符串:

    GET /my_index/_search
    {
        "query": {
            "query_string" : {
                "default_field" : "description.keyword",
                "query" : "*fox"
            }
        }
    }