elasticsearch 搜索的一个问题

新手上路,请多包涵

现有一索引 my_index,下面有q1,q2,q3字段,

用的分析器

"analyzer": {
    "my_custom_analyzer": {
      "type":      "custom",
      "tokenizer": "whitespace",
      "char_filter": [
        "html_strip"
      ],
      "filter": [
        "lowercase",
        "asciifolding"
      ]
    }
}

那么请问 如果现有搜索关键词为: abc def

查询如何写 能够得到q1,q2,q3中的一个或多个字段match到abc ,match到def的交集。

阅读 1.6k
1 个回答

我做了一个测试Demo
1、创建索引:

PUT /test_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type":      "custom",
          "tokenizer": "whitespace",
          "char_filter": [
            "html_strip"
          ],
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "q1": {
        "type": "text",
        "analyzer": "my_custom_analyzer"
      },
      "q2": {
        "type": "text",
        "analyzer": "my_custom_analyzer"
      },
      "q3": {
        "type": "text",
        "analyzer": "my_custom_analyzer"
      }
    }
  }
}

2、查看数据
查看映射

GET /test_index/_mapping
{
  "test_index" : {
    "mappings" : {
      "properties" : {
        "q1" : {
          "type" : "text",
          "analyzer" : "my_custom_analyzer"
        },
        "q2" : {
          "type" : "text",
          "analyzer" : "my_custom_analyzer"
        },
        "q3" : {
          "type" : "text",
          "analyzer" : "my_custom_analyzer"
        }
      }
    }
  }
}

查看设置

GET /test_index/_settings
{
  "test_index" : {
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "provided_name" : "test_index",
        "creation_date" : "1561303995802",
        "analysis" : {
          "analyzer" : {
            "my_custom_analyzer" : {
              "filter" : [
                "lowercase",
                "asciifolding"
              ],
              "char_filter" : [
                "html_strip"
              ],
              "type" : "custom",
              "tokenizer" : "whitespace"
            }
          }
        },
        "number_of_replicas" : "1",
        "uuid" : "ZficpCfFRICPgK-j3apRPw",
        "version" : {
          "created" : "7000099"
        }
      }
    }
  }
}

测试分词器:

GET /test_index/_analyze
{
  "analyzer": "my_custom_analyzer",
  "text": ["abc", "def", "aaa"]
}
{
  "tokens" : [
    {
      "token" : "abc",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "def",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "word",
      "position" : 101
    },
    {
      "token" : "aaa",
      "start_offset" : 8,
      "end_offset" : 11,
      "type" : "word",
      "position" : 202
    }
  ]
}

3、准备数据

PUT /test_index/_create/1
{
  "q1": "def",
  "q2": "abc",
  "q3": "aaa"
}

PUT /test_index/_create/2
{
  "q1": " aaa",
  "q2": "abc",
  "q3": "abc"
}

PUT /test_index/_create/3
{
  "q1": " aaa",
  "q2": "def",
  "q3": "def"
}

按照需求是要检索doc1
4、查询

GET /test_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "q1": {
                    "query": "abc"
                  }
                }
              },
              {
                "match": {
                  "q2": {
                    "query": "abc"
                  }
                }
              },
              {
                "match": {
                  "q3": {
                    "query": "abc"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "match": {
                  "q1": {
                    "query": "def"
                  }
                }
              },
              {
                "match": {
                  "q2": {
                    "query": "def"
                  }
                }
              },
              {
                "match": {
                  "q3": {
                    "query": "def"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.4508328,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.4508328,
        "_source" : {
          "q1" : "def",
          "q2" : "abc",
          "q3" : "aaa"
        }
      }
    ]
  }
}

测试时OK的,使用多字段查询

GET /test_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "abc",
            "fields": ["q1", "q2", "q3"]
          }
        },
        {
          "multi_match": {
            "query": "def",
            "fields": ["q1", "q2", "q3"]
          }
        }
      ]
    }
  }
}
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.4508328,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.4508328,
        "_source" : {
          "q1" : "def",
          "q2" : "abc",
          "q3" : "aaa"
        }
      }
    ]
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进