简体   繁体   English

ElasticSearch多字段搜索

[英]Elasticsearch search with multi fields

我如何像这样为弹性搜索创建身体

select * from table where full_name like '%q%' or address like '%q%' or description like '%q%' order by full_name , description , address

A wildcard query can be very expensive, especially if you search in several fields. 通配符查询可能非常昂贵,尤其是在多个字段中搜索时。 The right way to do this is by using an nGram token filter on the fields you want to search only a part of. 正确的方法是在仅要搜索一部分的字段上使用nGram令牌过滤器

First you create an index like below with a custom analyzer that will slice and dice your fields into searchable tokens: 首先,使用自定义分析器创建如下所示的索引,该索引会将您的字段切成小块并切成可搜索的标记:

curl -XPUT localhost:9200/tests -d '{
  "settings": {
    "analysis": {
      "analyzer": {
        "substring_analyzer": {
          "tokenizer": "standard",
          "filter": ["lowercase", "substring"]
        }
      },
      "filter": {
        "substring": {
          "type": "nGram",
          "min_gram": 1,
          "max_gram": 15
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "full_name": {
          "type": "string",
          "analyzer": "substring_analyzer"
        },
        "address": {
          "type": "string",
          "analyzer": "substring_analyzer"
        },
        "description": {
          "type": "string",
          "analyzer": "substring_analyzer"
        }
      }
    }
  }
}'

Then you can index a few docs: 然后,您可以索引一些文档:

curl -XPUT localhost:9200/tests/test/_bulk -d '
{"index":{"_id": 1}}
{"full_name": "Doe", "address": "1234 Quinn Street", "description": "Lovely guy"}
{"index":{"_id": 2}}
{"full_name": "Brennan", "address": "4567 Main Street", "description": "Not qualified"}
{"index":{"_id": 3}}
{"full_name": "Quantic", "address": "1234 Quinn Street", "description": "New friend"}
'

Finally, you can search with a query equivalent to your SQL query above and all three test documents will match: 最后,您可以使用与上述SQL查询等效的查询进行搜索,并且所有三个测试文档都将匹配:

curl -XPUT localhost:9200/tests/test/_search -d '{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "full_name": "q"
          }
        },
        {
          "match": {
            "address": "q"
          }
        },
        {
          "match": {
            "description": "q"
          }
        }
      ]
    }
  }
}'

You can try the following. 您可以尝试以下方法。 . . https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html ` https:// www.elastic.co/ guide/ zh- CN/ elasticsearch/ reference/ current/ query- dsl- wildcard- query.html`

{
    "wildcard" : { "user" : "ki*y" }
}

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM