如果两个文档包含相同的单词,如何搜索弹性文档?

时间:2018-09-19 20:27:53

标签: elasticsearch

我有以下两个有弹性的文件:

文档1

{"name":"doc1","tags":["Hello World"]}

文档2

{"name":"doc2", "tags":["World"]}

我要搜索一个包含“世界”作为单个单词的文档,但是此查询返回两个文档:

{"query":{"bool":{"must":[{"match":{"tags":{"query":"World"}}}]}}}

我该如何实现?

2 个答案:

答案 0 :(得分:0)

如果映射的keyword字段中有tags,则可以使用它,例如

GET your_index/doc
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "tags.keyword": { // use `keyword` field here
              "query": "World"
            }
          }
        }
      ]
    }
  }
}

仅当您的映射如下时

{
  "your_index": {
    "mappings": {
      "doc": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "tags": {
            "type": "text",
            "fields": {
              "keyword": { 
                "type": "keyword", // this mapping is required
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

答案 1 :(得分:0)

您需要使用keyword Analyzer

curl -X PUT "http://localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": { 
          "type": "custom",
          "tokenizer": "keyword"
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "filedname": {
          "type": "text",
          "analyzer": "my_analyzer" 
        }
      }
    }
  }
}'
相关问题