弹性搜索不同的记录

时间:2017-01-07 09:17:18

标签: elasticsearch

我在ES DEFINED BELOW中有两种类型

http://localhost/testindex/
{
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "autocomplete"
          ]
        },
        "autocomplete_search": {
          "tokenizer": "whitespace",
          "filter": [
            "lowercase"
          ]
        }
      },
      "filter": {
        "autocomplete": {
          "type": "nGram",
          "min_gram": 2,
          "max_gram": 40
        }
      }
    }
  },
  "mappings": {
    "table1": {
      "properties": {
        "title": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "id": {
          "type": "string",
          "analyzer": "autocomplete",
          "search_analyzer": "autocomplete_search"
        }
      }
    }
  }
}



http://localhost/testindex/table2/_mappings/
{
  "properties": {
    "title": {
      "type": "string"
    },
    "name": {
      "type": "string",
      "index": "not_analyzed"
    },
    "id1": {
      "type": "string",
      "analyzer": "autocomplete",
      "search_analyzer": "autocomplete_search"
    }
  }
}

将文档添加到类型

PUT testindex/table1/1
    {
  "title": "Labson Series LTD 2014",
"name":"a",
  "id": "36868FAE1"
    }

PUT testindex/table1/2
    {
  "title": "Labson Series LTD 2015",
"name":"b",
  "id": "45232dfw1"
    }


PUT testindex/table2/1
    {
  "title": "Labson Series LTD 2014",
"name":"a",
  "id1": "US36868FAE1"
    }

PUT testindex/table2/2
    {
  "title": "Labson Series LTD 2015",
"name":"b",
  "id1": "54722dfw1"
    }

在类似于sql查询的同一查询中搜索两种类型: 从表1中选择x,y,其中z ='' 联盟 从table2中选择a.b,其中c =''

http://localhost/testindex/_search/
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "query": {
            "bool": {
              "must": [
                {
                  "term": {
                    "_type": "table1"
                  }
                },
                {
                  "match": {
                    "id": {
                      "query": "36868FAE1",
                      "operator": "and"
                    }
                  }
                }
              ]
            }
          }
        },
        {
          "query": {
            "bool": {
              "must": [
                {
                  "term": {
                    "_type": "table2"
                  }
                },
                {
                  "match": {
                    "id1": {
                      "query": "36868FAE1",
                      "operator": "and"
                    }
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

但选择的输出如下: 从表1中选择x,y,其中z ='' 联合所有 从table2中选择a.b,其中c =''

"hits": [
{
"_index": "test",
"_type": "table2",
"_id": "1",
"_score": 0.35355338,
"_source": {
"title": "Labson Series LTD 2014",
"name": "a"
}
}
,
{
"_index": "test",
"_type": "table1",
"_id": "1",
"_score": 0.35355338,
"_source": {
"title": "Labson Series LTD 2014",
"name": "a"
}
}
]

在同一查询中搜索两种类型时,查找标题和名称的不同记录 预期产出是:

"hits": [
{
"_index": "test",
"_type": "table1",
"_id": "1",
"_score": 0.35355338,
"_source": {
"title": "Labson Series LTD 2014",
"name": "a"
}
}
]

我知道ES有一些围绕聚合查询的东西,但我不知道如何使用它我的当前场景

0 个答案:

没有答案