弹性搜索与多个字段 [英] Elasticsearch search with multi fields

查看:104
本文介绍了弹性搜索与多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为这个弹性搜索创建一个body

How can i create body for elasticsearch like this

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


推荐答案

通配符查询可能非常昂贵,特别是在多个字段中进行搜索时。正确的方法是使用 nGram 令牌过滤器您只想搜索一部分的字段。

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.

首先创建一个索引如下,使用自定义分析器将切片并将您的字段切成可搜索令牌:

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"
        }
      }
    }
  }
}'

然后你可以索引几个文档: / p>

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"}
'

最后,您可以搜索与上面的SQL查询相当的查询,所有三个测试文档将匹配:

Finally, you can search with a query equivalent to your SQL query above and all three test documents will match:

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

这篇关于弹性搜索与多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆