天天看点

es - elasticsearch search - DSL - span - span_near

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问:span_near有什么特点?

答:

es - elasticsearch search - DSL - span - span_near

问:span_near如何使用?

答:

# span_near_test
PUT /span_near_test
{
  "mappings": {
    "properties": {
      "name": {
        "type"  : "text",
        "fields": {
          "eng": {
            "type"    : "text",
            "analyzer": "english"
          }
        }
      },
      "age": {
        "type": "text"
      }
    }
  }
}

# 索引
POST /span_near_test/_doc/1
{
  "name": "hello good me foxes",
  "age" : "18"
}

# 索引
POST /span_near_test/_doc/2
{
  "name": "hello good me",
  "age" : "28"
}

# 索引
POST /span_near_test/_doc/3
{
  "name": "hello1 good1 foxes me",
  "age" : "58"
}

# 索引
POST /span_near_test/_doc/4
{
  "name": "hello1 good1 me1",
  "age" : "88"
}

# 索引
POST /span_near_test/_doc/5
{
  "name": "hello1 foxes good me1",
  "age" : "66"
}


# 搜索
GET /span_near_test/_search
{
  "query": {
    "span_near": {
      "clauses": [
        {"span_term": {"name": "hello"}},
        {"span_term": {"name": "me"}}
      ],
      "slop"    : 12,
      "in_order": true
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.003814,
    "hits" : [
      {
        "_index" : "span_near_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.003814,
        "_source" : {
          "name" : "hello good me",
          "age" : "28"
        }
      },
      {
        "_index" : "span_near_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.8643954,
        "_source" : {
          "name" : "hello good me foxes",
          "age" : "18"
        }
      }
    ]
  }
}

# 搜索
GET /span_near_test/_search
{
  "query": {
    "span_near": {
      "clauses": [
        {"span_term": {"name": "me"}},
        {"span_term": {"name": "hello"}}
      ],
      "slop"    : 12,
      "in_order": true
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}


# 搜索
GET /span_near_test/_search
{
  "query": {
    "span_near": {
      "clauses": [
        {"span_term": {"name": "me"}},
        {"span_term": {"name": "hello"}}
      ],
      "slop"    : 12,
      "in_order": false
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.5984278,
    "hits" : [
      {
        "_index" : "span_near_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.5984278,
        "_source" : {
          "name" : "hello good me",
          "age" : "28"
        }
      },
      {
        "_index" : "span_near_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5019071,
        "_source" : {
          "name" : "hello good me foxes",
          "age" : "18"
        }
      }
    ]
  }
}

# 搜索
GET /span_near_test/_search
{
  "query": {
    "span_near": {
      "clauses": [
        {"span_term": {"name": "hello1"}},
        {"span_term": {"name": "me1"}}
      ],
      "slop"    : 2,
      "in_order": false
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.5984278,
    "hits" : [
      {
        "_index" : "span_near_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.5984278,
        "_source" : {
          "name" : "hello1 good1 me1",
          "age" : "88"
        }
      },
      {
        "_index" : "span_near_test",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 0.41490984,
        "_source" : {
          "name" : "hello1 foxes good me1",
          "age" : "66"
        }
      }
    ]
  }
}


# 搜索
GET /span_near_test/_search
{
  "query": {
    "span_near": {
      "clauses": [
        {"span_term": {"name": "hello1"}},
        {"span_term": {"name": "me1"}}
      ],
      "slop"    : 1,
      "in_order": false
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5984278,
    "hits" : [
      {
        "_index" : "span_near_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.5984278,
        "_source" : {
          "name" : "hello1 good1 me1",
          "age" : "88"
        }
      }
    ]
  }
}

           

继续阅读